'use server' import { tasksService } from '@/services/tasks'; import { revalidatePath } from 'next/cache'; import { TaskStatus, TaskPriority } from '@/lib/types'; export type ActionResult = { success: boolean; data?: T; error?: string; }; /** * Server Action pour mettre à jour le statut d'une tâche */ export async function updateTaskStatus( taskId: string, status: TaskStatus ): Promise { try { const task = await tasksService.updateTask(taskId, { status }); // Revalidation automatique du cache revalidatePath('/'); revalidatePath('/tasks'); return { success: true, data: task }; } catch (error) { console.error('Error updating task status:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to update task status' }; } } /** * Server Action pour mettre à jour le titre d'une tâche */ export async function updateTaskTitle( taskId: string, title: string ): Promise { try { if (!title.trim()) { return { success: false, error: 'Title cannot be empty' }; } const task = await tasksService.updateTask(taskId, { title: title.trim() }); // Revalidation automatique du cache revalidatePath('/'); revalidatePath('/tasks'); return { success: true, data: task }; } catch (error) { console.error('Error updating task title:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to update task title' }; } } /** * Server Action pour supprimer une tâche */ export async function deleteTask(taskId: string): Promise { try { await tasksService.deleteTask(taskId); // Revalidation automatique du cache revalidatePath('/'); revalidatePath('/tasks'); return { success: true }; } catch (error) { console.error('Error deleting task:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to delete task' }; } } /** * Server Action pour mettre à jour une tâche complète (formulaire d'édition) */ export async function updateTask(data: { taskId: string; title?: string; description?: string; status?: TaskStatus; priority?: TaskPriority; tags?: string[]; dueDate?: Date; }): Promise { try { const updateData: Record = {}; if (data.title !== undefined) { if (!data.title.trim()) { return { success: false, error: 'Title cannot be empty' }; } updateData.title = data.title.trim(); } if (data.description !== undefined) updateData.description = data.description.trim(); if (data.status !== undefined) updateData.status = data.status; if (data.priority !== undefined) updateData.priority = data.priority; if (data.tags !== undefined) updateData.tags = data.tags; if (data.dueDate !== undefined) updateData.dueDate = data.dueDate; const task = await tasksService.updateTask(data.taskId, updateData); // Revalidation automatique du cache revalidatePath('/'); revalidatePath('/tasks'); return { success: true, data: task }; } catch (error) { console.error('Error updating task:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to update task' }; } } /** * Server Action pour créer une nouvelle tâche */ export async function createTask(data: { title: string; description?: string; status?: TaskStatus; priority?: TaskPriority; tags?: string[]; }): Promise { try { if (!data.title.trim()) { return { success: false, error: 'Title is required' }; } const task = await tasksService.createTask({ title: data.title.trim(), description: data.description?.trim() || '', status: data.status || 'todo', priority: data.priority || 'medium', tags: data.tags || [] }); // Revalidation automatique du cache revalidatePath('/'); revalidatePath('/tasks'); return { success: true, data: task }; } catch (error) { console.error('Error creating task:', error); return { success: false, error: error instanceof Error ? error.message : 'Failed to create task' }; } }