From 13b2a6fd53133ead58789de399802086f1e6499c Mon Sep 17 00:00:00 2001 From: Saheb Giri <47132373+iamsahebgiri@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:03:35 +0530 Subject: [PATCH] fix: persist data on tab switch in workspace (#646) * fix: persist data on tab switch in workspace * fix: build fail --- apps/app/components/onboarding/workspace.tsx | 9 +++++- .../workspace/create-workspace-form.tsx | 32 ++++++++++++------- apps/app/pages/create-workspace.tsx | 11 ++++++- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/apps/app/components/onboarding/workspace.tsx b/apps/app/components/onboarding/workspace.tsx index 35453fc55..5ad6aedfb 100644 --- a/apps/app/components/onboarding/workspace.tsx +++ b/apps/app/components/onboarding/workspace.tsx @@ -25,6 +25,11 @@ type Props = { export const Workspace: React.FC = ({ setStep, setWorkspace }) => { const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false); const [invitationsRespond, setInvitationsRespond] = useState([]); + const [defaultValues, setDefaultValues] = useState({ + name: "", + slug: "", + company_size: null, + }); const { data: invitations, mutate } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() @@ -99,11 +104,13 @@ export const Workspace: React.FC = ({ setStep, setWorkspace }) => { setWorkspace(res); setStep(3); }} + defaultValues={defaultValues} + setDefaultValues={setDefaultValues} />
-
+
{invitations && invitations.length > 0 ? ( invitations.map((invitation) => (
diff --git a/apps/app/components/workspace/create-workspace-form.tsx b/apps/app/components/workspace/create-workspace-form.tsx index c1555faa5..3f63dbf92 100644 --- a/apps/app/components/workspace/create-workspace-form.tsx +++ b/apps/app/components/workspace/create-workspace-form.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { mutate } from "swr"; @@ -19,15 +19,19 @@ import { COMPANY_SIZE } from "constants/workspace"; type Props = { onSubmit: (res: IWorkspace) => void; + defaultValues: { + name: string; + slug: string; + company_size: number | null; + }; + setDefaultValues: Dispatch>; }; -const defaultValues = { - name: "", - slug: "", - company_size: null, -}; - -export const CreateWorkspaceForm: React.FC = ({ onSubmit }) => { +export const CreateWorkspaceForm: React.FC = ({ + onSubmit, + defaultValues, + setDefaultValues, +}) => { const [slugError, setSlugError] = useState(false); const { setToastAlert } = useToast(); @@ -37,7 +41,7 @@ export const CreateWorkspaceForm: React.FC = ({ onSubmit }) => { handleSubmit, control, setValue, - reset, + getValues, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); @@ -72,9 +76,13 @@ export const CreateWorkspaceForm: React.FC = ({ onSubmit }) => { }); }; - useEffect(() => { - reset(defaultValues); - }, [reset]); + useEffect( + () => () => { + // when the component unmounts set the default values to whatever user typed in + setDefaultValues(getValues()); + }, + [getValues, setDefaultValues] + ); return (
{ const router = useRouter(); + const defaultValues = { + name: "", + slug: "", + company_size: null, + }; return ( @@ -24,7 +29,11 @@ const CreateWorkspace: NextPage = () => {
Plane Logo
- router.push(`/${res.slug}`)} /> + {}} + onSubmit={(res) => router.push(`/${res.slug}`)} + />