From fcf23b985bdd24bb05231e5f307c7b96edbc161d Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Mon, 30 Jan 2023 19:47:30 +0530 Subject: [PATCH] fix: issue description debounce issue (#208) * fix: issue description form * fix: build errors --- .../components/issues/description-form.tsx | 102 ++++++++++-------- apps/app/components/issues/form.tsx | 2 +- .../issue-detail-sidebar/index.tsx | 21 ++++ apps/app/package.json | 4 + .../projects/[projectId]/issues/[issueId].tsx | 24 +---- pnpm-lock.yaml | 86 ++++++++++++++- 6 files changed, 172 insertions(+), 67 deletions(-) diff --git a/apps/app/components/issues/description-form.tsx b/apps/app/components/issues/description-form.tsx index 4a484d819..d3f5433cc 100644 --- a/apps/app/components/issues/description-form.tsx +++ b/apps/app/components/issues/description-form.tsx @@ -1,7 +1,11 @@ -import { FC, useEffect, useRef, useState } from "react"; +import { FC, useCallback, useEffect, useMemo } from "react"; + import dynamic from "next/dynamic"; -// types -import { IIssue } from "types"; + +// react-hook-form +import { useForm } from "react-hook-form"; +// lodash +import debounce from "lodash.debounce"; // components import { Loader, Input } from "components/ui"; const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), { @@ -12,8 +16,8 @@ const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor ), }); -// hooks -import useDebounce from "hooks/use-debounce"; +// types +import { IIssue } from "types"; export interface IssueDescriptionFormValues { name: string; @@ -23,61 +27,73 @@ export interface IssueDescriptionFormValues { export interface IssueDetailsProps { issue: IIssue; - handleSubmit: (value: IssueDescriptionFormValues) => void; + handleFormSubmit: (value: IssueDescriptionFormValues) => void; } -export const IssueDescriptionForm: FC = ({ issue, handleSubmit }) => { - // states - // const [issueFormValues, setIssueFormValues] = useState({ - // name: issue.name, - // description: issue?.description, - // description_html: issue?.description_html, - // }); - const [issueName, setIssueName] = useState(issue?.name); - const [issueDescription, setIssueDescription] = useState(issue?.description); - const [issueDescriptionHTML, setIssueDescriptionHTML] = useState(issue?.description_html); - const textareaRef = useRef(null); +export const IssueDescriptionForm: FC = ({ issue, handleFormSubmit }) => { + const { handleSubmit, watch, setValue, reset } = useForm({ + defaultValues: { + name: "", + description: "", + description_html: "", + }, + }); - // hooks - const formValues = useDebounce( - { name: issueName, description: issueDescription, description_html: issueDescriptionHTML }, - 2000 + const handleDescriptionFormSubmit = useCallback( + (formData: Partial) => { + handleFormSubmit({ + name: formData.name ?? "", + description: formData.description, + description_html: formData.description_html, + }); + }, + [handleFormSubmit] ); - const stringFromValues = JSON.stringify(formValues); - useEffect(() => { - handleSubmit(formValues); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [handleSubmit, stringFromValues]); + const debounceHandler = useMemo( + () => debounce(handleSubmit(handleDescriptionFormSubmit), 2000), + [handleSubmit, handleDescriptionFormSubmit] + ); + useEffect( + () => () => { + debounceHandler.cancel(); + }, + [debounceHandler] + ); + + // reset form values useEffect(() => { - if (textareaRef && textareaRef.current) { - textareaRef.current.style.height = "0px"; - const scrollHeight = textareaRef.current.scrollHeight; - textareaRef.current.style.height = scrollHeight + "px"; - } - }, [issueName]); + if (!issue) return; + + reset(issue); + }, [issue, reset]); return (
-