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:
parent
4ab82b9616
commit
13b2a6fd53
3 changed files with 38 additions and 14 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue