/*********************************************************************************************************************************************************************** * File Name : TaskContext.jsx * Type : Context / Provider * Description : Client task context. * Covers: task lists for a group (with status filter), single task list, * single task with requirements + latest completion, * completion history, and task completion (submit). * Used by: GroupList.jsx (task lists), ViewTaskDetails.jsx (task + completions) ***********************************************************************************************************************************************************************/ import { createContext, useCallback, useContext, useState } from 'react'; import api from '@/utils/api.util'; import { toast } from 'sonner'; const BASE = '/client/groups'; const TaskContext = createContext(null); export function useTask() { const ctx = useContext(TaskContext); if (!ctx) throw new Error('useTask must be used within a TaskProvider'); return ctx; } export function TaskProvider({ children }) { // ── Task List state ─────────────────────────────────────────────────────── const [taskLists, setTaskLists] = useState([]); const [taskList, setTaskList] = useState(null); // ── Task state ──────────────────────────────────────────────────────────── const [task, setTask] = useState(null); // ── Completion state ────────────────────────────────────────────────────── // latest_completion is embedded in task from getTask — kept separately // so the submit action can update it without refetching the whole task. const [completions, setCompletions] = useState([]); // full history const [latestCompletion, setLatestCompletion] = useState(null); // ── Shared ──────────────────────────────────────────────────────────────── const [loading, setLoading] = useState(false); const [completionLoading, setCompletionLoading] = useState(false); const request = useCallback(async (fn) => { setLoading(true); try { return await fn(); } catch (err) { const message = err?.response?.data?.message ?? 'Something went wrong.'; toast.error(message); return null; } finally { setLoading(false); } }, []); const completionRequest = useCallback(async (fn) => { setCompletionLoading(true); try { return await fn(); } catch (err) { const message = err?.response?.data?.message ?? 'Something went wrong.'; toast.error(message); return null; } finally { setCompletionLoading(false); } }, []); // ══════════════════════════════════════════════════════════════════════════ // TASK LISTS // ══════════════════════════════════════════════════════════════════════════ // ─── GET TASK LISTS FOR GROUP ───────────────────────────────────────────── // GET /client/groups/:groupId/task-lists?status=ongoing|done|overdue const fetchTaskLists = useCallback( (groupId, { status } = {}) => request(async () => { const res = await api.get(`${BASE}/${groupId}/task-lists`, { params: status ? { status } : {}, }); const data = res.data?.data ?? []; setTaskLists(data); return data; }), [request] ); // ───────────────────────────────────────────────────────────────────────────── // PATCH: fetchTaskList in TaskContext.jsx (client) — add params support // ───────────────────────────────────────────────────────────────────────────── const fetchTaskList = useCallback( (groupId, taskListId, params = {}) => request(async () => { const res = await api.get(`${BASE}/${groupId}/task-lists/${taskListId}`, { params }); const data = res.data?.data ?? null; setTaskList(data); return data; }), [request] ); // ══════════════════════════════════════════════════════════════════════════ // TASKS // ══════════════════════════════════════════════════════════════════════════ // ─── GET ONE TASK ───────────────────────────────────────────────────────── // GET /client/groups/:groupId/task-lists/:taskListId/tasks/:taskId // Response includes requirements[] and latest_completion const fetchTask = useCallback( (groupId, taskListId, taskId) => request(async () => { const res = await api.get( `${BASE}/${groupId}/task-lists/${taskListId}/tasks/${taskId}` ); const data = res.data?.data ?? null; setTask(data); // Seed latest completion from the embedded payload if (data?.latest_completion !== undefined) { setLatestCompletion(data.latest_completion); } return data; }), [request] ); // ══════════════════════════════════════════════════════════════════════════ // COMPLETIONS // ══════════════════════════════════════════════════════════════════════════ // ─── GET MY COMPLETION HISTORY ──────────────────────────────────────────── // GET /client/groups/:groupId/task-lists/:taskListId/tasks/:taskId/completions const fetchCompletions = useCallback( (groupId, taskListId, taskId) => completionRequest(async () => { const res = await api.get( `${BASE}/${groupId}/task-lists/${taskListId}/tasks/${taskId}/completions` ); const data = res.data?.data ?? []; setCompletions(data); // Keep latest in sync if (data.length) setLatestCompletion(data[0]); return data; }), [completionRequest] ); // ─── COMPLETE TASK (SUBMIT) ─────────────────────────────────────────────── // POST /client/groups/:groupId/task-lists/:taskListId/tasks/:taskId/completions // Body: { note?: string, files: [{ file_url, file_name, file_size?, mime_type? }] } const completeTask = useCallback( (groupId, taskListId, taskId, payload) => completionRequest(async () => { const res = await api.post( `${BASE}/${groupId}/task-lists/${taskListId}/tasks/${taskId}/completions`, payload ); const data = res.data?.data ?? null; toast.success('Task submitted successfully.'); // Immediately update latest completion so UI reflects the new state setLatestCompletion(data); // Prepend to history if it's already loaded setCompletions((prev) => (prev.length ? [data, ...prev] : prev)); return data; }), [completionRequest] ); return ( {children} ); }