2023-01-30 14:17:30 +00:00
|
|
|
import { FC, useCallback, useEffect, useMemo } from "react";
|
|
|
|
|
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
|
|
|
|
import { IIssue } 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-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
export const IssueDescriptionForm: FC<IssueDetailsProps> = ({ issue, handleFormSubmit }) => {
|
2023-01-31 12:40:50 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
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-01-26 18:12:20 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2023-01-30 14:17:30 +00:00
|
|
|
<Input
|
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")}
|
|
|
|
autoComplete="off"
|
|
|
|
onChange={(e) => {
|
|
|
|
setValue("name", e.target.value);
|
|
|
|
debounceHandler();
|
|
|
|
}}
|
|
|
|
mode="transparent"
|
|
|
|
className="text-xl font-medium"
|
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-01-26 18:12:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|