import React from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { Dialog, Transition } from "@headlessui/react"; // store hooks import { useGlobalView } from "hooks/store"; import useToast from "hooks/use-toast"; // components import { WorkspaceViewForm } from "components/workspace"; // types import { IWorkspaceView } from "@plane/types"; type Props = { data?: IWorkspaceView; isOpen: boolean; onClose: () => void; preLoadedData?: Partial; }; export const CreateUpdateWorkspaceViewModal: React.FC = observer((props) => { const { isOpen, onClose, data, preLoadedData } = props; // router const router = useRouter(); const { workspaceSlug } = router.query; // store hooks const { createGlobalView, updateGlobalView } = useGlobalView(); // toast alert const { setToastAlert } = useToast(); const handleClose = () => { onClose(); }; const handleCreateView = async (payload: Partial) => { if (!workspaceSlug) return; const payloadData: Partial = { ...payload, filters: { ...payload?.filters, }, }; await createGlobalView(workspaceSlug.toString(), payloadData) .then((res) => { setToastAlert({ type: "success", title: "Success!", message: "View created successfully.", }); router.push(`/${workspaceSlug}/workspace-views/${res.id}`); handleClose(); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "View could not be created. Please try again.", }) ); }; const handleUpdateView = async (payload: Partial) => { if (!workspaceSlug || !data) return; const payloadData: Partial = { ...payload, query: { ...payload?.filters, }, }; await updateGlobalView(workspaceSlug.toString(), data.id, payloadData) .then(() => { setToastAlert({ type: "success", title: "Success!", message: "View updated successfully.", }); handleClose(); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "View could not be updated. Please try again.", }) ); }; const handleFormSubmit = async (formData: Partial) => { if (!workspaceSlug) return; if (!data) await handleCreateView(formData); else await handleUpdateView(formData); }; return (
); });