working leantime widget 77

This commit is contained in:
Alma 2025-04-12 20:05:46 +02:00
parent 12695cc58f
commit 3443805c7b
2 changed files with 34 additions and 33 deletions

View File

@ -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));
})

View File

@ -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() {
<Button
variant="ghost"
size="icon"
onClick={() => fetchTasks(true)}
onClick={() => fetchTasks()}
className="h-8 w-8 p-0 hover:bg-gray-100/50 rounded-full"
>
<RefreshCw className="h-4 w-4" />