import { Fragment } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import cycleService from "services/cycles.service"; // hooks import useToast from "hooks/use-toast"; // components import { CycleForm } from "components/cycles"; // types import type { ICycle } from "types"; // fetch keys import { CYCLE_LIST } from "constants/fetch-keys"; type CycleModalProps = { isOpen: boolean; handleClose: () => void; data?: ICycle; }; export const CreateUpdateCycleModal: React.FC = ({ isOpen, handleClose, data, }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const createCycle = async (payload: Partial) => { await cycleService .createCycle(workspaceSlug as string, projectId as string, payload) .then((res) => { mutate(CYCLE_LIST(projectId as string)); handleClose(); setToastAlert({ type: "success", title: "Success!", message: "Cycle created successfully.", }); }) .catch((err) => { setToastAlert({ type: "error", title: "Error!", message: "Error in creating cycle. Please try again.", }); }); }; const updateCycle = async (cycleId: string, payload: Partial) => { await cycleService .updateCycle(workspaceSlug as string, projectId as string, cycleId, payload) .then((res) => { mutate(CYCLE_LIST(projectId as string)); handleClose(); setToastAlert({ type: "success", title: "Success!", message: "Cycle updated successfully.", }); }) .catch((err) => { setToastAlert({ type: "error", title: "Error!", message: "Error in updating cycle. Please try again.", }); }); }; const handleFormSubmit = async (formData: Partial) => { if (!workspaceSlug || !projectId) return; const payload: Partial = { ...formData, }; if (!data) await createCycle(payload); else await updateCycle(data.id, payload); }; return (
); };