import { FC, useCallback, useEffect, useState } from "react"; import dynamic from "next/dynamic"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // hooks import useReloadConfirmations from "hooks/use-reload-confirmation"; // components import { Loader, TextArea } from "components/ui"; const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), { ssr: false, loading: () => ( ), }); // types import { IIssue } from "types"; export interface IssueDescriptionFormValues { name: string; description: any; description_html: string; } export interface IssueDetailsProps { issue: { name: string; description: string; description_html: string; }; handleFormSubmit: (value: IssueDescriptionFormValues) => Promise; isAllowed: boolean; } export const IssueDescriptionForm: FC = ({ issue, handleFormSubmit, isAllowed, }) => { const [isSubmitting, setIsSubmitting] = useState(false); const [characterLimit, setCharacterLimit] = useState(false); const { setShowAlert } = useReloadConfirmations(); const { handleSubmit, watch, setValue, reset, register, control, formState: { errors }, } = useForm({ defaultValues: { name: "", description: "", description_html: "", }, }); const handleDescriptionFormSubmit = useCallback( async (formData: Partial) => { if (!formData.name || formData.name.length === 0 || formData.name.length > 255) return; await handleFormSubmit({ name: formData.name ?? "", description: formData.description ?? "", description_html: formData.description_html ?? "", }); }, [handleFormSubmit] ); // reset form values useEffect(() => { if (!issue) return; reset({ ...issue, }); }, [issue, reset]); return ( setCharacterLimit(true)} onBlur={() => { setCharacterLimit(false); setIsSubmitting(true); handleSubmit(handleDescriptionFormSubmit)().finally(() => setIsSubmitting(false)); }} required={true} className="min-h-10 block w-full resize-none overflow-hidden rounded border-none bg-transparent px-3 py-2 text-xl outline-none ring-0 focus:ring-1 focus:ring-custom-primary" role="textbox" disabled={!isAllowed} /> {characterLimit && ( 255 ? "text-red-500" : "" }`} > {watch("name").length} /255 )} {errors.name ? errors.name.message : null} { if (!value && !watch("description_html")) return <>>; return ( { setShowAlert(true); setValue("description", jsonValue); }} onHTMLChange={(htmlValue) => { setShowAlert(true); setValue("description_html", htmlValue); }} onBlur={() => { setIsSubmitting(true); handleSubmit(handleDescriptionFormSubmit)() .then(() => setShowAlert(false)) .finally(() => setIsSubmitting(false)); }} placeholder="Description" editable={isAllowed} /> ); }} /> {isSubmitting && ( Saving... )} ); };