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

@ -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<SetStateAction<any>>;
};
const defaultValues = {
name: "",
slug: "",
company_size: null,
};
export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
export const CreateWorkspaceForm: React.FC<Props> = ({
onSubmit,
defaultValues,
setDefaultValues,
}) => {
const [slugError, setSlugError] = useState(false);
const { setToastAlert } = useToast();
@ -37,7 +41,7 @@ export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
handleSubmit,
control,
setValue,
reset,
getValues,
formState: { errors, isSubmitting },
} = useForm<IWorkspace>({ defaultValues });
@ -72,9 +76,13 @@ export const CreateWorkspaceForm: React.FC<Props> = ({ onSubmit }) => {
});
};
useEffect(() => {
reset(defaultValues);
}, [reset]);
useEffect(
() => () => {
// when the component unmounts set the default values to whatever user typed in
setDefaultValues(getValues());
},
[getValues, setDefaultValues]
);
return (
<form