import { Edit } from "iconsax-react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import ModalConfrim from "../../../../../components/ModalConfrim"; import { toast } from "../../../../../components/Toast"; import ProgressBar from "../../../../../components/ProgressBar"; import { ErrorType } from "../../../../../helpers/types"; import { useDeleteCheckListItem, useUpdateCheckListItem } from "../../../task/hooks/useTaskData"; import type { TaskChecklistItemType } from "../../../task/types/TaskTypes"; import ChecklistItem from "./ChecklistItem"; type Props = { taskId: string; checkListItems?: TaskChecklistItemType[]; checkListItemCount?: number; completedCheckListItemCount?: number; }; const CheckLists = ({ taskId, checkListItems = [], checkListItemCount, completedCheckListItemCount, }: Props) => { const { t } = useTranslation("global"); const updateCheckListItem = useUpdateCheckListItem(); const deleteCheckListItem = useDeleteCheckListItem(); const [deleteItemId, setDeleteItemId] = useState(null); const items = checkListItems.map((item) => ({ id: item.id, title: item.title, checked: item.isDone, })); const totalCount = checkListItemCount ?? items.length; const doneCount = completedCheckListItemCount ?? items.filter((item) => item.checked).length; const progress = totalCount > 0 ? Math.round((doneCount / totalCount) * 100) : 0; const handleToggle = (id: string, title: string, checked: boolean) => { updateCheckListItem.mutate( { id, params: { title, isDone: !checked, taskId }, taskId }, { onError: (error: ErrorType) => { toast(error.response?.data?.error.message[0], "error"); }, }, ); }; const handleSave = (id: string, title: string, checked: boolean) => { updateCheckListItem.mutate( { id, params: { title, isDone: checked, taskId }, taskId }, { onError: (error: ErrorType) => { toast(error.response?.data?.error.message[0], "error"); }, }, ); }; const handleDelete = () => { if (!deleteItemId) return; deleteCheckListItem.mutate( { id: deleteItemId, taskId }, { onSuccess: () => { setDeleteItemId(null); }, onError: (error: ErrorType) => { toast(error.response?.data?.error.message[0], "error"); }, }, ); }; if (checkListItems.length === 0) return null; return (
{t("taskmanager.task_detail.checklist")}
{progress}٪
{items.map((item) => ( handleToggle(item.id, item.title, item.checked)} onSave={(title) => handleSave(item.id, title, item.checked)} onDelete={() => setDeleteItemId(item.id)} isSaving={updateCheckListItem.isPending} /> ))}
setDeleteItemId(null)} onConfrim={handleDelete} isLoading={deleteCheckListItem.isPending} />
); }; export default CheckLists;