2024-01-04 10:59:18 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
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";
|
|
|
|
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-03-06 08:48:41 +00:00
|
|
|
// ui
|
2024-01-04 10:59:18 +00:00
|
|
|
// components
|
|
|
|
// types
|
|
|
|
|
|
|
|
export interface DraftIssueProps {
|
|
|
|
changesMade: Partial<TIssue> | null;
|
|
|
|
data?: Partial<TIssue>;
|
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,
|
|
|
|
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 = () => {
|
|
|
|
if (changesMade) setIssueDiscardModal(true);
|
|
|
|
else onClose(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
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}
|
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|