2024-03-20 12:38:21 +00:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2024-01-04 10:59:18 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { useRouter } from "next/router";
|
2024-05-07 07:14:36 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import type { TIssue } from "@plane/types";
|
2024-05-07 07:14:36 +00:00
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
2024-05-07 07:14:36 +00:00
|
|
|
// components
|
|
|
|
import { EModalPosition, EModalWidth, ModalCore } from "@/components/core";
|
|
|
|
// constants
|
2024-03-19 14:38:35 +00:00
|
|
|
import { ISSUE_CREATED, ISSUE_UPDATED } from "@/constants/event-tracker";
|
|
|
|
import { EIssuesStoreType } from "@/constants/issue";
|
2024-05-07 07:14:36 +00:00
|
|
|
// hooks
|
2024-02-27 11:28:46 +00:00
|
|
|
import {
|
|
|
|
useEventTracker,
|
|
|
|
useCycle,
|
|
|
|
useIssues,
|
|
|
|
useModule,
|
|
|
|
useProject,
|
|
|
|
useIssueDetail,
|
2024-05-08 17:31:20 +00:00
|
|
|
useAppRouter,
|
2024-03-19 14:38:35 +00:00
|
|
|
} from "@/hooks/store";
|
|
|
|
import { useIssuesActions } from "@/hooks/use-issues-actions";
|
|
|
|
import useLocalStorage from "@/hooks/use-local-storage";
|
2024-01-04 10:59:18 +00:00
|
|
|
// components
|
|
|
|
import { DraftIssueLayout } from "./draft-issue-layout";
|
|
|
|
import { IssueFormRoot } from "./form";
|
2024-02-09 10:52:08 +00:00
|
|
|
|
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-03-06 15:17:38 +00:00
|
|
|
storeType?: EIssuesStoreType;
|
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-03-20 12:38:21 +00:00
|
|
|
// ref
|
|
|
|
const issueTitleRef = useRef<HTMLInputElement>(null);
|
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-02-27 11:28:46 +00:00
|
|
|
const [description, setDescription] = useState<string | undefined>(undefined);
|
2024-01-04 10:59:18 +00:00
|
|
|
// store hooks
|
2024-02-05 07:49:07 +00:00
|
|
|
const { captureIssueEvent } = useEventTracker();
|
2024-05-08 17:31:20 +00:00
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId } = useAppRouter();
|
2024-01-18 09:12:10 +00:00
|
|
|
const { workspaceProjectIds } = useProject();
|
|
|
|
const { fetchCycleDetails } = useCycle();
|
|
|
|
const { fetchModuleDetails } = useModule();
|
|
|
|
const { issues: moduleIssues } = useIssues(EIssuesStoreType.MODULE);
|
|
|
|
const { issues: cycleIssues } = useIssues(EIssuesStoreType.CYCLE);
|
2024-02-27 11:28:46 +00:00
|
|
|
const { issues: draftIssues } = useIssues(EIssuesStoreType.DRAFT);
|
|
|
|
const { fetchIssue } = useIssueDetail();
|
2024-02-05 07:49:07 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2024-01-04 10:59:18 +00:00
|
|
|
// local storage
|
2024-02-28 09:59:40 +00:00
|
|
|
const { storedValue: localStorageDraftIssues, setValue: setLocalStorageDraftIssue } = useLocalStorage<
|
|
|
|
Record<string, Partial<TIssue>>
|
|
|
|
>("draftedIssue", {});
|
2024-01-18 09:12:10 +00:00
|
|
|
// current store details
|
2024-03-06 15:17:38 +00:00
|
|
|
const { createIssue, updateIssue } = useIssuesActions(storeType);
|
2024-01-18 09:12:10 +00:00
|
|
|
|
2024-02-27 11:28:46 +00:00
|
|
|
const fetchIssueDetail = async (issueId: string | undefined) => {
|
2024-04-04 11:54:16 +00:00
|
|
|
setDescription(undefined);
|
2024-02-27 15:05:32 +00:00
|
|
|
if (!workspaceSlug) return;
|
2024-02-29 14:11:30 +00:00
|
|
|
|
2024-02-27 15:05:32 +00:00
|
|
|
if (!projectId || issueId === undefined) {
|
2024-02-29 14:11:30 +00:00
|
|
|
setDescription(data?.description_html || "<p></p>");
|
2024-02-27 11:28:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const response = await fetchIssue(workspaceSlug, projectId, issueId, isDraft ? "DRAFT" : "DEFAULT");
|
|
|
|
if (response) setDescription(response?.description_html || "<p></p>");
|
|
|
|
};
|
|
|
|
|
2024-01-18 09:12:10 +00:00
|
|
|
useEffect(() => {
|
2024-02-27 11:28:46 +00:00
|
|
|
// fetching issue details
|
|
|
|
if (isOpen) fetchIssueDetail(data?.id);
|
|
|
|
|
2024-01-18 09:12:10 +00:00
|
|
|
// 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]);
|
2024-02-27 11:28:46 +00:00
|
|
|
|
|
|
|
// clearing up the description state when we leave the component
|
|
|
|
return () => setDescription(undefined);
|
2024-04-30 14:09:50 +00:00
|
|
|
}, [data, projectId, isOpen, activeProjectId]);
|
2024-01-18 09:12:10 +00:00
|
|
|
|
|
|
|
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-05-16 11:59:01 +00:00
|
|
|
await moduleIssues.changeModulesInIssue(workspaceSlug, activeProjectId, issue.id, moduleIds, []);
|
2024-01-30 09:53:20 +00:00
|
|
|
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) {
|
2024-02-28 09:59:40 +00:00
|
|
|
// updating the current edited issue data in the local storage
|
|
|
|
let draftIssues = localStorageDraftIssues ? localStorageDraftIssues : {};
|
|
|
|
if (workspaceSlug) {
|
|
|
|
draftIssues = { ...draftIssues, [workspaceSlug]: changesMade };
|
|
|
|
setLocalStorageDraftIssue(draftIssues);
|
|
|
|
}
|
2024-01-04 10:59:18 +00:00
|
|
|
}
|
2024-02-28 09:59:40 +00:00
|
|
|
|
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
|
2024-02-27 11:28:46 +00:00
|
|
|
? await draftIssues.createIssue(workspaceSlug, payload.project_id, payload)
|
2024-03-06 15:17:38 +00:00
|
|
|
: createIssue && (await createIssue(payload.project_id, payload));
|
2024-01-18 09:12:10 +00:00
|
|
|
if (!response) throw new Error();
|
|
|
|
|
|
|
|
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-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-01-16 07:16:03 +00:00
|
|
|
title: "Success!",
|
2024-05-23 08:58:11 +00:00
|
|
|
message: `${is_draft_issue ? "Draft issue" : "Issue"} created successfully.`,
|
2024-01-16 07:16:03 +00:00
|
|
|
});
|
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();
|
2024-05-17 06:05:37 +00:00
|
|
|
if (createMore) issueTitleRef && issueTitleRef?.current?.focus();
|
|
|
|
setDescription("<p></p>");
|
|
|
|
setChangesMade(null);
|
2024-01-16 07:16:03 +00:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-16 07:16:03 +00:00
|
|
|
title: "Error!",
|
2024-05-23 08:58:11 +00:00
|
|
|
message: `${is_draft_issue ? "Draft issue" : "Issue"} could not be created. Please try again.`,
|
2024-01-16 07:16:03 +00:00
|
|
|
});
|
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-27 11:28:46 +00:00
|
|
|
isDraft
|
|
|
|
? await draftIssues.updateIssue(workspaceSlug, payload.project_id, data.id, payload)
|
2024-03-06 15:17:38 +00:00
|
|
|
: updateIssue && (await updateIssue(payload.project_id, data.id, payload));
|
2024-02-27 11:28:46 +00:00
|
|
|
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-01-16 07:16:03 +00:00
|
|
|
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-22 15:28:34 +00:00
|
|
|
payload: { ...payload, issueId: data.id, state: "SUCCESS" },
|
2024-02-05 07:49:07 +00:00
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-16 07:16:03 +00:00
|
|
|
handleClose();
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-16 07:16:03 +00:00
|
|
|
title: "Error!",
|
2024-03-06 15:17:38 +00:00
|
|
|
message: "Issue could not be updated. Please try again.",
|
2024-01-16 07:16:03 +00:00
|
|
|
});
|
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-03-06 15:17:38 +00:00
|
|
|
const handleFormSubmit = async (payload: Partial<TIssue>, is_draft_issue: boolean = false) => {
|
|
|
|
if (!workspaceSlug || !payload.project_id || !storeType) return;
|
2024-01-04 10:59:18 +00:00
|
|
|
|
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 (
|
2024-05-07 07:14:36 +00:00
|
|
|
<ModalCore
|
|
|
|
isOpen={isOpen}
|
|
|
|
handleClose={() => handleClose(true)}
|
|
|
|
position={EModalPosition.TOP}
|
|
|
|
width={EModalWidth.XXXXL}
|
|
|
|
>
|
|
|
|
{withDraftIssueWrapper ? (
|
|
|
|
<DraftIssueLayout
|
|
|
|
changesMade={changesMade}
|
|
|
|
data={{
|
|
|
|
...data,
|
|
|
|
description_html: description,
|
|
|
|
cycle_id: data?.cycle_id ? data?.cycle_id : cycleId ? cycleId : null,
|
|
|
|
module_ids: data?.module_ids ? data?.module_ids : moduleId ? [moduleId] : null,
|
|
|
|
}}
|
|
|
|
issueTitleRef={issueTitleRef}
|
|
|
|
onChange={handleFormChange}
|
|
|
|
onClose={handleClose}
|
|
|
|
onSubmit={handleFormSubmit}
|
|
|
|
projectId={activeProjectId}
|
|
|
|
isCreateMoreToggleEnabled={createMore}
|
|
|
|
onCreateMoreToggleChange={handleCreateMoreToggleChange}
|
|
|
|
isDraft={isDraft}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<IssueFormRoot
|
|
|
|
issueTitleRef={issueTitleRef}
|
|
|
|
data={{
|
|
|
|
...data,
|
|
|
|
description_html: description,
|
|
|
|
cycle_id: data?.cycle_id ? data?.cycle_id : cycleId ? cycleId : null,
|
|
|
|
module_ids: data?.module_ids ? data?.module_ids : moduleId ? [moduleId] : null,
|
|
|
|
}}
|
|
|
|
onClose={() => handleClose(false)}
|
|
|
|
isCreateMoreToggleEnabled={createMore}
|
|
|
|
onCreateMoreToggleChange={handleCreateMoreToggleChange}
|
|
|
|
onSubmit={handleFormSubmit}
|
|
|
|
projectId={activeProjectId}
|
|
|
|
isDraft={isDraft}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ModalCore>
|
2024-01-04 10:59:18 +00:00
|
|
|
);
|
|
|
|
});
|