2023-11-20 16:01:12 +00:00
|
|
|
import React, { FC } from "react";
|
2023-03-23 05:31:06 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IPage } from "@plane/types";
|
2023-03-23 05:31:06 +00:00
|
|
|
// components
|
2024-03-19 14:38:35 +00:00
|
|
|
import { PAGE_CREATED, PAGE_UPDATED } from "@/constants/event-tracker";
|
|
|
|
import { useEventTracker } from "@/hooks/store";
|
2024-03-06 13:09:14 +00:00
|
|
|
// hooks
|
2023-03-23 05:31:06 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useProjectPages } from "@/hooks/store/use-project-page";
|
|
|
|
import { IPageStore } from "@/store/page.store";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { PageForm } from "./page-form";
|
2024-02-09 10:52:08 +00:00
|
|
|
// constants
|
2023-03-23 05:31:06 +00:00
|
|
|
|
|
|
|
type Props = {
|
2024-01-19 09:48:47 +00:00
|
|
|
// data?: IPage | null;
|
|
|
|
pageStore?: IPageStore;
|
2023-11-20 16:01:12 +00:00
|
|
|
handleClose: () => void;
|
|
|
|
isOpen: boolean;
|
2023-10-17 15:04:16 +00:00
|
|
|
projectId: string;
|
2023-03-23 05:31:06 +00:00
|
|
|
};
|
|
|
|
|
2023-11-20 16:01:12 +00:00
|
|
|
export const CreateUpdatePageModal: FC<Props> = (props) => {
|
2024-01-19 09:48:47 +00:00
|
|
|
const { isOpen, handleClose, projectId, pageStore } = props;
|
2023-10-17 15:04:16 +00:00
|
|
|
// router
|
2023-03-23 05:31:06 +00:00
|
|
|
const router = useRouter();
|
2023-11-20 16:01:12 +00:00
|
|
|
const { workspaceSlug } = router.query;
|
2024-02-09 10:52:08 +00:00
|
|
|
// store hooks
|
2024-01-19 09:48:47 +00:00
|
|
|
const { createPage } = useProjectPages();
|
2024-02-09 10:52:08 +00:00
|
|
|
const { capturePageEvent } = useEventTracker();
|
2023-03-23 05:31:06 +00:00
|
|
|
|
2023-11-20 16:01:12 +00:00
|
|
|
const createProjectPage = async (payload: IPage) => {
|
|
|
|
if (!workspaceSlug) return;
|
2024-02-09 10:52:08 +00:00
|
|
|
await createPage(workspaceSlug.toString(), projectId, payload)
|
|
|
|
.then((res) => {
|
|
|
|
capturePageEvent({
|
|
|
|
eventName: PAGE_CREATED,
|
|
|
|
payload: {
|
|
|
|
...res,
|
|
|
|
state: "SUCCESS",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
capturePageEvent({
|
|
|
|
eventName: PAGE_CREATED,
|
|
|
|
payload: {
|
|
|
|
state: "FAILED",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-03-23 05:31:06 +00:00
|
|
|
};
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
const handleFormSubmit = async (formData: IPage) => {
|
2023-03-23 05:31:06 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
2024-01-19 09:48:47 +00:00
|
|
|
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();
|
|
|
|
}
|
2024-02-09 10:52:08 +00:00
|
|
|
capturePageEvent({
|
|
|
|
eventName: PAGE_UPDATED,
|
|
|
|
payload: {
|
|
|
|
...pageStore,
|
|
|
|
state: "SUCCESS",
|
|
|
|
},
|
|
|
|
});
|
2024-01-19 09:48:47 +00:00
|
|
|
} else {
|
|
|
|
await createProjectPage(formData);
|
|
|
|
}
|
|
|
|
handleClose();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2023-03-23 05:31:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transition.Root show={isOpen} as={React.Fragment}>
|
|
|
|
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
|
|
|
<Transition.Child
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2023-11-10 10:18:34 +00:00
|
|
|
<div className="fixed inset-0 bg-custom-backdrop transition-opacity" />
|
2023-03-23 05:31:06 +00:00
|
|
|
</Transition.Child>
|
|
|
|
|
|
|
|
<div className="fixed inset-0 z-20 overflow-y-auto">
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="my-10 flex justify-center p-4 text-center sm:p-0 md:my-20">
|
2023-03-23 05:31:06 +00:00
|
|
|
<Transition.Child
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
>
|
2024-02-12 11:31:58 +00:00
|
|
|
<Dialog.Panel className="relative transform rounded-lg bg-custom-background-100 p-5 px-4 text-left shadow-custom-shadow-md transition-all w-full sm:max-w-2xl">
|
2024-01-19 09:48:47 +00:00
|
|
|
<PageForm handleFormSubmit={handleFormSubmit} handleClose={handleClose} pageStore={pageStore} />
|
2023-03-23 05:31:06 +00:00
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
);
|
|
|
|
};
|