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-08-25 12:11:23 +00:00
|
|
|
import { useDebouncedCallback } from "use-debounce";
|
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-09-20 04:40:11 +00:00
|
|
|
import { TipTapEditor } from "@plane/editor"
|
2023-09-19 08:12:54 +00:00
|
|
|
import fileService from "services/file.service";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export interface IssueDescriptionFormValues {
|
|
|
|
name: string;
|
|
|
|
description_html: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IssueDetailsProps {
|
2023-06-16 13:27:17 +00:00
|
|
|
issue: {
|
|
|
|
name: 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,
|
|
|
|
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_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_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-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">
|
2023-08-25 12:11:23 +00:00
|
|
|
{isAllowed ? (
|
|
|
|
<TextArea
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
placeholder="Enter issue name"
|
|
|
|
register={register}
|
|
|
|
onFocus={() => setCharacterLimit(true)}
|
|
|
|
onChange={(e) => {
|
|
|
|
setCharacterLimit(false);
|
|
|
|
setIsSubmitting("submitting");
|
|
|
|
debouncedTitleSave();
|
|
|
|
}}
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<h4 className="break-words text-2xl font-semibold">{issue.name}</h4>
|
|
|
|
)}
|
|
|
|
{characterLimit && isAllowed && (
|
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-09-19 08:12: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-08-25 12:11:23 +00:00
|
|
|
if (!value) return <></>;
|
2023-05-10 20:45:49 +00:00
|
|
|
|
2023-07-23 16:42:13 +00:00
|
|
|
return (
|
2023-08-28 10:24:49 +00:00
|
|
|
<TipTapEditor
|
2023-09-19 08:12:54 +00:00
|
|
|
uploadFile={fileService.uploadFile}
|
|
|
|
deleteFile={fileService.deleteImage}
|
2023-07-23 16:42:13 +00:00
|
|
|
value={
|
|
|
|
!value ||
|
2023-09-19 08:12:54 +00:00
|
|
|
value === "" ||
|
|
|
|
(typeof value === "object" && Object.keys(value).length === 0)
|
2023-08-25 12:11:23 +00:00
|
|
|
? "<p></p>"
|
2023-07-23 16:42:13 +00:00
|
|
|
: 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-25 12:11:23 +00:00
|
|
|
customClassName={
|
|
|
|
isAllowed ? "min-h-[150px] shadow-sm" : "!p-0 !pt-2 text-custom-text-200"
|
|
|
|
}
|
|
|
|
noBorder={!isAllowed}
|
2023-08-15 09:34:46 +00:00
|
|
|
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);
|
2023-08-25 12:11:23 +00:00
|
|
|
handleSubmit(handleDescriptionFormSubmit)().finally(() =>
|
|
|
|
setIsSubmitting("submitted")
|
|
|
|
);
|
2023-07-23 16:42:13 +00:00
|
|
|
}}
|
2023-08-25 12:11:23 +00:00
|
|
|
editable={isAllowed}
|
2023-07-23 16:42:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
2023-08-16 12:55:11 +00:00
|
|
|
<div
|
2023-09-19 08:12: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>
|
|
|
|
);
|
|
|
|
};
|