2023-03-07 16:52:06 +00:00
|
|
|
import { FC, useCallback, useEffect, useState } from "react";
|
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-08-15 09:34:46 +00:00
|
|
|
import { TextArea } from "components/ui";
|
|
|
|
|
2023-01-30 14:17:30 +00:00
|
|
|
// types
|
2023-04-08 08:16:46 +00:00
|
|
|
import { IIssue } from "types";
|
2023-08-15 09:34:46 +00:00
|
|
|
import Tiptap from "components/tiptap";
|
|
|
|
import { useDebouncedCallback } from "use-debounce";
|
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-08-19 13:28:54 +00:00
|
|
|
workspaceSlug: 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,
|
2023-08-19 13:28:54 +00:00
|
|
|
workspaceSlug,
|
2023-06-16 13:27:17 +00:00
|
|
|
isAllowed,
|
|
|
|
}) => {
|
2023-08-15 09:34:46 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState<"submitting" | "submitted" | "saved">("saved");
|
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-08-15 09:34:46 +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-08-15 09:34:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (isSubmitting === "submitted") {
|
2023-08-19 13:28:54 +00:00
|
|
|
setShowAlert(false);
|
2023-08-15 09:34:46 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
setIsSubmitting("saved");
|
|
|
|
}, 2000);
|
2023-08-19 13:28:54 +00:00
|
|
|
} else if (isSubmitting === "submitting") {
|
|
|
|
setShowAlert(true);
|
2023-08-15 09:34:46 +00:00
|
|
|
}
|
2023-08-19 13:28:54 +00:00
|
|
|
}, [isSubmitting, setShowAlert]);
|
|
|
|
|
2023-08-15 09:34:46 +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-08-15 09:34:46 +00:00
|
|
|
const debouncedTitleSave = useDebouncedCallback(async () => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)().finally(() => setIsSubmitting("submitted"));
|
|
|
|
}, 500);
|
|
|
|
}, 1000);
|
|
|
|
|
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-08-15 09:34:46 +00:00
|
|
|
onChange={(e) => {
|
2023-03-07 16:52:06 +00:00
|
|
|
setCharacterLimit(false);
|
2023-08-15 09:34:46 +00:00
|
|
|
setIsSubmitting("submitting");
|
|
|
|
debouncedTitleSave();
|
2023-03-07 16:52:06 +00:00
|
|
|
}}
|
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-23 16:42:13 +00:00
|
|
|
<div className="pointer-events-none absolute bottom-1 right-1 z-[2] rounded bg-custom-background-100 text-custom-text-200 p-0.5 text-xs">
|
2023-02-23 05:24:54 +00:00
|
|
|
<span
|
2023-08-19 13:28:54 +00:00
|
|
|
className={`${watch("name").length === 0 || watch("name").length > 255 ? "text-red-500" : ""
|
|
|
|
}`}
|
2023-02-23 05:24:54 +00:00
|
|
|
>
|
|
|
|
{watch("name").length}
|
|
|
|
</span>
|
|
|
|
/255
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-01-31 12:40:50 +00:00
|
|
|
<span>{errors.name ? errors.name.message : null}</span>
|
2023-08-16 12:55:11 +00:00
|
|
|
<div className="relative">
|
2023-07-23 16:42:13 +00:00
|
|
|
<Controller
|
2023-08-15 09:34:46 +00:00
|
|
|
name="description_html"
|
2023-07-23 16:42:13 +00:00
|
|
|
control={control}
|
2023-08-15 09:34:46 +00:00
|
|
|
render={({ field: { value, onChange } }) => {
|
2023-07-23 16:42:13 +00:00
|
|
|
if (!value && !watch("description_html")) return <></>;
|
2023-05-10 20:45:49 +00:00
|
|
|
|
2023-07-23 16:42:13 +00:00
|
|
|
return (
|
2023-08-15 09:34:46 +00:00
|
|
|
<Tiptap
|
2023-07-23 16:42:13 +00:00
|
|
|
value={
|
|
|
|
!value ||
|
2023-08-19 13:28:54 +00:00
|
|
|
value === "" ||
|
|
|
|
(typeof value === "object" && Object.keys(value).length === 0)
|
2023-07-23 16:42:13 +00:00
|
|
|
? watch("description_html")
|
|
|
|
: value
|
|
|
|
}
|
2023-08-19 13:28:54 +00:00
|
|
|
workspaceSlug={workspaceSlug}
|
2023-08-15 09:34:46 +00:00
|
|
|
debouncedUpdatesEnabled={true}
|
2023-08-19 13:28:54 +00:00
|
|
|
setShouldShowAlert={setShowAlert}
|
2023-08-15 09:34:46 +00:00
|
|
|
setIsSubmitting={setIsSubmitting}
|
2023-08-18 06:31:51 +00:00
|
|
|
customClassName="min-h-[150px] shadow-sm"
|
2023-08-15 09:34:46 +00:00
|
|
|
editorContentCustomClassNames="pb-9"
|
|
|
|
onChange={(description: Object, description_html: string) => {
|
2023-08-19 13:28:54 +00:00
|
|
|
setShowAlert(true);
|
2023-08-15 09:34:46 +00:00
|
|
|
setIsSubmitting("submitting");
|
|
|
|
onChange(description_html);
|
|
|
|
setValue("description", description);
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)().finally(() => {
|
|
|
|
setIsSubmitting("submitted");
|
|
|
|
});
|
2023-07-23 16:42:13 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
2023-08-16 12:55:11 +00:00
|
|
|
<div
|
2023-08-19 13:28:54 +00:00
|
|
|
className={`absolute right-5 bottom-5 text-xs text-custom-text-200 border border-custom-border-400 rounded-xl w-[6.5rem] py-1 z-10 flex items-center justify-center ${isSubmitting === "saved" ? "fadeOut" : "fadeIn"
|
|
|
|
}`}
|
2023-08-16 12:55:11 +00:00
|
|
|
>
|
|
|
|
{isSubmitting === "submitting" ? "Saving..." : "Saved"}
|
2023-08-15 09:34:46 +00:00
|
|
|
</div>
|
2023-03-17 05:09:06 +00:00
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|