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