fix: persist data on tab switch in workspace (#646)

* fix: persist data on tab switch in workspace

* fix: build fail
This commit is contained in:
Saheb Giri 2023-03-31 16:03:35 +05:30 committed by GitHub
parent 4ab82b9616
commit 13b2a6fd53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 14 deletions

View File

@ -25,6 +25,11 @@ type Props = {
export const Workspace: React.FC<Props> = ({ setStep, setWorkspace }) => { export const Workspace: React.FC<Props> = ({ setStep, setWorkspace }) => {
const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false); const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false);
const [invitationsRespond, setInvitationsRespond] = useState<string[]>([]); const [invitationsRespond, setInvitationsRespond] = useState<string[]>([]);
const [defaultValues, setDefaultValues] = useState({
name: "",
slug: "",
company_size: null,
});
const { data: invitations, mutate } = useSWR(USER_WORKSPACE_INVITATIONS, () => const { data: invitations, mutate } = useSWR(USER_WORKSPACE_INVITATIONS, () =>
workspaceService.userWorkspaceInvitations() workspaceService.userWorkspaceInvitations()
@ -99,11 +104,13 @@ export const Workspace: React.FC<Props> = ({ setStep, setWorkspace }) => {
setWorkspace(res); setWorkspace(res);
setStep(3); setStep(3);
}} }}
defaultValues={defaultValues}
setDefaultValues={setDefaultValues}
/> />
</Tab.Panel> </Tab.Panel>
<Tab.Panel> <Tab.Panel>
<div className="mt-6"> <div className="mt-6">
<div className="divide-y pb-8 px-4"> <div className="divide-y px-4 pb-8">
{invitations && invitations.length > 0 ? ( {invitations && invitations.length > 0 ? (
invitations.map((invitation) => ( invitations.map((invitation) => (
<div key={invitation.id}> <div key={invitation.id}>

View File

@ -1,4 +1,4 @@
import { useEffect, useState } from "react"; import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { mutate } from "swr"; import { mutate } from "swr";
@ -19,15 +19,19 @@ import { COMPANY_SIZE } from "constants/workspace";
type Props = { type Props = {
onSubmit: (res: IWorkspace) => void; onSubmit: (res: IWorkspace) => void;
defaultValues: {
name: string;
slug: string;
company_size: number | null;
};
setDefaultValues: Dispatch<SetStateAction<any>>;
}; };
const defaultValues = { export const CreateWorkspaceForm: React.FC<Props> = ({
name: "", onSubmit,
slug: "", defaultValues,
company_size: null, setDefaultValues,
}; }) => {
export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
const [slugError, setSlugError] = useState(false); const [slugError, setSlugError] = useState(false);
const { setToastAlert } = useToast(); const { setToastAlert } = useToast();
@ -37,7 +41,7 @@ export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
handleSubmit, handleSubmit,
control, control,
setValue, setValue,
reset, getValues,
formState: { errors, isSubmitting }, formState: { errors, isSubmitting },
} = useForm<IWorkspace>({ defaultValues }); } = useForm<IWorkspace>({ defaultValues });
@ -72,9 +76,13 @@ export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
}); });
}; };
useEffect(() => { useEffect(
reset(defaultValues); () => () => {
}, [reset]); // when the component unmounts set the default values to whatever user typed in
setDefaultValues(getValues());
},
[getValues, setDefaultValues]
);
return ( return (
<form <form

View File

@ -16,6 +16,11 @@ import { CreateWorkspaceForm } from "components/workspace";
const CreateWorkspace: NextPage = () => { const CreateWorkspace: NextPage = () => {
const router = useRouter(); const router = useRouter();
const defaultValues = {
name: "",
slug: "",
company_size: null,
};
return ( return (
<DefaultLayout> <DefaultLayout>
@ -24,7 +29,11 @@ const CreateWorkspace: NextPage = () => {
<div className="mb-8 text-center"> <div className="mb-8 text-center">
<Image src={Logo} height="50" alt="Plane Logo" /> <Image src={Logo} height="50" alt="Plane Logo" />
</div> </div>
<CreateWorkspaceForm onSubmit={(res) => router.push(`/${res.slug}`)} /> <CreateWorkspaceForm
defaultValues={defaultValues}
setDefaultValues={() => {}}
onSubmit={(res) => router.push(`/${res.slug}`)}
/>
</div> </div>
</div> </div>
</DefaultLayout> </DefaultLayout>