2024-01-04 10:59:18 +00:00
|
|
|
import React, { useState } from "react";
|
2024-03-21 15:36:55 +00:00
|
|
|
import isEmpty from "lodash/isEmpty";
|
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-03-19 14:38:35 +00:00
|
|
|
import type { TIssue } from "@plane/types";
|
2024-01-04 10:59:18 +00:00
|
|
|
// hooks
|
2024-03-06 13:09:14 +00:00
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { ConfirmIssueDiscard } from "@/components/issues";
|
|
|
|
import { IssueFormRoot } from "@/components/issues/issue-modal/form";
|
2024-03-21 15:36:55 +00:00
|
|
|
import { isEmptyHtmlString } from "@/helpers/string.helper";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useEventTracker } from "@/hooks/store";
|
2024-01-04 10:59:18 +00:00
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IssueDraftService } from "@/services/issue";
|
2024-01-04 10:59:18 +00:00
|
|
|
|
|
|
|
export interface DraftIssueProps {
|
|
|
|
changesMade: Partial<TIssue> | null;
|
|
|
|
data?: Partial<TIssue>;
|
2024-03-20 12:38:21 +00:00
|
|
|
issueTitleRef: React.MutableRefObject<HTMLInputElement | null>;
|
2024-01-11 15:31:05 +00:00
|
|
|
isCreateMoreToggleEnabled: boolean;
|
|
|
|
onCreateMoreToggleChange: (value: boolean) => void;
|
2024-01-04 10:59:18 +00:00
|
|
|
onChange: (formData: Partial<TIssue> | null) => void;
|
|
|
|
onClose: (saveDraftIssueInLocalStorage?: boolean) => void;
|
|
|
|
onSubmit: (formData: Partial<TIssue>) => Promise<void>;
|
|
|
|
projectId: string;
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft: boolean;
|
2024-01-04 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const issueDraftService = new IssueDraftService();
|
|
|
|
|
|
|
|
export const DraftIssueLayout: React.FC<DraftIssueProps> = observer((props) => {
|
2024-01-11 15:31:05 +00:00
|
|
|
const {
|
|
|
|
changesMade,
|
|
|
|
data,
|
2024-03-20 12:38:21 +00:00
|
|
|
issueTitleRef,
|
2024-01-11 15:31:05 +00:00
|
|
|
onChange,
|
|
|
|
onClose,
|
|
|
|
onSubmit,
|
|
|
|
projectId,
|
|
|
|
isCreateMoreToggleEnabled,
|
|
|
|
onCreateMoreToggleChange,
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft,
|
2024-01-11 15:31:05 +00:00
|
|
|
} = props;
|
2024-01-04 10:59:18 +00:00
|
|
|
// states
|
|
|
|
const [issueDiscardModal, setIssueDiscardModal] = useState(false);
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
2024-02-05 07:49:07 +00:00
|
|
|
// store hooks
|
|
|
|
const { captureIssueEvent } = useEventTracker();
|
2024-01-04 10:59:18 +00:00
|
|
|
|
|
|
|
const handleClose = () => {
|
2024-03-22 13:16:38 +00:00
|
|
|
if (data?.id) {
|
2024-03-22 12:58:44 +00:00
|
|
|
onClose(false);
|
|
|
|
setIssueDiscardModal(false);
|
2024-03-22 13:16:38 +00:00
|
|
|
} else {
|
|
|
|
if (changesMade) {
|
|
|
|
Object.entries(changesMade).forEach(([key, value]) => {
|
|
|
|
const issueKey = key as keyof TIssue;
|
|
|
|
if (value === null || value === undefined || value === "") delete changesMade[issueKey];
|
|
|
|
if (typeof value === "object" && isEmpty(value)) delete changesMade[issueKey];
|
|
|
|
if (Array.isArray(value) && value.length === 0) delete changesMade[issueKey];
|
|
|
|
if (issueKey === "project_id") delete changesMade.project_id;
|
|
|
|
if (issueKey === "priority" && value && value === "none") delete changesMade.priority;
|
|
|
|
if (
|
|
|
|
issueKey === "description_html" &&
|
|
|
|
changesMade.description_html &&
|
|
|
|
isEmptyHtmlString(changesMade.description_html)
|
|
|
|
)
|
|
|
|
delete changesMade.description_html;
|
|
|
|
});
|
|
|
|
if (isEmpty(changesMade)) {
|
|
|
|
onClose(false);
|
|
|
|
setIssueDiscardModal(false);
|
|
|
|
} else setIssueDiscardModal(true);
|
|
|
|
} else {
|
|
|
|
onClose(false);
|
|
|
|
setIssueDiscardModal(false);
|
|
|
|
}
|
2024-03-21 15:36:55 +00:00
|
|
|
}
|
2024-01-04 10:59:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleCreateDraftIssue = async () => {
|
|
|
|
if (!changesMade || !workspaceSlug || !projectId) return;
|
|
|
|
|
2024-03-15 12:13:42 +00:00
|
|
|
const payload = {
|
|
|
|
...changesMade,
|
|
|
|
name: changesMade.name?.trim() === "" ? "Untitled" : changesMade.name?.trim(),
|
|
|
|
};
|
2024-01-04 10:59:18 +00:00
|
|
|
|
|
|
|
await issueDraftService
|
|
|
|
.createDraftIssue(workspaceSlug.toString(), projectId.toString(), payload)
|
2024-02-05 07:49:07 +00:00
|
|
|
.then((res) => {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-01-04 10:59:18 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "Draft Issue created successfully.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
|
|
|
eventName: "Draft issue created",
|
|
|
|
payload: { ...res, state: "SUCCESS" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
2024-01-04 10:59:18 +00:00
|
|
|
onChange(null);
|
|
|
|
setIssueDiscardModal(false);
|
|
|
|
onClose(false);
|
|
|
|
})
|
2024-02-05 07:49:07 +00:00
|
|
|
.catch(() => {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-04 10:59:18 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Issue could not be created. Please try again.",
|
2024-02-05 07:49:07 +00:00
|
|
|
});
|
|
|
|
captureIssueEvent({
|
|
|
|
eventName: "Draft issue created",
|
|
|
|
payload: { ...payload, state: "FAILED" },
|
|
|
|
path: router.asPath,
|
|
|
|
});
|
|
|
|
});
|
2024-01-04 10:59:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmIssueDiscard
|
|
|
|
isOpen={issueDiscardModal}
|
|
|
|
handleClose={() => setIssueDiscardModal(false)}
|
|
|
|
onConfirm={handleCreateDraftIssue}
|
|
|
|
onDiscard={() => {
|
|
|
|
onChange(null);
|
|
|
|
setIssueDiscardModal(false);
|
|
|
|
onClose(false);
|
|
|
|
}}
|
|
|
|
/>
|
2024-01-11 15:31:05 +00:00
|
|
|
<IssueFormRoot
|
|
|
|
isCreateMoreToggleEnabled={isCreateMoreToggleEnabled}
|
|
|
|
onCreateMoreToggleChange={onCreateMoreToggleChange}
|
|
|
|
data={data}
|
2024-03-20 12:38:21 +00:00
|
|
|
issueTitleRef={issueTitleRef}
|
2024-01-11 15:31:05 +00:00
|
|
|
onChange={onChange}
|
|
|
|
onClose={handleClose}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
projectId={projectId}
|
2024-02-07 15:15:05 +00:00
|
|
|
isDraft={isDraft}
|
2024-01-11 15:31:05 +00:00
|
|
|
/>
|
2024-01-04 10:59:18 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|