import { Fragment } from "react"; import { mutate } from "swr"; import { Dialog, Transition } from "@headlessui/react"; // services import { CycleService } from "services/cycle.service"; // hooks import useToast from "hooks/use-toast"; // components import { CycleForm } from "components/cycles"; // helper import { getDateRangeStatus } from "helpers/date-time.helper"; // types import type { CycleDateCheckData, ICycle, IProject, IUser } from "types"; // fetch keys import { COMPLETED_CYCLES_LIST, CURRENT_CYCLE_LIST, CYCLES_LIST, DRAFT_CYCLES_LIST, INCOMPLETE_CYCLES_LIST, PROJECT_DETAILS, UPCOMING_CYCLES_LIST, } from "constants/fetch-keys"; type CycleModalProps = { isOpen: boolean; handleClose: () => void; data?: ICycle | null; workspaceSlug: string; projectId: string; }; // services const cycleService = new CycleService(); export const CreateUpdateCycleModal: React.FC = (props) => { const { isOpen, handleClose, data, workspaceSlug, projectId } = props; const { setToastAlert } = useToast(); const createCycle = async (payload: Partial) => { await cycleService .createCycle(workspaceSlug.toString(), projectId.toString(), payload, {} as IUser) .then((res) => { switch (getDateRangeStatus(res.start_date, res.end_date)) { case "completed": mutate(COMPLETED_CYCLES_LIST(projectId.toString())); break; case "current": mutate(CURRENT_CYCLE_LIST(projectId.toString())); break; case "upcoming": mutate(UPCOMING_CYCLES_LIST(projectId.toString())); break; default: mutate(DRAFT_CYCLES_LIST(projectId.toString())); } mutate(INCOMPLETE_CYCLES_LIST(projectId.toString())); mutate(CYCLES_LIST(projectId.toString())); // update total cycles count in the project details mutate( PROJECT_DETAILS(projectId.toString()), (prevData) => { if (!prevData) return prevData; return { ...prevData, total_cycles: prevData.total_cycles + 1, }; }, false ); setToastAlert({ type: "success", title: "Success!", message: "Cycle created successfully.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Error in creating cycle. Please try again.", }); }); }; const updateCycle = async (cycleId: string, payload: Partial) => { await cycleService .updateCycle(workspaceSlug.toString(), projectId.toString(), cycleId, payload, {} as IUser) .then((res) => { switch (getDateRangeStatus(data?.start_date, data?.end_date)) { case "completed": mutate(COMPLETED_CYCLES_LIST(projectId.toString())); break; case "current": mutate(CURRENT_CYCLE_LIST(projectId.toString())); break; case "upcoming": mutate(UPCOMING_CYCLES_LIST(projectId.toString())); break; default: mutate(DRAFT_CYCLES_LIST(projectId.toString())); } mutate(CYCLES_LIST(projectId.toString())); if (getDateRangeStatus(data?.start_date, data?.end_date) != getDateRangeStatus(res.start_date, res.end_date)) { switch (getDateRangeStatus(res.start_date, res.end_date)) { case "completed": mutate(COMPLETED_CYCLES_LIST(projectId.toString())); break; case "current": mutate(CURRENT_CYCLE_LIST(projectId.toString())); break; case "upcoming": mutate(UPCOMING_CYCLES_LIST(projectId.toString())); break; default: mutate(DRAFT_CYCLES_LIST(projectId.toString())); } } setToastAlert({ type: "success", title: "Success!", message: "Cycle updated successfully.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Error in updating cycle. Please try again.", }); }); }; const dateChecker = async (payload: CycleDateCheckData) => { let status = false; await cycleService.cycleDateCheck(workspaceSlug as string, projectId as string, payload).then((res) => { status = res.status; }); return status; }; const handleFormSubmit = async (formData: Partial) => { if (!workspaceSlug || !projectId) return; const payload: Partial = { ...formData, }; let isDateValid: boolean = true; if (payload.start_date && payload.end_date) { if (data?.start_date && data?.end_date) isDateValid = await dateChecker({ start_date: payload.start_date, end_date: payload.end_date, cycle_id: data.id, }); else isDateValid = await dateChecker({ start_date: payload.start_date, end_date: payload.end_date, }); } if (isDateValid) { if (data) await updateCycle(data.id, payload); else await createCycle(payload); handleClose(); } else setToastAlert({ type: "error", title: "Error!", message: "You already have a cycle on the given dates, if you want to create a draft cycle, remove the dates.", }); }; return (
); };