import React from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import workspaceService from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // components import { WorkspaceViewForm } from "components/workspace/views/form"; // types import { IWorkspaceView } from "types/workspace-views"; // fetch-keys import { WORKSPACE_VIEWS_LIST } from "constants/fetch-keys"; type Props = { isOpen: boolean; handleClose: () => void; data?: IWorkspaceView | null; preLoadedData?: Partial | null; }; export const CreateUpdateWorkspaceViewModal: React.FC = ({ isOpen, handleClose, data, preLoadedData, }) => { const router = useRouter(); const { workspaceSlug } = router.query; const { setToastAlert } = useToast(); const onClose = () => { handleClose(); }; const createView = async (payload: any) => { const payloadData = { ...payload, query_data: { filters: payload.query, }, }; await workspaceService .createView(workspaceSlug as string, payloadData) .then((res) => { mutate(WORKSPACE_VIEWS_LIST(workspaceSlug as string)); handleClose(); router.replace(`/${workspaceSlug}/workspace-views/issues?globalViewId=${res.id}`); setToastAlert({ type: "success", title: "Success!", message: "View created successfully.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "View could not be created. Please try again.", }); }); }; const updateView = async (payload: any) => { const payloadData = { ...payload, query_data: { filters: payload.query, }, }; await workspaceService .updateView(workspaceSlug as string, data?.id ?? "", payloadData) .then((res) => { mutate( WORKSPACE_VIEWS_LIST(workspaceSlug as string), (prevData) => prevData?.map((p) => { if (p.id === res.id) return { ...p, ...payloadData }; return p; }), false ); onClose(); 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: any) => { if (!workspaceSlug) return; if (!data) await createView(formData); else await updateView(formData); }; return (
); };