import React, { useState } from "react"; import { Dialog, Transition } from "@headlessui/react"; // services import { CycleService } from "services/cycle.service"; // hooks import useToast from "hooks/use-toast"; import { useMobxStore } from "lib/mobx/store-provider"; // components import { CycleForm } from "components/cycles"; // types import type { CycleDateCheckData, ICycle } from "types"; type CycleModalProps = { isOpen: boolean; handleClose: () => void; data?: ICycle | null; workspaceSlug: string; projectId: string; }; // services const cycleService = new CycleService(); export const CycleCreateUpdateModal: React.FC = (props) => { const { isOpen, handleClose, data, workspaceSlug, projectId } = props; // store const { cycle: cycleStore, trackEvent: { postHogEventTracker } } = useMobxStore(); // states const [activeProject, setActiveProject] = useState(projectId); // toast const { setToastAlert } = useToast(); const createCycle = async (payload: Partial) => { if (!workspaceSlug || !projectId) return; const selectedProjectId = payload.project ?? projectId.toString(); await cycleStore .createCycle(workspaceSlug, selectedProjectId, payload) .then((res) => { setToastAlert({ type: "success", title: "Success!", message: "Cycle created successfully.", }); postHogEventTracker( "CYCLE_CREATE", { ...res, state: "SUCCESS", } ); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Error in creating cycle. Please try again.", }); postHogEventTracker( "CYCLE_CREATE", { state: "FAILED", } ); }); }; const updateCycle = async (cycleId: string, payload: Partial) => { if (!workspaceSlug || !projectId) return; const selectedProjectId = payload.project ?? projectId.toString(); await cycleStore .patchCycle(workspaceSlug, selectedProjectId, cycleId, payload) .then(() => { 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 (
); };