2024-04-15 14:15:47 +00:00
|
|
|
import { FC, FormEvent, useCallback, useRef, useState } from "react";
|
2024-04-15 07:19:14 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { useRouter } from "next/router";
|
2024-05-07 07:14:36 +00:00
|
|
|
// editor
|
2024-04-15 07:19:14 +00:00
|
|
|
import { EditorRefApi } from "@plane/rich-text-editor";
|
2024-05-07 07:14:36 +00:00
|
|
|
// types
|
2024-04-15 07:19:14 +00:00
|
|
|
import { TIssue } from "@plane/types";
|
|
|
|
import { Button, ToggleSwitch, TOAST_TYPE, setToast } from "@plane/ui";
|
|
|
|
// components
|
|
|
|
import {
|
|
|
|
InboxIssueTitle,
|
|
|
|
InboxIssueDescription,
|
|
|
|
InboxIssueProperties,
|
|
|
|
} from "@/components/inbox/modals/create-edit-modal";
|
|
|
|
// constants
|
|
|
|
import { ISSUE_CREATED } from "@/constants/event-tracker";
|
|
|
|
// helpers
|
|
|
|
import { renderFormattedPayloadDate } from "@/helpers/date-time.helper";
|
|
|
|
// hooks
|
|
|
|
import { useEventTracker, useProjectInbox, useWorkspace } from "@/hooks/store";
|
2024-05-29 12:55:03 +00:00
|
|
|
import useKeypress from "@/hooks/use-keypress";
|
2024-04-15 07:19:14 +00:00
|
|
|
|
|
|
|
type TInboxIssueCreateRoot = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
handleModalClose: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultIssueData: Partial<TIssue> = {
|
|
|
|
id: undefined,
|
|
|
|
name: "",
|
|
|
|
description_html: "",
|
|
|
|
priority: "none",
|
|
|
|
state_id: "",
|
|
|
|
label_ids: [],
|
|
|
|
assignee_ids: [],
|
|
|
|
start_date: renderFormattedPayloadDate(new Date()),
|
|
|
|
target_date: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
export const InboxIssueCreateRoot: FC<TInboxIssueCreateRoot> = observer((props) => {
|
|
|
|
const { workspaceSlug, projectId, handleModalClose } = props;
|
|
|
|
const router = useRouter();
|
|
|
|
// refs
|
|
|
|
const descriptionEditorRef = useRef<EditorRefApi>(null);
|
2024-05-28 06:18:20 +00:00
|
|
|
const submitBtnRef = useRef<HTMLButtonElement | null>(null);
|
2024-04-15 07:19:14 +00:00
|
|
|
// hooks
|
|
|
|
const { captureIssueEvent } = useEventTracker();
|
|
|
|
const { createInboxIssue } = useProjectInbox();
|
|
|
|
const { getWorkspaceBySlug } = useWorkspace();
|
|
|
|
const workspaceId = getWorkspaceBySlug(workspaceSlug)?.id;
|
|
|
|
// states
|
|
|
|
const [createMore, setCreateMore] = useState<boolean>(false);
|
|
|
|
const [formSubmitting, setFormSubmitting] = useState(false);
|
|
|
|
const [formData, setFormData] = useState<Partial<TIssue>>(defaultIssueData);
|
|
|
|
const handleFormData = useCallback(
|
|
|
|
<T extends keyof Partial<TIssue>>(issueKey: T, issueValue: Partial<TIssue>[T]) => {
|
|
|
|
setFormData({
|
|
|
|
...formData,
|
|
|
|
[issueKey]: issueValue,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[formData]
|
|
|
|
);
|
|
|
|
|
2024-05-29 12:55:03 +00:00
|
|
|
const handleEscKeyDown = (event: KeyboardEvent) => {
|
|
|
|
if (descriptionEditorRef.current?.isEditorReadyToDiscard()) {
|
|
|
|
handleModalClose();
|
|
|
|
} else {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Editor is still processing changes. Please wait before proceeding.",
|
|
|
|
});
|
|
|
|
event.preventDefault(); // Prevent default action if editor is not ready to discard
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useKeypress("Escape", handleEscKeyDown);
|
|
|
|
|
2024-04-15 14:15:47 +00:00
|
|
|
const handleFormSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
|
|
|
event.preventDefault();
|
2024-05-29 12:55:03 +00:00
|
|
|
|
|
|
|
if (descriptionEditorRef.current?.isEditorReadyToDiscard()) {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Editor is still processing changes. Please wait before proceeding.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-15 07:19:14 +00:00
|
|
|
const payload: Partial<TIssue> = {
|
|
|
|
name: formData.name || "",
|
|
|
|
description_html: formData.description_html || "<p></p>",
|
|
|
|
priority: formData.priority || "none",
|
|
|
|
state_id: formData.state_id || "",
|
|
|
|
label_ids: formData.label_ids || [],
|
|
|
|
assignee_ids: formData.assignee_ids || [],
|
|
|
|
target_date: formData.target_date || null,
|
|
|
|
};
|
|
|
|
setFormSubmitting(true);
|
|
|
|
|
|
|
|
await createInboxIssue(workspaceSlug, projectId, payload)
|
|
|
|
.then((res) => {
|
|
|
|
if (!createMore) {
|
|
|
|
router.push(`/${workspaceSlug}/projects/${projectId}/inbox/?currentTab=open&inboxIssueId=${res?.issue?.id}`);
|
|
|
|
handleModalClose();
|
|
|
|
} else {
|
|
|
|
descriptionEditorRef?.current?.clearEditor();
|
|
|
|
setFormData(defaultIssueData);
|
|
|
|
}
|
|
|
|
captureIssueEvent({
|
|
|
|
eventName: ISSUE_CREATED,
|
|
|
|
payload: {
|
|
|
|
...formData,
|
|
|
|
state: "SUCCESS",
|
|
|
|
element: "Inbox page",
|
|
|
|
},
|
|
|
|
path: router.pathname,
|
|
|
|
});
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: `Success!`,
|
2024-04-15 07:19:14 +00:00
|
|
|
message: "Issue created successfully.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
captureIssueEvent({
|
|
|
|
eventName: ISSUE_CREATED,
|
|
|
|
payload: {
|
|
|
|
...formData,
|
|
|
|
state: "FAILED",
|
|
|
|
element: "Inbox page",
|
|
|
|
},
|
|
|
|
path: router.pathname,
|
|
|
|
});
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: `Error!`,
|
2024-04-15 07:19:14 +00:00
|
|
|
message: "Some error occurred. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setFormSubmitting(false);
|
|
|
|
};
|
|
|
|
|
2024-04-24 09:47:50 +00:00
|
|
|
const isTitleLengthMoreThan255Character = formData?.name ? formData.name.length > 255 : false;
|
|
|
|
|
2024-04-15 07:19:14 +00:00
|
|
|
if (!workspaceSlug || !projectId || !workspaceId) return <></>;
|
|
|
|
return (
|
2024-05-07 07:14:36 +00:00
|
|
|
<form onSubmit={handleFormSubmit}>
|
|
|
|
<div className="space-y-5 p-5">
|
|
|
|
<h3 className="text-xl font-medium text-custom-text-200">Create Inbox Issue</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<InboxIssueTitle
|
|
|
|
data={formData}
|
|
|
|
handleData={handleFormData}
|
|
|
|
isTitleLengthMoreThan255Character={isTitleLengthMoreThan255Character}
|
|
|
|
/>
|
|
|
|
<InboxIssueDescription
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
workspaceId={workspaceId}
|
|
|
|
data={formData}
|
|
|
|
handleData={handleFormData}
|
|
|
|
editorRef={descriptionEditorRef}
|
|
|
|
containerClassName="border-[0.5px] border-custom-border-200 py-3 min-h-[150px]"
|
2024-05-28 06:18:20 +00:00
|
|
|
onEnterKeyPress={() => submitBtnRef?.current?.click()}
|
2024-05-07 07:14:36 +00:00
|
|
|
/>
|
|
|
|
<InboxIssueProperties projectId={projectId} data={formData} handleData={handleFormData} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="px-5 py-4 flex items-center justify-between gap-2 border-t-[0.5px] border-custom-border-200">
|
2024-04-30 11:51:24 +00:00
|
|
|
<div
|
2024-05-07 07:14:36 +00:00
|
|
|
className="inline-flex items-center gap-1.5 cursor-pointer"
|
2024-04-30 11:51:24 +00:00
|
|
|
onClick={() => setCreateMore((prevData) => !prevData)}
|
2024-05-07 07:14:36 +00:00
|
|
|
role="button"
|
2024-04-30 11:51:24 +00:00
|
|
|
>
|
|
|
|
<ToggleSwitch value={createMore} onChange={() => {}} size="sm" />
|
2024-04-15 07:19:14 +00:00
|
|
|
<span className="text-xs">Create more</span>
|
|
|
|
</div>
|
2024-05-07 07:14:36 +00:00
|
|
|
<div className="flex items-center gap-3">
|
2024-05-29 12:55:03 +00:00
|
|
|
<Button
|
|
|
|
variant="neutral-primary"
|
|
|
|
size="sm"
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
if (descriptionEditorRef.current?.isEditorReadyToDiscard()) {
|
|
|
|
handleModalClose();
|
|
|
|
} else {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Editor is still processing changes. Please wait before proceeding.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2024-04-15 07:19:14 +00:00
|
|
|
Discard
|
|
|
|
</Button>
|
2024-04-24 09:47:50 +00:00
|
|
|
<Button
|
|
|
|
variant="primary"
|
2024-05-28 06:18:20 +00:00
|
|
|
ref={submitBtnRef}
|
2024-04-24 09:47:50 +00:00
|
|
|
size="sm"
|
|
|
|
type="submit"
|
|
|
|
loading={formSubmitting}
|
|
|
|
disabled={isTitleLengthMoreThan255Character}
|
|
|
|
>
|
2024-05-07 07:14:36 +00:00
|
|
|
{formSubmitting ? "Creating" : "Create Issue"}
|
2024-04-15 07:19:14 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-04-15 14:15:47 +00:00
|
|
|
</form>
|
2024-04-15 07:19:14 +00:00
|
|
|
);
|
|
|
|
});
|