2024-01-18 09:12:10 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2024-02-05 07:49:07 +00:00
|
|
|
import { useRouter } from "next/router";
|
2024-01-04 10:59:18 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
|
|
|
// hooks
|
2024-02-05 07:49:07 +00:00
|
|
|
import { useApplication, useEventTracker, useCycle, useIssues, useModule, useProject, useWorkspace } from "hooks/store";
|
2024-01-04 10:59:18 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
import useLocalStorage from "hooks/use-local-storage";
|
|
|
|
// components
|
|
|
|
import { DraftIssueLayout } from "./draft-issue-layout";
|
|
|
|
import { IssueFormRoot } from "./form";
|
|
|
|
// types
|
|
|
|
import type { TIssue } from "@plane/types";
|
|
|
|
// constants
|
2024-01-18 09:12:10 +00:00
|
|
|
import { EIssuesStoreType, TCreateModalStoreTypes } from "constants/issue";
|
2024-02-09 10:52:08 +00:00
|
|
|
import { ISSUE_CREATED, ISSUE_UPDATED } from "constants/event-tracker";
|
|
|
|
|
2024-01-04 10:59:18 +00:00
|
|
|
export interface IssuesModalProps {
|
|
|
|
data?: Partial<TIssue>;
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
2024-01-16 07:16:03 +00:00
|
|
|
onSubmit?: (res: TIssue) => Promise<void>;
|
2024-01-04 10:59:18 +00:00
|
|
|
withDraftIssueWrapper?: boolean;
|
2024-01-18 09:12:10 +00:00
|
|
|
storeType?: TCreateModalStoreTypes;
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft?: boolean;
|
2024-01-04 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const CreateUpdateIssueModal: React.FC<IssuesModalProps> = observer((props) => {
|
2024-02-07 15:15:05 +00:00
|
|
|
const {
|
|
|
|
data,
|
|
|
|
isOpen,
|
|
|
|
onClose,
|
|
|
|
onSubmit,
|
|
|
|
withDraftIssueWrapper = true,
|
|
|
|
storeType = EIssuesStoreType.PROJECT,
|
|
|
|
isDraft = false,
|
|
|
|
} = props;
|
2024-01-04 10:59:18 +00:00
|
|
|
// states
|
|
|
|
const [changesMade, setChangesMade] = useState<Partial<TIssue> | null>(null);
|
2024-01-11 15:31:05 +00:00
|
|
|
const [createMore, setCreateMore] = useState(false);
|
2024-01-18 09:12:10 +00:00
|
|
|
const [activeProjectId, setActiveProjectId] = useState<string | null>(null);
|
2024-01-04 10:59:18 +00:00
|
|
|
// store hooks
|
2024-02-05 07:49:07 +00:00
|
|
|
const { captureIssueEvent } = useEventTracker();
|
2024-01-04 10:59:18 +00:00
|
|
|
const {
|
2024-01-18 09:12:10 +00:00
|
|
|
router: { workspaceSlug, projectId, cycleId, moduleId, viewId: projectViewId },
|
|
|
|
} = useApplication();
|
|
|
|
const { currentWorkspace } = useWorkspace();
|
|
|
|
const { workspaceProjectIds } = useProject();
|
|
|
|
const { fetchCycleDetails } = useCycle();
|
|
|
|
const { fetchModuleDetails } = useModule();
|
|
|
|
const { issues: projectIssues } = useIssues(EIssuesStoreType.PROJECT);
|
|
|
|
const { issues: moduleIssues } = useIssues(EIssuesStoreType.MODULE);
|
|
|
|
const { issues: cycleIssues } = useIssues(EIssuesStoreType.CYCLE);
|
|
|
|
const { issues: viewIssues } = useIssues(EIssuesStoreType.PROJECT_VIEW);
|
|
|
|
const { issues: profileIssues } = useIssues(EIssuesStoreType.PROFILE);
|
2024-02-07 15:15:05 +00:00
|
|
|
const { issues: draftIssueStore } = useIssues(EIssuesStoreType.DRAFT);
|
2024-01-18 09:12:10 +00:00
|
|
|
// store mapping based on current store
|
|
|
|
const issueStores = {
|
|
|
|
[EIssuesStoreType.PROJECT]: {
|
|
|
|
store: projectIssues,
|
|
|
|
viewId: undefined,
|
|
|
|
},
|
|
|
|
[EIssuesStoreType.PROJECT_VIEW]: {
|
|
|
|
store: viewIssues,
|
|
|
|
viewId: projectViewId,
|
|
|
|
},
|
|
|
|
[EIssuesStoreType.PROFILE]: {
|
|
|
|
store: profileIssues,
|
|
|
|
viewId: undefined,
|
|
|
|
},
|
|
|
|
[EIssuesStoreType.CYCLE]: {
|
|
|
|
store: cycleIssues,
|
|
|
|
viewId: cycleId,
|
|
|
|
},
|
|
|
|
[EIssuesStoreType.MODULE]: {
|
|
|
|
store: moduleIssues,
|
|
|
|
viewId: moduleId,
|
|
|
|
},
|
|
|
|
};
|
2024-02-05 07:49:07 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2024-01-04 10:59:18 +00:00
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
// local storage
|
|
|
|
const { setValue: setLocalStorageDraftIssue } = useLocalStorage<any>("draftedIssue", {});
|
2024-01-18 09:12:10 +00:00
|
|
|
// current store details
|
2024-02-02 08:59:59 +00:00
|
|
|
const { store: currentIssueStore, viewId } = issueStores[storeType];
|
2024-01-18 09:12:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// if modal is closed, reset active project to null
|
|
|
|
// and return to avoid activeProjectId being set to some other project
|
|
|
|
if (!isOpen) {
|
|
|
|
setActiveProjectId(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_id) {
|
|
|
|
setActiveProjectId(data.project_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if data is not present, set active project to the project
|
|
|
|
// in the url. This has the least priority.
|
|
|
|
if (workspaceProjectIds && workspaceProjectIds.length > 0 && !activeProjectId)
|
|
|
|
setActiveProjectId(projectId ?? workspaceProjectIds?.[0]);
|
|
|
|
}, [data, projectId, workspaceProjectIds, isOpen, activeProjectId]);
|
|
|
|
|
|
|
|
const addIssueToCycle = async (issue: TIssue, cycleId: string) => {
|
|
|
|
if (!workspaceSlug || !activeProjectId) return;
|
|
|
|
|
|
|
|
await cycleIssues.addIssueToCycle(workspaceSlug, issue.project_id, cycleId, [issue.id]);
|
|
|
|
fetchCycleDetails(workspaceSlug, activeProjectId, cycleId);
|
|
|
|
};
|
|
|
|
|
2024-01-30 09:53:20 +00:00
|
|
|
const addIssueToModule = async (issue: TIssue, moduleIds: string[]) => {
|
2024-01-18 09:12:10 +00:00
|
|
|
if (!workspaceSlug || !activeProjectId) return;
|
|
|
|
|
2024-01-30 09:53:20 +00:00
|
|
|
await moduleIssues.addModulesToIssue(workspaceSlug, activeProjectId, issue.id, moduleIds);
|
|
|
|
moduleIds.forEach((moduleId) => fetchModuleDetails(workspaceSlug, activeProjectId, moduleId));
|
2024-01-18 09:12:10 +00:00
|
|
|
};
|
2024-01-04 10:59:18 +00:00
|
|
|
|
2024-01-11 15:31:05 +00:00
|
|
|
const handleCreateMoreToggleChange = (value: boolean) => {
|
|
|
|
setCreateMore(value);
|
|
|
|
};
|
|
|
|
|
2024-01-04 10:59:18 +00:00
|
|
|
const handleClose = (saveDraftIssueInLocalStorage?: boolean) => {
|
|
|
|
if (changesMade && saveDraftIssueInLocalStorage) {
|
|
|
|
const draftIssue = JSON.stringify(changesMade);
|
|
|
|
setLocalStorageDraftIssue(draftIssue);
|
|
|
|
}
|
2024-01-18 09:12:10 +00:00
|
|
|
setActiveProjectId(null);
|
2024-01-04 10:59:18 +00:00
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
2024-02-07 15:15:05 +00:00
|
|
|
const handleCreateIssue = async (
|
|
|
|
payload: Partial<TIssue>,
|
|
|
|
is_draft_issue: boolean = false
|
|
|
|
): Promise<TIssue | undefined> => {
|
2024-02-02 08:59:59 +00:00
|
|
|
if (!workspaceSlug || !payload.project_id) return;
|
2024-01-04 10:59:18 +00:00
|
|
|
|
2024-01-16 07:16:03 +00:00
|
|
|
try {
|
2024-02-07 15:15:05 +00:00
|
|
|
const response = is_draft_issue
|
|
|
|
? await draftIssueStore.createIssue(workspaceSlug, payload.project_id, payload)
|
|
|
|
: await currentIssueStore.createIssue(workspaceSlug, payload.project_id, payload, viewId);
|
2024-01-18 09:12:10 +00:00
|
|
|
if (!response) throw new Error();
|
|
|
|
|
2024-02-02 08:59:59 +00:00
|
|
|
currentIssueStore.fetchIssues(workspaceSlug, payload.project_id, "mutation", viewId);
|
2024-01-18 09:12:10 +00:00
|
|
|
|
|
|
|
if (payload.cycle_id && payload.cycle_id !== "" && storeType !== EIssuesStoreType.CYCLE)
|
|
|
|
await addIssueToCycle(response, payload.cycle_id);
|
2024-01-30 09:53:20 +00:00
|
|
|
if (payload.module_ids && payload.module_ids.length > 0 && storeType !== EIssuesStoreType.MODULE)
|
|
|
|
await addIssueToModule(response, payload.module_ids);
|
2024-01-18 09:12:10 +00:00
|
|
|
|
2024-01-16 07:16:03 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Issue created successfully.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_CREATED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: { ...response, state: "SUCCESS" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-16 07:16:03 +00:00
|
|
|
!createMore && handleClose();
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Issue could not be created. Please try again.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_CREATED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: { ...payload, state: "FAILED" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-16 07:16:03 +00:00
|
|
|
}
|
2024-01-04 10:59:18 +00:00
|
|
|
};
|
|
|
|
|
2024-01-16 07:16:03 +00:00
|
|
|
const handleUpdateIssue = async (payload: Partial<TIssue>): Promise<TIssue | undefined> => {
|
2024-02-02 08:59:59 +00:00
|
|
|
if (!workspaceSlug || !payload.project_id || !data?.id) return;
|
2024-01-04 10:59:18 +00:00
|
|
|
|
2024-01-16 07:16:03 +00:00
|
|
|
try {
|
2024-02-02 08:59:59 +00:00
|
|
|
const response = await currentIssueStore.updateIssue(workspaceSlug, payload.project_id, data.id, payload, viewId);
|
2024-01-16 07:16:03 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Issue updated successfully.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_UPDATED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: { ...response, state: "SUCCESS" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-16 07:16:03 +00:00
|
|
|
handleClose();
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Issue could not be created. Please try again.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_UPDATED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: { ...payload, state: "FAILED" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-16 07:16:03 +00:00
|
|
|
}
|
2024-01-04 10:59:18 +00:00
|
|
|
};
|
|
|
|
|
2024-02-07 15:15:05 +00:00
|
|
|
const handleFormSubmit = async (formData: Partial<TIssue>, is_draft_issue: boolean = false) => {
|
2024-02-02 08:59:59 +00:00
|
|
|
if (!workspaceSlug || !formData.project_id || !storeType) return;
|
2024-01-04 10:59:18 +00:00
|
|
|
|
|
|
|
const payload: Partial<TIssue> = {
|
|
|
|
...formData,
|
|
|
|
description_html: formData.description_html ?? "<p></p>",
|
|
|
|
};
|
|
|
|
|
2024-01-18 09:12:10 +00:00
|
|
|
let response: TIssue | undefined = undefined;
|
2024-02-07 15:15:05 +00:00
|
|
|
if (!data?.id) response = await handleCreateIssue(payload, is_draft_issue);
|
2024-01-18 09:12:10 +00:00
|
|
|
else response = await handleUpdateIssue(payload);
|
2024-01-04 10:59:18 +00:00
|
|
|
|
2024-01-18 09:12:10 +00:00
|
|
|
if (response != undefined && onSubmit) await onSubmit(response);
|
2024-01-04 10:59:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleFormChange = (formData: Partial<TIssue> | null) => setChangesMade(formData);
|
|
|
|
|
|
|
|
// don't open the modal if there are no projects
|
2024-01-18 09:12:10 +00:00
|
|
|
if (!workspaceProjectIds || workspaceProjectIds.length === 0 || !activeProjectId) return null;
|
2024-01-04 10:59:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Transition.Root show={isOpen} as={React.Fragment}>
|
|
|
|
<Dialog as="div" className="relative z-20" onClose={() => handleClose(true)}>
|
|
|
|
<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" />
|
|
|
|
</Transition.Child>
|
|
|
|
|
|
|
|
<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">
|
|
|
|
<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 p-5 text-left shadow-custom-shadow-md transition-all sm:w-full mx-4 sm:max-w-4xl">
|
|
|
|
{withDraftIssueWrapper ? (
|
|
|
|
<DraftIssueLayout
|
|
|
|
changesMade={changesMade}
|
2024-01-18 09:12:10 +00:00
|
|
|
data={{
|
|
|
|
...data,
|
2024-01-29 08:38:45 +00:00
|
|
|
cycle_id: data?.cycle_id ? data?.cycle_id : cycleId ? cycleId : null,
|
2024-01-30 09:53:20 +00:00
|
|
|
module_ids: data?.module_ids ? data?.module_ids : moduleId ? [moduleId] : null,
|
2024-01-18 09:12:10 +00:00
|
|
|
}}
|
2024-01-04 10:59:18 +00:00
|
|
|
onChange={handleFormChange}
|
|
|
|
onClose={handleClose}
|
|
|
|
onSubmit={handleFormSubmit}
|
2024-01-18 09:12:10 +00:00
|
|
|
projectId={activeProjectId}
|
2024-01-11 15:31:05 +00:00
|
|
|
isCreateMoreToggleEnabled={createMore}
|
|
|
|
onCreateMoreToggleChange={handleCreateMoreToggleChange}
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft={isDraft}
|
2024-01-04 10:59:18 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<IssueFormRoot
|
2024-01-18 09:12:10 +00:00
|
|
|
data={{
|
|
|
|
...data,
|
2024-01-29 08:38:45 +00:00
|
|
|
cycle_id: data?.cycle_id ? data?.cycle_id : cycleId ? cycleId : null,
|
2024-01-30 09:53:20 +00:00
|
|
|
module_ids: data?.module_ids ? data?.module_ids : moduleId ? [moduleId] : null,
|
2024-01-18 09:12:10 +00:00
|
|
|
}}
|
2024-01-04 10:59:18 +00:00
|
|
|
onClose={() => handleClose(false)}
|
2024-01-11 15:31:05 +00:00
|
|
|
isCreateMoreToggleEnabled={createMore}
|
|
|
|
onCreateMoreToggleChange={handleCreateMoreToggleChange}
|
2024-01-04 10:59:18 +00:00
|
|
|
onSubmit={handleFormSubmit}
|
2024-01-18 09:12:10 +00:00
|
|
|
projectId={activeProjectId}
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft={isDraft}
|
2024-01-04 10:59:18 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
);
|
|
|
|
});
|