plane/web/components/views/modal.tsx

120 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-03-15 05:30:05 +00:00
import React from "react";
import { useRouter } from "next/router";
import { observer } from "mobx-react-lite";
2023-03-15 05:30:05 +00:00
import { Dialog, Transition } from "@headlessui/react";
// mobx store
import { useMobxStore } from "lib/mobx/store-provider";
2023-03-15 05:30:05 +00:00
// hooks
import useToast from "hooks/use-toast";
// components
import { ProjectViewForm } from "components/views";
2023-03-15 05:30:05 +00:00
// types
import { IProjectView } from "types";
2023-03-15 05:30:05 +00:00
type Props = {
data?: IProjectView | null;
2023-03-15 05:30:05 +00:00
isOpen: boolean;
onClose: () => void;
preLoadedData?: Partial<IProjectView> | null;
2023-03-15 05:30:05 +00:00
};
export const CreateUpdateProjectViewModal: React.FC<Props> = observer((props) => {
const { data, isOpen, onClose, preLoadedData } = props;
2023-03-15 05:30:05 +00:00
const router = useRouter();
const { workspaceSlug, projectId } = router.query;
const { projectViews: projectViewsStore } = useMobxStore();
2023-03-15 05:30:05 +00:00
const { setToastAlert } = useToast();
const handleClose = () => {
onClose();
2023-03-15 05:30:05 +00:00
};
const createView = async (formData: IProjectView) => {
if (!workspaceSlug || !projectId) return;
const payload = {
...formData,
};
2023-03-15 05:30:05 +00:00
await projectViewsStore
.createView(workspaceSlug.toString(), projectId.toString(), payload)
.then(() => handleClose())
.catch(() =>
2023-03-15 05:30:05 +00:00
setToastAlert({
type: "error",
title: "Error!",
message: "Something went wrong. Please try again.",
})
);
2023-03-15 05:30:05 +00:00
};
const updateView = async (formData: IProjectView) => {
if (!workspaceSlug || !projectId) return;
const payload = {
...formData,
};
2023-03-15 05:30:05 +00:00
await projectViewsStore
.updateView(workspaceSlug.toString(), projectId.toString(), data?.id as string, payload)
.then(() => handleClose())
.catch(() =>
2023-03-15 05:30:05 +00:00
setToastAlert({
type: "error",
title: "Error!",
message: "Something went wrong. Please try again.",
})
);
2023-03-15 05:30:05 +00:00
};
const handleFormSubmit = async (formData: IProjectView) => {
2023-03-15 05:30:05 +00:00
if (!data) await createView(formData);
else await updateView(formData);
};
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"
>
<div className="fixed inset-0 bg-custom-backdrop bg-opacity-50 transition-opacity" />
2023-03-15 05:30:05 +00:00
</Transition.Child>
<div className="fixed inset-0 z-20 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
<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"
>
<Dialog.Panel className="relative transform rounded-lg border border-custom-border-200 bg-custom-background-100 px-5 py-8 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-2xl sm:p-6">
<ProjectViewForm
2023-03-15 05:30:05 +00:00
data={data}
handleClose={handleClose}
handleFormSubmit={handleFormSubmit}
preLoadedData={preLoadedData}
2023-03-15 05:30:05 +00:00
/>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition.Root>
);
});