2023-02-17 13:21:45 +00:00
|
|
|
import { FC, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2023-01-30 14:17:30 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
import dynamic from "next/dynamic";
|
2023-01-30 14:17:30 +00:00
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// lodash
|
|
|
|
import debounce from "lodash.debounce";
|
2023-01-26 18:12:20 +00:00
|
|
|
// components
|
|
|
|
import { Loader, Input } from "components/ui";
|
|
|
|
const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), {
|
|
|
|
ssr: false,
|
|
|
|
loading: () => (
|
|
|
|
<Loader>
|
|
|
|
<Loader.Item height="12rem" width="100%" />
|
|
|
|
</Loader>
|
|
|
|
),
|
|
|
|
});
|
2023-01-30 14:17:30 +00:00
|
|
|
// types
|
2023-02-01 15:03:18 +00:00
|
|
|
import { IIssue, UserAuth } from "types";
|
2023-01-31 12:40:50 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export interface IssueDescriptionFormValues {
|
|
|
|
name: string;
|
|
|
|
description: any;
|
|
|
|
description_html: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IssueDetailsProps {
|
|
|
|
issue: IIssue;
|
2023-01-30 14:17:30 +00:00
|
|
|
handleFormSubmit: (value: IssueDescriptionFormValues) => void;
|
2023-02-01 15:03:18 +00:00
|
|
|
userAuth: UserAuth;
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 15:03:18 +00:00
|
|
|
export const IssueDescriptionForm: FC<IssueDetailsProps> = ({
|
|
|
|
issue,
|
|
|
|
handleFormSubmit,
|
|
|
|
userAuth,
|
|
|
|
}) => {
|
2023-01-31 12:40:50 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-02-17 13:21:45 +00:00
|
|
|
const [issueTitleName, setIssueTitleName] = useState("");
|
|
|
|
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
|
2023-01-31 12:40:50 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
watch,
|
|
|
|
setValue,
|
|
|
|
reset,
|
|
|
|
formState: { errors },
|
|
|
|
setError,
|
|
|
|
} = useForm<IIssue>({
|
2023-01-30 14:17:30 +00:00
|
|
|
defaultValues: {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
description_html: "",
|
|
|
|
},
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
const handleDescriptionFormSubmit = useCallback(
|
|
|
|
(formData: Partial<IIssue>) => {
|
2023-01-31 12:40:50 +00:00
|
|
|
if (!formData.name || formData.name === "") {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error in saving!",
|
|
|
|
message: "Title is required.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formData.name.length > 255) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error in saving!",
|
|
|
|
message: "Title cannot have more than 255 characters.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
handleFormSubmit({
|
|
|
|
name: formData.name ?? "",
|
|
|
|
description: formData.description,
|
|
|
|
description_html: formData.description_html,
|
|
|
|
});
|
|
|
|
},
|
2023-01-31 12:40:50 +00:00
|
|
|
[handleFormSubmit, setToastAlert]
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
const debounceHandler = useMemo(
|
|
|
|
() => debounce(handleSubmit(handleDescriptionFormSubmit), 2000),
|
|
|
|
[handleSubmit, handleDescriptionFormSubmit]
|
|
|
|
);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
debounceHandler.cancel();
|
|
|
|
},
|
|
|
|
[debounceHandler]
|
|
|
|
);
|
|
|
|
|
|
|
|
// reset form values
|
2023-01-30 14:07:25 +00:00
|
|
|
useEffect(() => {
|
2023-01-30 14:17:30 +00:00
|
|
|
if (!issue) return;
|
|
|
|
|
|
|
|
reset(issue);
|
|
|
|
}, [issue, reset]);
|
2023-01-30 14:07:25 +00:00
|
|
|
|
2023-02-01 15:03:18 +00:00
|
|
|
const isNotAllowed = userAuth.isGuest || userAuth.isViewer;
|
|
|
|
|
2023-02-17 13:21:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (textareaRef && textareaRef.current) {
|
|
|
|
textareaRef.current.style.height = "0px";
|
|
|
|
const scrollHeight = textareaRef.current.scrollHeight;
|
|
|
|
textareaRef.current.style.height = scrollHeight + "px";
|
|
|
|
}
|
|
|
|
}, [issueTitleName]);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2023-02-17 13:21:45 +00:00
|
|
|
<textarea
|
2023-01-26 18:12:20 +00:00
|
|
|
id="name"
|
|
|
|
placeholder="Enter issue name"
|
|
|
|
name="name"
|
2023-01-30 14:17:30 +00:00
|
|
|
value={watch("name")}
|
2023-02-17 13:21:45 +00:00
|
|
|
ref={textareaRef}
|
2023-01-30 14:17:30 +00:00
|
|
|
onChange={(e) => {
|
2023-02-17 13:21:45 +00:00
|
|
|
setIssueTitleName(e.target.value);
|
2023-01-30 14:17:30 +00:00
|
|
|
setValue("name", e.target.value);
|
|
|
|
debounceHandler();
|
|
|
|
}}
|
2023-02-17 13:21:45 +00:00
|
|
|
required={true}
|
|
|
|
className="block px-3 py-2 text-xl
|
|
|
|
w-full overflow-hidden resize-none min-h-10
|
|
|
|
rounded border-none bg-transparent ring-0 focus:ring-1 focus:ring-theme outline-none "
|
|
|
|
role="textbox "
|
2023-01-26 18:12:20 +00:00
|
|
|
/>
|
2023-01-31 12:40:50 +00:00
|
|
|
<span>{errors.name ? errors.name.message : null}</span>
|
2023-01-26 18:12:20 +00:00
|
|
|
<RemirrorRichTextEditor
|
2023-01-30 14:17:30 +00:00
|
|
|
value={watch("description")}
|
|
|
|
placeholder="Describe the issue..."
|
|
|
|
onJSONChange={(json) => {
|
|
|
|
setValue("description", json);
|
|
|
|
debounceHandler();
|
|
|
|
}}
|
|
|
|
onHTMLChange={(html) => setValue("description_html", html)}
|
2023-02-01 15:03:18 +00:00
|
|
|
editable={!isNotAllowed}
|
2023-01-26 18:12:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|