From 3443805c7bebd8a42c4a2b6a3b47df1ccdedb81b Mon Sep 17 00:00:00 2001 From: Alma Date: Sat, 12 Apr 2025 20:05:46 +0200 Subject: [PATCH] working leantime widget 77 --- app/api/leantime/tasks/route.ts | 6 ++-- components/flow.tsx | 61 ++++++++++++++++----------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/app/api/leantime/tasks/route.ts b/app/api/leantime/tasks/route.ts index 453ae21b..25829930 100644 --- a/app/api/leantime/tasks/route.ts +++ b/app/api/leantime/tasks/route.ts @@ -160,8 +160,8 @@ export async function GET(request: NextRequest) { status: task.status }); - // Skip completed tasks (status 3) - if (task.status === 3) { + // Skip completed tasks (status 5) + if (task.status === 5) { return false; } @@ -173,8 +173,10 @@ export async function GET(request: NextRequest) { // 1. The user is the editor // 2. The task is unassigned (empty editorId) // 3. The user is in the assignedTo array + // 4. The task's userId matches the current user return taskEditorId === currentUserId || taskEditorId === '' || + task.userId === userId || (Array.isArray(task.assignedTo) && task.assignedTo.some((id: any) => String(id).trim() === currentUserId)); }) diff --git a/components/flow.tsx b/components/flow.tsx index 51e1d3b7..1a2bdaf5 100644 --- a/components/flow.tsx +++ b/components/flow.tsx @@ -10,13 +10,17 @@ interface Task { id: number; headline: string; description: string; + dateToFinish: string; + date: string; + projectId: number; projectName: string; status: number; - dateToFinish: string; - editorId: string; - editorFirstname: string | null; - editorLastname: string | null; - userId: number; + editorId?: string; + editorFirstname?: string; + editorLastname?: string; + authorFirstname: string; + authorLastname: string; + milestoneHeadline?: string; } interface ProjectSummary { @@ -70,39 +74,34 @@ export function Flow() { } }; - const fetchTasks = async (isRefresh = false) => { + const fetchTasks = async () => { + setLoading(true); try { - setLoading(true); const response = await fetch('/api/leantime/tasks'); + if (!response.ok) { + throw new Error('Failed to fetch tasks'); + } const data = await response.json(); + + if (!Array.isArray(data)) { + console.warn('No tasks found in response', data as unknown); + setTasks([]); + return; + } - // Filter and sort tasks - const sortedTasks = data.tasks - .filter((task: Task) => - task.headline && - typeof task.headline === 'string' && - task.status !== 5 && // Not done - (task.editorId === '2' || task.userId === 2) // Tasks assigned to or owned by user 2 - ) + // Sort tasks by due date (oldest first) + const sortedTasks = data .sort((a: Task, b: Task) => { - // Get valid dates for comparison - const getValidDate = (task: Task) => { - if (task.dateToFinish && task.dateToFinish !== '0000-00-00 00:00:00') { - return new Date(task.dateToFinish).getTime(); - } - return Number.MAX_SAFE_INTEGER; - }; - - const dateA = getValidDate(a); - const dateB = getValidDate(b); + const dateA = a.dateToFinish ? new Date(a.dateToFinish).getTime() : new Date(a.date).getTime(); + const dateB = b.dateToFinish ? new Date(b.dateToFinish).getTime() : new Date(b.date).getTime(); return dateA - dateB; - }); + }) + .slice(0, 6); // Limit to 6 tasks - console.log('Filtered and sorted tasks:', sortedTasks); + console.log('Sorted and filtered tasks:', sortedTasks); setTasks(sortedTasks); - setError(null); - } catch (err) { - console.error('Error fetching tasks:', err); + } catch (error) { + console.error('Error fetching tasks:', error); setError('Failed to fetch tasks'); } finally { setLoading(false); @@ -120,7 +119,7 @@ export function Flow() {