import { Fragment } from "react"; import { mutate } from "swr"; import { Dialog, Transition } from "@headlessui/react"; // services import cycleService from "services/cycles.service"; // components import { CycleForm } from "components/cycles"; // helpers import { renderDateFormat } from "helpers/date-time.helper"; // 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 = (props) => { const { isOpen, handleClose, initialData, projectId, workspaceSlug } = props; const createCycle = (payload: Partial) => { cycleService .createCycle(workspaceSlug as string, projectId, payload) .then((res) => { mutate(CYCLE_LIST(projectId)); handleClose(); }) .catch((err) => { // TODO: Handle this ERROR. // Object.keys(err).map((key) => { // setError(key as keyof typeof defaultValues, { // message: err[key].join(", "), // }); // }); }); }; const updateCycle = (cycleId: string, payload: Partial) => { cycleService .updateCycle(workspaceSlug, projectId, cycleId, payload) .then((res) => { mutate(CYCLE_LIST(projectId)); handleClose(); }) .catch((err) => { // TODO: Handle this ERROR. // Object.keys(err).map((key) => { // setError(key as keyof typeof defaultValues, { // message: err[key].join(", "), // }); // }); }); }; const handleFormSubmit = (formValues: Partial) => { if (workspaceSlug && projectId) { const payload = { ...formValues, start_date: formValues.start_date ? renderDateFormat(formValues.start_date) : null, end_date: formValues.end_date ? renderDateFormat(formValues.end_date) : null, }; if (initialData) { updateCycle(initialData.id, payload); } else { createCycle(payload); } } }; return (
{initialData ? "Update" : "Create"} Cycle
); };