import { FC, Fragment } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { Dialog, Transition } from "@headlessui/react"; // hooks import { useEventTracker, useProjectView } from "hooks/store"; import useToast from "hooks/use-toast"; // components import { ProjectViewForm } from "components/views"; // types import { IProjectView } from "@plane/types"; // constants import { VIEW_CREATED, VIEW_UPDATED } from "constants/event-tracker"; type Props = { data?: IProjectView | null; isOpen: boolean; onClose: () => void; preLoadedData?: Partial | null; workspaceSlug: string; projectId: string; }; export const CreateUpdateProjectViewModal: FC = observer((props) => { const { data, isOpen, onClose, preLoadedData, workspaceSlug, projectId } = props; // router const router = useRouter(); const { cycleId, moduleId, viewId } = router.query; // store hooks const { createView, updateView } = useProjectView(); const { captureEvent, trackElement } = useEventTracker(); // toast alert const { setToastAlert } = useToast(); const handleClose = () => { onClose(); }; const handleCreateView = async (payload: IProjectView) => { await createView(workspaceSlug, projectId, payload) .then((res) => { handleClose(); captureEvent(VIEW_CREATED, { view_id: res.id, filters: res.filters, element_id: cycleId ?? moduleId ?? viewId ?? projectId, element: trackElement ? trackElement : cycleId ? "Cycle issues page" : moduleId ? "Module issues page" : viewId ? "View issues page" : "Project issues page", state: "SUCCESS", }); setToastAlert({ type: "success", title: "Success!", message: "View created successfully.", }); }) .catch(() => { captureEvent(VIEW_CREATED, { state: "FAILED", }); setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again.", }); }); }; const handleUpdateView = async (payload: IProjectView) => { await updateView(workspaceSlug, projectId, data?.id as string, payload) .then((res) => { captureEvent(VIEW_UPDATED, { view_id: res.id, filters: res.filters, element: "Views page", state: "SUCCESS", }); handleClose(); }) .catch((err) => { captureEvent(VIEW_UPDATED, { view_id: data?.id, element: "Views page", state: "FAILED", }); setToastAlert({ type: "error", title: "Error!", message: err.detail ?? "Something went wrong. Please try again.", }); }); }; const handleFormSubmit = async (formData: IProjectView) => { if (!data) await handleCreateView(formData); else await handleUpdateView(formData); }; return (
); });