import { Fragment, useEffect, useState } 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 [activeProject, setActiveProject] = useState(null); const { project: projectStore, cycle: cycleStore } = useMobxStore(); const projects = workspaceSlug ? projectStore.projects[workspaceSlug.toString()] : undefined; 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.", }); }; useEffect(() => { // if modal is closed, reset active project to null // and return to avoid activeProject being set to some other project if (!modal) { setActiveProject(null); return; } // if data is present, set active project to the project of the // issue. This has more priority than the project in the url. if (cycle && cycle.project) { setActiveProject(cycle.project); return; } // if data is not present, set active project to the project // in the url. This has the least priority. if (projects && projects.length > 0 && !activeProject) setActiveProject(projects?.find((p) => p.id === projectId)?.id ?? projects?.[0].id ?? null); }, [activeProject, cycle, projectId, projects, modal]); return (
); });