import React, { FC } from "react"; import { useRouter } from "next/router"; import { Dialog, Transition } from "@headlessui/react"; // components import { PageForm } from "./page-form"; // hooks import { useEventTracker } from "hooks/store"; // types import { IPage } from "@plane/types"; import { useProjectPages } from "hooks/store/use-project-page"; import { IPageStore } from "store/page.store"; // constants import { PAGE_CREATED, PAGE_UPDATED } from "constants/event-tracker"; 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", }, }); console.log("Page updated successfully", pageStore); } else { await createProjectPage(formData); } handleClose(); } catch (error) { console.log(error); } }; return (
); };