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-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-02-01 15:03:18 +00:00
|
|
|
import { IIssue, UserAuth } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export interface IssueDescriptionFormValues {
|
|
|
|
name: string;
|
|
|
|
description: any;
|
|
|
|
description_html: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IssueDetailsProps {
|
|
|
|
issue: IIssue;
|
2023-03-07 16:52:06 +00:00
|
|
|
handleFormSubmit: (value: IssueDescriptionFormValues) => Promise<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-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
|
|
|
|
|
|
|
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-03-29 11:00:40 +00:00
|
|
|
// useEffect(() => {
|
|
|
|
// const alertUser = (e: BeforeUnloadEvent) => {
|
|
|
|
// e.preventDefault();
|
|
|
|
// e.returnValue = "";
|
|
|
|
// return "Are you sure you want to leave?";
|
|
|
|
// };
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-03-29 11:00:40 +00:00
|
|
|
// window.addEventListener("beforeunload", alertUser);
|
|
|
|
// return () => {
|
|
|
|
// window.removeEventListener("beforeunload", alertUser);
|
|
|
|
// };
|
|
|
|
// }, [isSubmitting]);
|
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;
|
|
|
|
|
|
|
|
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-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-03-07 16:52:06 +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-theme"
|
2023-02-23 05:24:54 +00:00
|
|
|
role="textbox"
|
|
|
|
/>
|
|
|
|
{characterLimit && (
|
2023-03-07 16:52:06 +00:00
|
|
|
<div className="pointer-events-none absolute bottom-0 right-0 z-[2] rounded bg-white 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}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<RemirrorRichTextEditor
|
|
|
|
value={
|
|
|
|
!value ||
|
|
|
|
value === "" ||
|
|
|
|
(typeof value === "object" && Object.keys(value).length === 0)
|
|
|
|
? watch("description_html")
|
|
|
|
: value
|
|
|
|
}
|
|
|
|
onJSONChange={(jsonValue) => setValue("description", jsonValue)}
|
|
|
|
onHTMLChange={(htmlValue) => setValue("description_html", htmlValue)}
|
|
|
|
onBlur={() => {
|
|
|
|
setIsSubmitting(true);
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)()
|
|
|
|
.then(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
placeholder="Describe the issue..."
|
|
|
|
editable={!isNotAllowed}
|
|
|
|
/>
|
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
/>
|
2023-03-17 05:09:06 +00:00
|
|
|
<div
|
|
|
|
className={`absolute -bottom-8 right-0 text-sm text-gray-500 ${
|
|
|
|
isSubmitting ? "block" : "hidden"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
Saving...
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|