Skip to content

Commit

Permalink
Fix race conditions when setting default value
Browse files Browse the repository at this point in the history
  • Loading branch information
amitbadala committed Oct 30, 2023
1 parent bcef516 commit 7241f6a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
45 changes: 19 additions & 26 deletions lib/build/emailpassword-shared7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 20 additions & 28 deletions lib/ts/recipe/emailpassword/components/library/formBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import STGeneralError from "supertokens-web-js/utils/error";
import { MANDATORY_FORM_FIELDS_ID_ARRAY } from "../../constants";

import type { APIFormField } from "../../../../types";
import type { FormBaseProps } from "../../types";
import type { FormBaseProps, FormFieldThemeProps } from "../../types";
import type { FormEvent } from "react";

import { Button, FormRow, Input, InputError, Label } from ".";
Expand All @@ -37,24 +37,24 @@ type FieldState = {
value: string;
};

// const fetchDefaultValue = (field: FormFieldThemeProps): string => {
// if (field && field.getDefaultValue) {
// try {
// if (typeof field.getDefaultValue !== "function") {
// throw new Error(`getDefaultValue for ${field.id} must be a function`);
// }
// const defaultValue = field.getDefaultValue();
// if (typeof defaultValue !== "string") {
// throw new Error(`getDefaultValue for ${field.id} must return a string`);
// } else {
// return defaultValue;
// }
// } catch (error) {
// console.error(error);
// }
// }
// return "";
// };
const fetchDefaultValue = (field: FormFieldThemeProps): string => {
if (field && field.getDefaultValue) {
try {
if (typeof field.getDefaultValue !== "function") {
throw new Error(`getDefaultValue for ${field.id} must be a function`);
}
const defaultValue = field.getDefaultValue();
if (typeof defaultValue !== "string") {
throw new Error(`getDefaultValue for ${field.id} must return a string`);
} else {
return defaultValue;
}
} catch (error) {
console.error(error);
}
}
return "";
};

export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
const { footer, buttonLabel, showLabels, validateOnBlur, formFields } = props;
Expand All @@ -69,18 +69,10 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
}, [unmounting]);

const [fieldStates, setFieldStates] = useState<FieldState[]>(
props.formFields.map((f) => ({ id: f.id, value: "" }))
props.formFields.map((f) => ({ id: f.id, value: fetchDefaultValue(f) }))
);
const [isLoading, setIsLoading] = useState(false);

// useEffect(() => {
// const initialValues = props.formFields.map((f) => ({
// id: f.id,
// value: fetchDefaultValue(f),
// }));
// setFieldStates(initialValues);
// }, []);

const updateFieldState = useCallback(
(id: string, update: (os: FieldState) => FieldState) => {
setFieldStates((os) => {
Expand Down

0 comments on commit 7241f6a

Please sign in to comment.