From eacd3b7575cc0d6cbf891c7ed7943c9b4b66ab76 Mon Sep 17 00:00:00 2001 From: Filip Naumovski Date: Tue, 13 Jun 2023 11:09:29 +0200 Subject: [PATCH] feat: add reset to useForm, handle back button in DeleteAccountModal --- public/locales/en/user.json | 2 +- .../DeleteAccountModal/DeleteAccountModal.tsx | 39 +++++++++++++++---- src/hooks/useForm.ts | 10 ++++- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/public/locales/en/user.json b/public/locales/en/user.json index ae2cbb576..965978194 100644 --- a/public/locales/en/user.json +++ b/public/locales/en/user.json @@ -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", diff --git a/src/components/DeleteAccountModal/DeleteAccountModal.tsx b/src/components/DeleteAccountModal/DeleteAccountModal.tsx index de78daf40..c348f7f1a 100644 --- a/src/components/DeleteAccountModal/DeleteAccountModal.tsx +++ b/src/components/DeleteAccountModal/DeleteAccountModal.tsx @@ -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'; @@ -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); @@ -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 ; + return ; } return enteredPassword ? ( @@ -94,7 +119,7 @@ const DeleteAccountModal = () => { />
); diff --git a/src/hooks/useForm.ts b/src/hooks/useForm.ts index 44cfd0fbd..ff130687b 100644 --- a/src/hooks/useForm.ts +++ b/src/hooks/useForm.ts @@ -13,6 +13,7 @@ export type UseFormReturnValue = { setValue: (key: keyof T, value: string) => void; setErrors: (errors: FormErrors) => void; setSubmitting: (submitting: boolean) => void; + reset: () => void; }; type UseFormMethods = { @@ -37,6 +38,13 @@ export default function useForm( const [submitting, setSubmitting] = useState(false); const [errors, setErrors] = useState>({}); + const reset = useCallback(() => { + setValues(initialValues); + setErrors({}); + setSubmitting(false); + setTouched(Object.fromEntries((Object.keys(initialValues) as Array).map((key) => [key, false])) as Record); + }, [initialValues]); + const validateField = (name: string, formValues: T) => { if (!validationSchema) return; @@ -121,5 +129,5 @@ export default function useForm( 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 }; }