Skip to content

Commit

Permalink
feat: add reset to useForm, handle back button in DeleteAccountModal
Browse files Browse the repository at this point in the history
  • Loading branch information
naumovski-filip committed Jun 13, 2023
1 parent 7843a3d commit eacd3b7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion public/locales/en/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"firstname": "First name",
"hide_password": "Hide password",
"lastname": "Last name",
"password": "",
"password": "Password",
"save": "Save",
"security": "Password",
"terms_and_tracking": "Legal & Marketing",
Expand Down
39 changes: 32 additions & 7 deletions src/components/DeleteAccountModal/DeleteAccountModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'react-i18next';
import { type SchemaOf, object, string } from 'yup';
import { useNavigate, useLocation } from 'react-router';
import { useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useMutation } from 'react-query';

import PasswordField from '../PasswordField/PasswordField';
Expand Down Expand Up @@ -37,7 +37,13 @@ const DeleteAccountModal = () => {
password: string().required(t('login.field_required')),
});
const initialValues: DeleteAccountFormData = { password: '' };
const { handleSubmit, handleChange, values, errors } = useForm(
const {
handleSubmit,
handleChange,
values,
errors,
reset: resetForm,
} = useForm(
initialValues,
() => {
setEnteredPassword(values.password);
Expand All @@ -46,12 +52,31 @@ const DeleteAccountModal = () => {
validationSchema,
);

const handleCancel = () => {
navigate(removeQueryParam(location, 'u'));
};
useEffect(() => {
if (!location.search.includes('delete-account-confirmation') && enteredPassword) {
// handle back button
setEnteredPassword('');
deleteAccount.reset();
resetForm();
}
if (location.search.includes('delete-account-confirmation') && !enteredPassword) {
navigate(addQueryParam(location, 'u', 'delete-account'), { replace: true });
}
}, [location, location.search, navigate, enteredPassword, deleteAccount, resetForm]);

const handleError = useCallback(() => {
deleteAccount.reset();
resetForm();
setEnteredPassword('');
navigate(addQueryParam(location, 'u', 'delete-account'), { replace: true });
}, [location, navigate, setEnteredPassword, deleteAccount, resetForm]);

const handleCancel = useCallback(() => {
navigate(removeQueryParam(location, 'u'), { replace: true });
}, [location, navigate]);

if (deleteAccount.isError) {
return <Alert open isSuccess={false} onClose={handleCancel} message={t('account.delete_account.error')} />;
return <Alert open isSuccess={false} onClose={handleError} message={t('account.delete_account.error')} />;
}

return enteredPassword ? (
Expand Down Expand Up @@ -94,7 +119,7 @@ const DeleteAccountModal = () => {
/>
<div className={styles.passwordButtonsContainer}>
<Button type="submit" className={styles.button} color="primary" fullWidth label={t('account.continue')} />
<Button onClick={handleCancel} className={styles.button} variant="text" fullWidth label={t('account.cancel')} />
<Button type="button" onClick={handleCancel} className={styles.button} variant="text" fullWidth label={t('account.cancel')} />
</div>
</form>
);
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type UseFormReturnValue<T> = {
setValue: (key: keyof T, value: string) => void;
setErrors: (errors: FormErrors<T>) => void;
setSubmitting: (submitting: boolean) => void;
reset: () => void;
};

type UseFormMethods<T> = {
Expand All @@ -37,6 +38,13 @@ export default function useForm<T extends GenericFormValues>(
const [submitting, setSubmitting] = useState(false);
const [errors, setErrors] = useState<FormErrors<T>>({});

const reset = useCallback(() => {
setValues(initialValues);
setErrors({});
setSubmitting(false);
setTouched(Object.fromEntries((Object.keys(initialValues) as Array<keyof T>).map((key) => [key, false])) as Record<keyof T, boolean>);
}, [initialValues]);

const validateField = (name: string, formValues: T) => {
if (!validationSchema) return;

Expand Down Expand Up @@ -121,5 +129,5 @@ export default function useForm<T extends GenericFormValues>(
onSubmit(values, { setValue, setErrors, setSubmitting, validate });
};

return { values, errors, handleChange, handleBlur, handleSubmit, submitting, setValue, setErrors, setSubmitting };
return { values, errors, handleChange, handleBlur, handleSubmit, submitting, setValue, setErrors, setSubmitting, reset };
}

0 comments on commit eacd3b7

Please sign in to comment.