2023-03-07 16:52:06 +00:00
|
|
|
import { FC, useCallback, useEffect, 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
|
2023-03-29 11:00:40 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-05-10 20:45:49 +00:00
|
|
|
// hooks
|
|
|
|
import useReloadConfirmations from "hooks/use-reload-confirmation";
|
2023-01-26 18:12:20 +00:00
|
|
|
// components
|
2023-02-17 14:28:27 +00:00
|
|
|
import { Loader, TextArea } from "components/ui";
|
2023-01-26 18:12:20 +00:00
|
|
|
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-04-08 08:16:46 +00:00
|
|
|
import { IIssue } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export interface IssueDescriptionFormValues {
|
|
|
|
name: string;
|
|
|
|
description: any;
|
|
|
|
description_html: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IssueDetailsProps {
|
2023-06-16 13:27:17 +00:00
|
|
|
issue: {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
description_html: string;
|
|
|
|
};
|
2023-03-07 16:52:06 +00:00
|
|
|
handleFormSubmit: (value: IssueDescriptionFormValues) => Promise<void>;
|
2023-06-16 13:27:17 +00:00
|
|
|
isAllowed: boolean;
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 13:27:17 +00:00
|
|
|
export const IssueDescriptionForm: FC<IssueDetailsProps> = ({
|
|
|
|
issue,
|
|
|
|
handleFormSubmit,
|
|
|
|
isAllowed,
|
|
|
|
}) => {
|
2023-03-07 16:52:06 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2023-02-23 05:24:54 +00:00
|
|
|
const [characterLimit, setCharacterLimit] = useState(false);
|
2023-01-31 12:40:50 +00:00
|
|
|
|
2023-05-10 20:45:49 +00:00
|
|
|
const { setShowAlert } = useReloadConfirmations();
|
|
|
|
|
2023-01-31 12:40:50 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
watch,
|
|
|
|
setValue,
|
|
|
|
reset,
|
2023-03-29 11:00:40 +00:00
|
|
|
register,
|
|
|
|
control,
|
2023-01-31 12:40:50 +00:00
|
|
|
formState: { errors },
|
|
|
|
} = 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(
|
2023-03-07 16:52:06 +00:00
|
|
|
async (formData: Partial<IIssue>) => {
|
2023-02-23 05:24:54 +00:00
|
|
|
if (!formData.name || formData.name.length === 0 || formData.name.length > 255) return;
|
2023-01-31 12:40:50 +00:00
|
|
|
|
2023-03-07 16:52:06 +00:00
|
|
|
await handleFormSubmit({
|
2023-01-30 14:17:30 +00:00
|
|
|
name: formData.name ?? "",
|
2023-02-20 05:53:04 +00:00
|
|
|
description: formData.description ?? "",
|
|
|
|
description_html: formData.description_html ?? "<p></p>",
|
2023-01-30 14:17:30 +00:00
|
|
|
});
|
|
|
|
},
|
2023-02-23 05:24:54 +00:00
|
|
|
[handleFormSubmit]
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
// reset form values
|
2023-01-30 14:07:25 +00:00
|
|
|
useEffect(() => {
|
2023-01-30 14:17:30 +00:00
|
|
|
if (!issue) return;
|
|
|
|
|
2023-05-10 20:45:49 +00:00
|
|
|
reset({
|
|
|
|
...issue,
|
|
|
|
});
|
2023-01-30 14:17:30 +00:00
|
|
|
}, [issue, reset]);
|
2023-01-30 14:07:25 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
2023-03-17 05:09:06 +00:00
|
|
|
<div className="relative">
|
2023-02-23 05:24:54 +00:00
|
|
|
<div className="relative">
|
|
|
|
<TextArea
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
placeholder="Enter issue name"
|
2023-03-29 11:00:40 +00:00
|
|
|
register={register}
|
2023-02-23 05:24:54 +00:00
|
|
|
onFocus={() => setCharacterLimit(true)}
|
2023-03-07 16:52:06 +00:00
|
|
|
onBlur={() => {
|
|
|
|
setCharacterLimit(false);
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)()
|
|
|
|
.then(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
});
|
|
|
|
}}
|
2023-02-23 05:24:54 +00:00
|
|
|
required={true}
|
2023-07-10 07:17:00 +00:00
|
|
|
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"
|
2023-02-23 05:24:54 +00:00
|
|
|
role="textbox"
|
2023-06-16 13:27:17 +00:00
|
|
|
disabled={!isAllowed}
|
2023-02-23 05:24:54 +00:00
|
|
|
/>
|
|
|
|
{characterLimit && (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="pointer-events-none absolute bottom-0 right-0 z-[2] rounded bg-custom-background-80 p-1 text-xs">
|
2023-02-23 05:24:54 +00:00
|
|
|
<span
|
|
|
|
className={`${
|
|
|
|
watch("name").length === 0 || watch("name").length > 255 ? "text-red-500" : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{watch("name").length}
|
|
|
|
</span>
|
|
|
|
/255
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-01-31 12:40:50 +00:00
|
|
|
<span>{errors.name ? errors.name.message : null}</span>
|
2023-03-29 11:00:40 +00:00
|
|
|
<Controller
|
|
|
|
name="description"
|
|
|
|
control={control}
|
2023-05-10 20:45:49 +00:00
|
|
|
render={({ field: { value } }) => {
|
2023-05-11 08:11:16 +00:00
|
|
|
if (!value && !watch("description_html")) return <></>;
|
2023-05-10 20:45:49 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RemirrorRichTextEditor
|
|
|
|
value={
|
|
|
|
!value ||
|
|
|
|
value === "" ||
|
|
|
|
(typeof value === "object" && Object.keys(value).length === 0)
|
|
|
|
? watch("description_html")
|
|
|
|
: value
|
|
|
|
}
|
|
|
|
onJSONChange={(jsonValue) => {
|
|
|
|
setShowAlert(true);
|
|
|
|
setValue("description", jsonValue);
|
|
|
|
}}
|
|
|
|
onHTMLChange={(htmlValue) => {
|
|
|
|
setShowAlert(true);
|
|
|
|
setValue("description_html", htmlValue);
|
|
|
|
}}
|
|
|
|
onBlur={() => {
|
|
|
|
setIsSubmitting(true);
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)()
|
|
|
|
.then(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
setShowAlert(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
});
|
|
|
|
}}
|
2023-05-11 08:11:16 +00:00
|
|
|
placeholder="Description"
|
2023-06-16 13:27:17 +00:00
|
|
|
editable={isAllowed}
|
2023-05-10 20:45:49 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2023-01-26 18:12:20 +00:00
|
|
|
/>
|
2023-03-17 05:09:06 +00:00
|
|
|
<div
|
2023-07-10 07:17:00 +00:00
|
|
|
className={`absolute -bottom-8 right-0 text-sm text-custom-text-200 ${
|
2023-03-17 05:09:06 +00:00
|
|
|
isSubmitting ? "block" : "hidden"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
Saving...
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|