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";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// components
|
|
|
|
import { PageForm } from "./page-form";
|
|
|
|
// types
|
2023-11-20 16:01:12 +00:00
|
|
|
import { IPage } from "types";
|
|
|
|
// store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-03-23 05:31:06 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-03-25 18:09:46 +00:00
|
|
|
data?: IPage | null;
|
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) => {
|
|
|
|
const { isOpen, handleClose, data, projectId } = 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;
|
|
|
|
// store
|
|
|
|
const {
|
|
|
|
page: { createPage, updatePage },
|
2023-12-06 11:45:14 +00:00
|
|
|
trackEvent: { postHogEventTracker },
|
|
|
|
workspace: { currentWorkspace }
|
2023-11-20 16:01:12 +00:00
|
|
|
} = useMobxStore();
|
2023-03-23 05:31:06 +00:00
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const onClose = () => {
|
|
|
|
handleClose();
|
|
|
|
};
|
|
|
|
|
2023-11-20 16:01:12 +00:00
|
|
|
const createProjectPage = async (payload: IPage) => {
|
|
|
|
if (!workspaceSlug) return;
|
2023-03-28 18:50:00 +00:00
|
|
|
|
2023-11-21 10:17:34 +00:00
|
|
|
await createPage(workspaceSlug.toString(), projectId, payload)
|
2023-11-20 16:01:12 +00:00
|
|
|
.then((res) => {
|
2023-03-30 21:21:39 +00:00
|
|
|
router.push(`/${workspaceSlug}/projects/${projectId}/pages/${res.id}`);
|
2023-11-20 16:01:12 +00:00
|
|
|
onClose();
|
2023-03-23 05:31:06 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Page created successfully.",
|
|
|
|
});
|
2023-12-06 11:45:14 +00:00
|
|
|
postHogEventTracker(
|
|
|
|
"PAGE_CREATED",
|
|
|
|
{
|
|
|
|
...res,
|
|
|
|
state: "SUCCESS",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isGrouping: true,
|
|
|
|
groupType: "Workspace_metrics",
|
|
|
|
gorupId: currentWorkspace?.id!
|
|
|
|
}
|
|
|
|
);
|
2023-03-23 05:31:06 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be created. Please try again.",
|
|
|
|
});
|
2023-12-06 11:45:14 +00:00
|
|
|
postHogEventTracker("PAGE_CREATED",
|
|
|
|
{
|
|
|
|
state: "FAILED",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isGrouping: true,
|
|
|
|
groupType: "Workspace_metrics",
|
|
|
|
gorupId: currentWorkspace?.id!
|
|
|
|
}
|
|
|
|
);
|
2023-03-23 05:31:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-20 16:01:12 +00:00
|
|
|
const updateProjectPage = async (payload: IPage) => {
|
|
|
|
if (!data || !workspaceSlug) return;
|
2023-03-27 17:49:05 +00:00
|
|
|
|
2023-11-21 10:17:34 +00:00
|
|
|
await updatePage(workspaceSlug.toString(), projectId, data.id, payload)
|
2023-11-20 16:01:12 +00:00
|
|
|
.then((res) => {
|
2023-03-23 05:31:06 +00:00
|
|
|
onClose();
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Page updated successfully.",
|
|
|
|
});
|
2023-12-06 11:45:14 +00:00
|
|
|
postHogEventTracker("PAGE_UPDATED",
|
|
|
|
{
|
|
|
|
...res,
|
|
|
|
state: "SUCCESS",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isGrouping: true,
|
|
|
|
groupType: "Workspace_metrics",
|
|
|
|
gorupId: currentWorkspace?.id!
|
|
|
|
}
|
|
|
|
);
|
2023-03-23 05:31:06 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be updated. Please try again.",
|
|
|
|
});
|
2023-12-06 11:45:14 +00:00
|
|
|
postHogEventTracker("PAGE_UPDATED",
|
|
|
|
{
|
|
|
|
state: "FAILED",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isGrouping: true,
|
|
|
|
groupType: "Workspace_metrics",
|
|
|
|
gorupId: currentWorkspace?.id!
|
|
|
|
}
|
|
|
|
);
|
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;
|
|
|
|
|
2023-11-20 16:01:12 +00:00
|
|
|
if (!data) await createProjectPage(formData);
|
|
|
|
else await updateProjectPage(formData);
|
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-11-20 16:01:12 +00:00
|
|
|
<div className="flex justify-center text-center p-4 sm:p-0 my-10 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"
|
|
|
|
>
|
2023-11-20 16:01:12 +00:00
|
|
|
<Dialog.Panel className="relative transform rounded-lg bg-custom-background-100 p-5 text-left shadow-custom-shadow-md transition-all px-4 sm:w-full sm:max-w-2xl">
|
|
|
|
<PageForm handleFormSubmit={handleFormSubmit} handleClose={handleClose} data={data} />
|
2023-03-23 05:31:06 +00:00
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
);
|
|
|
|
};
|