2023-10-19 11:08:36 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2023-10-23 13:47:42 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-02-02 12:34:13 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
2023-10-23 13:47:42 +00:00
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-02-02 12:34:13 +00:00
|
|
|
// components
|
|
|
|
import { ModuleForm } from "components/modules";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// types
|
2023-10-23 13:47:42 +00:00
|
|
|
import type { IModule } from "types";
|
2023-02-02 12:34:13 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
isOpen: boolean;
|
2023-10-17 15:04:16 +00:00
|
|
|
onClose: () => void;
|
2023-02-02 12:34:13 +00:00
|
|
|
data?: IModule;
|
2023-10-17 15:04:16 +00:00
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
2023-02-02 12:34:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IModule> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
2023-05-29 16:46:32 +00:00
|
|
|
status: "backlog",
|
2023-02-02 12:34:13 +00:00
|
|
|
lead: null,
|
2023-10-31 06:48:04 +00:00
|
|
|
members: [],
|
2023-02-02 12:34:13 +00:00
|
|
|
};
|
|
|
|
|
2023-10-23 13:47:42 +00:00
|
|
|
export const CreateUpdateModuleModal: React.FC<Props> = observer((props) => {
|
2023-10-17 15:04:16 +00:00
|
|
|
const { isOpen, onClose, data, workspaceSlug, projectId } = props;
|
2023-02-02 12:34:13 +00:00
|
|
|
|
2023-10-19 11:08:36 +00:00
|
|
|
const [activeProject, setActiveProject] = useState<string | null>(null);
|
|
|
|
|
2023-12-10 10:18:10 +00:00
|
|
|
const {
|
|
|
|
project: projectStore,
|
|
|
|
module: moduleStore,
|
|
|
|
trackEvent: { postHogEventTracker },
|
|
|
|
} = useMobxStore();
|
2023-10-19 11:08:36 +00:00
|
|
|
|
|
|
|
const projects = workspaceSlug ? projectStore.projects[workspaceSlug.toString()] : undefined;
|
|
|
|
|
2023-02-02 12:34:13 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
reset(defaultValues);
|
2023-10-17 15:04:16 +00:00
|
|
|
onClose();
|
2023-02-02 12:34:13 +00:00
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
const { reset } = useForm<IModule>({
|
2023-02-02 12:34:13 +00:00
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
const createModule = async (payload: Partial<IModule>) => {
|
2023-10-23 13:47:42 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-11-22 10:05:24 +00:00
|
|
|
const selectedProjectId = payload.project ?? projectId.toString();
|
2023-10-23 13:47:42 +00:00
|
|
|
await moduleStore
|
2023-11-22 10:05:24 +00:00
|
|
|
.createModule(workspaceSlug.toString(), selectedProjectId, payload)
|
2023-12-06 11:45:14 +00:00
|
|
|
.then((res) => {
|
2023-02-02 12:34:13 +00:00
|
|
|
handleClose();
|
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
2023-02-20 05:53:04 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "Module created successfully.",
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
2023-12-10 10:18:10 +00:00
|
|
|
postHogEventTracker("MODULE_CREATED", {
|
|
|
|
...res,
|
|
|
|
state: "SUCCESS",
|
|
|
|
});
|
2023-02-02 12:34:13 +00:00
|
|
|
})
|
2023-12-11 11:59:43 +00:00
|
|
|
.catch((err) => {
|
2023-02-20 05:53:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
2023-12-11 11:59:43 +00:00
|
|
|
message: err.detail ?? "Module could not be created. Please try again.",
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
2023-12-10 10:18:10 +00:00
|
|
|
postHogEventTracker("MODULE_CREATED", {
|
|
|
|
state: "FAILED",
|
|
|
|
});
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateModule = async (payload: Partial<IModule>) => {
|
2023-10-23 13:47:42 +00:00
|
|
|
if (!workspaceSlug || !projectId || !data) return;
|
2023-11-22 10:05:24 +00:00
|
|
|
const selectedProjectId = payload.project ?? projectId.toString();
|
2023-10-23 13:47:42 +00:00
|
|
|
await moduleStore
|
2023-11-22 10:05:24 +00:00
|
|
|
.updateModuleDetails(workspaceSlug.toString(), selectedProjectId, data.id, payload)
|
2023-12-06 11:45:14 +00:00
|
|
|
.then((res) => {
|
2023-02-02 12:34:13 +00:00
|
|
|
handleClose();
|
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
2023-02-20 05:53:04 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "Module updated successfully.",
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
2023-12-10 10:18:10 +00:00
|
|
|
postHogEventTracker("MODULE_UPDATED", {
|
|
|
|
...res,
|
|
|
|
state: "SUCCESS",
|
|
|
|
});
|
2023-02-02 12:34:13 +00:00
|
|
|
})
|
2023-12-11 11:59:43 +00:00
|
|
|
.catch((err) => {
|
2023-02-20 05:53:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
2023-12-11 11:59:43 +00:00
|
|
|
message: err.detail ?? "Module could not be updated. Please try again.",
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
2023-12-10 10:18:10 +00:00
|
|
|
postHogEventTracker("MODULE_UPDATED", {
|
|
|
|
state: "FAILED",
|
|
|
|
});
|
2023-02-02 12:34:13 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFormSubmit = async (formData: Partial<IModule>) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
const payload: Partial<IModule> = {
|
|
|
|
...formData,
|
|
|
|
};
|
|
|
|
if (!data) await createModule(payload);
|
|
|
|
else await updateModule(payload);
|
|
|
|
};
|
|
|
|
|
2023-10-19 11:08:36 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// if modal is closed, reset active project to null
|
|
|
|
// and return to avoid activeProject being set to some other project
|
|
|
|
if (!isOpen) {
|
|
|
|
setActiveProject(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if data is present, set active project to the project of the
|
|
|
|
// issue. This has more priority than the project in the url.
|
|
|
|
if (data && data.project) {
|
|
|
|
setActiveProject(data.project);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if data is not present, set active project to the project
|
|
|
|
// in the url. This has the least priority.
|
|
|
|
if (projects && projects.length > 0 && !activeProject)
|
|
|
|
setActiveProject(projects?.find((p) => p.id === projectId)?.id ?? projects?.[0].id ?? null);
|
|
|
|
}, [activeProject, data, projectId, projects, isOpen]);
|
|
|
|
|
2023-02-02 12:34:13 +00:00
|
|
|
return (
|
2023-10-19 11:08:36 +00:00
|
|
|
<Transition.Root show={isOpen} as={React.Fragment}>
|
2023-02-02 12:34:13 +00:00
|
|
|
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
|
|
|
<Transition.Child
|
2023-10-19 11:08:36 +00:00
|
|
|
as={React.Fragment}
|
2023-02-02 12:34:13 +00:00
|
|
|
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-02-02 12:34:13 +00:00
|
|
|
</Transition.Child>
|
|
|
|
|
2023-10-19 11:08:36 +00:00
|
|
|
<div className="fixed inset-0 z-10 overflow-y-auto">
|
|
|
|
<div className="my-10 flex items-center justify-center p-4 text-center sm:p-0 md:my-20">
|
2023-02-02 12:34:13 +00:00
|
|
|
<Transition.Child
|
2023-10-19 11:08:36 +00:00
|
|
|
as={React.Fragment}
|
2023-02-02 12:34:13 +00:00
|
|
|
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-10 10:18:34 +00:00
|
|
|
<Dialog.Panel className="relative transform rounded-lg bg-custom-background-100 p-5 text-left shadow-custom-shadow-md transition-all sm:w-full sm:max-w-2xl">
|
2023-02-02 12:34:13 +00:00
|
|
|
<ModuleForm
|
|
|
|
handleFormSubmit={handleFormSubmit}
|
|
|
|
handleClose={handleClose}
|
|
|
|
status={data ? true : false}
|
2023-10-19 11:08:36 +00:00
|
|
|
projectId={activeProject ?? ""}
|
|
|
|
setActiveProject={setActiveProject}
|
2023-02-20 05:53:04 +00:00
|
|
|
data={data}
|
2023-02-02 12:34:13 +00:00
|
|
|
/>
|
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
);
|
2023-10-23 13:47:42 +00:00
|
|
|
});
|