import { Fragment } from "react"; import { Dialog, Transition } from "@headlessui/react"; import { observer } from "mobx-react-lite"; // components import { CycleForm } from "./form"; // hooks import useToast from "hooks/use-toast"; import { useMobxStore } from "lib/mobx/store-provider"; // types import { CycleDateCheckData, ICycle } from "types"; interface ICycleCreateEdit { cycle?: ICycle | null; modal: boolean; modalClose: () => void; onSubmit?: () => void; workspaceSlug: string; projectId: string; } export const CycleCreateEditModal: React.FC = observer((props) => { const { modal, modalClose, cycle = null, onSubmit, workspaceSlug, projectId } = props; const { cycle: cycleStore } = useMobxStore(); const { setToastAlert } = useToast(); const validateCycleDate = async (payload: CycleDateCheckData) => { let status = false; await cycleStore.validateDate(workspaceSlug as string, projectId as string, payload).then((res) => { status = res.status; }); return status; }; const formSubmit = async (data: Partial) => { let isDateValid: boolean = true; if (data?.start_date && data?.end_date) { if (cycle?.id && cycle?.start_date && cycle?.end_date) isDateValid = await validateCycleDate({ start_date: data.start_date, end_date: data.end_date, cycle_id: cycle.id, }); else isDateValid = await validateCycleDate({ start_date: data.start_date, end_date: data.end_date, }); } if (isDateValid) if (cycle) { try { await cycleStore.updateCycle(workspaceSlug, projectId, cycle.id, data); if (modalClose) modalClose(); if (onSubmit) onSubmit(); setToastAlert({ type: "success", title: "Success!", message: "Cycle updated successfully.", }); } catch (error) { console.log("error", error); setToastAlert({ type: "error", title: "Warning!", message: "Something went wrong please try again later.", }); } } else { try { await cycleStore.createCycle(workspaceSlug, projectId, data); if (modalClose) modalClose(); if (onSubmit) onSubmit(); setToastAlert({ type: "success", title: "Success!", message: "Cycle created successfully.", }); } catch (error) { console.log("error", error); setToastAlert({ type: "error", title: "Warning!", message: "Something went wrong please try again later.", }); } } 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 (
); });