import React, { FC } from "react"; import { useRouter } from "next/router"; import { Dialog, Transition } from "@headlessui/react"; import { IPage } from "@plane/types"; // components import { PAGE_CREATED, PAGE_UPDATED } from "@/constants/event-tracker"; import { useEventTracker } from "@/hooks/store"; // hooks // types import { useProjectPages } from "@/hooks/store/use-project-page"; import { IPageStore } from "@/store/page.store"; import { PageForm } from "./page-form"; // constants type Props = { // data?: IPage | null; pageStore?: IPageStore; handleClose: () => void; isOpen: boolean; projectId: string; }; export const CreateUpdatePageModal: FC = (props) => { const { isOpen, handleClose, projectId, pageStore } = props; // router const router = useRouter(); const { workspaceSlug } = router.query; // store hooks const { createPage } = useProjectPages(); const { capturePageEvent } = useEventTracker(); const createProjectPage = async (payload: IPage) => { if (!workspaceSlug) return; await createPage(workspaceSlug.toString(), projectId, payload) .then((res) => { capturePageEvent({ eventName: PAGE_CREATED, payload: { ...res, state: "SUCCESS", }, }); }) .catch(() => { capturePageEvent({ eventName: PAGE_CREATED, payload: { state: "FAILED", }, }); }); }; const handleFormSubmit = async (formData: IPage) => { if (!workspaceSlug || !projectId) return; try { if (pageStore) { if (pageStore.name !== formData.name) { await pageStore.updateName(formData.name); } if (pageStore.access !== formData.access) { formData.access === 1 ? await pageStore.makePrivate() : await pageStore.makePublic(); } capturePageEvent({ eventName: PAGE_UPDATED, payload: { ...pageStore, state: "SUCCESS", }, }); } else { await createProjectPage(formData); } handleClose(); } catch (error) { console.log(error); } }; return (
); };