2023-09-01 11:12:30 +00:00
|
|
|
import React, { useRef } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-09-01 11:12:30 +00:00
|
|
|
import { useForm, Controller } from "react-hook-form";
|
2024-04-11 15:58:59 +00:00
|
|
|
// components
|
|
|
|
import { EditorRefApi } from "@plane/lite-text-editor";
|
|
|
|
import { LiteTextEditor } from "@/components/editor/lite-text-editor";
|
|
|
|
// hooks
|
|
|
|
import useToast from "@/hooks/use-toast";
|
2023-09-01 11:12:30 +00:00
|
|
|
// lib
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useMobxStore } from "@/lib/mobx/store-provider";
|
2023-09-01 11:12:30 +00:00
|
|
|
// types
|
2024-04-11 15:58:59 +00:00
|
|
|
import { Comment } from "@/types/issue";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<Comment> = {
|
|
|
|
comment_html: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
disabled?: boolean;
|
|
|
|
};
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
export const AddComment: React.FC<Props> = observer(() => {
|
|
|
|
// const { disabled = false } = props;
|
|
|
|
// refs
|
|
|
|
const editorRef = useRef<EditorRefApi>(null);
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspace_slug, project_slug } = router.query;
|
|
|
|
// store hooks
|
|
|
|
const { project } = useMobxStore();
|
|
|
|
const { user: userStore, issueDetails: issueDetailStore } = useMobxStore();
|
|
|
|
// derived values
|
|
|
|
const workspaceId = project.workspace?.id;
|
|
|
|
const issueId = issueDetailStore.peekId;
|
|
|
|
// form info
|
2023-09-01 11:12:30 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
watch,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
reset,
|
|
|
|
} = useForm<Comment>({ defaultValues });
|
2024-04-11 15:58:59 +00:00
|
|
|
// toast alert
|
2023-09-01 11:12:30 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const onSubmit = async (formData: Comment) => {
|
|
|
|
if (!workspace_slug || !project_slug || !issueId || isSubmitting || !formData.comment_html) return;
|
|
|
|
|
|
|
|
await issueDetailStore
|
2024-04-11 15:58:59 +00:00
|
|
|
.addIssueComment(workspace_slug.toString(), project_slug.toString(), issueId, formData)
|
2023-09-01 11:12:30 +00:00
|
|
|
.then(() => {
|
|
|
|
reset(defaultValues);
|
|
|
|
editorRef.current?.clearEditor();
|
|
|
|
})
|
2024-04-11 15:58:59 +00:00
|
|
|
.catch(() =>
|
2023-09-01 11:12:30 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Comment could not be posted. Please try again.",
|
2024-04-11 15:58:59 +00:00
|
|
|
})
|
|
|
|
);
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="issue-comments-section">
|
|
|
|
<Controller
|
|
|
|
name="comment_html"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextEditor
|
|
|
|
onEnterKeyPress={(e) => userStore.requiredLogin(() => handleSubmit(onSubmit)(e))}
|
|
|
|
workspaceId={workspaceId as string}
|
|
|
|
workspaceSlug={workspace_slug as string}
|
2023-09-01 11:12:30 +00:00
|
|
|
ref={editorRef}
|
2024-04-11 15:58:59 +00:00
|
|
|
initialValue={
|
2023-09-01 11:12:30 +00:00
|
|
|
!value || value === "" || (typeof value === "object" && Object.keys(value).length === 0)
|
|
|
|
? watch("comment_html")
|
|
|
|
: value
|
|
|
|
}
|
2024-04-11 15:58:59 +00:00
|
|
|
onChange={(comment_json, comment_html) => onChange(comment_html)}
|
|
|
|
isSubmitting={isSubmitting}
|
2023-09-01 11:12:30 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|