import { Fragment } from "react"; 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"; export interface CycleModalProps { isOpen: boolean; handleClose: () => void; projectId: string; workspaceSlug: string; initialData?: ICycle; } export const CycleModal: React.FC = ({ isOpen, handleClose, initialData, projectId, workspaceSlug, }) => { const { setToastAlert } = useToast(); const createCycle = (payload: Partial) => { cycleService .createCycle(workspaceSlug as string, projectId, payload) .then((res) => { mutate(CYCLE_LIST(projectId)); handleClose(); }) .catch((err) => { setToastAlert({ type: "error", title: "Error", message: "Error in creating cycle. Please try again!", }); }); }; const updateCycle = (cycleId: string, payload: Partial) => { cycleService .updateCycle(workspaceSlug, projectId, cycleId, payload) .then((res) => { mutate(CYCLE_LIST(projectId)); handleClose(); }) .catch((err) => { setToastAlert({ type: "error", title: "Error", message: "Error in updating cycle. Please try again!", }); }); }; const handleFormSubmit = (formValues: Partial) => { if (workspaceSlug && projectId) { const payload = { ...formValues, }; if (initialData) { updateCycle(initialData.id, payload); } else { createCycle(payload); } } }; return (
{initialData ? "Update" : "Create"} Cycle
); };