From f93fc254f409aab0520ef5e708e6fd2ca9191bae Mon Sep 17 00:00:00 2001 From: Mirlan Date: Thu, 29 Oct 2020 21:04:15 +0600 Subject: [PATCH] Ott 525 user account optional fields (#201) * refactor(#525): removed validation of optional fields * refactor(#525): removed form store validation helpers * fixup! refactor(#525): removed validation of optional fields --- .../FormStore/hooks/useFormValidators.tsx | 23 ----------- src/features/FormStore/index.tsx | 14 +------ src/features/UserAccount/config.tsx | 28 ------------- .../UserAccount/hooks/useUserInfo.tsx | 2 +- .../UserAccount/hooks/useValidateForm.tsx | 41 +++---------------- src/features/UserAccount/index.tsx | 8 ---- src/requests/saveUserInfo.tsx | 20 ++++----- 7 files changed, 19 insertions(+), 117 deletions(-) delete mode 100644 src/features/FormStore/hooks/useFormValidators.tsx delete mode 100644 src/features/UserAccount/config.tsx diff --git a/src/features/FormStore/hooks/useFormValidators.tsx b/src/features/FormStore/hooks/useFormValidators.tsx deleted file mode 100644 index 9ca1db88..00000000 --- a/src/features/FormStore/hooks/useFormValidators.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import trim from 'lodash/trim' -import every from 'lodash/every' -import isEmpty from 'lodash/isEmpty' - -import { useFormState } from './useFormState' - -type Args = ReturnType - -export const useFormValidators = ({ - readFormValue, - updateFormError, -}: Args) => { - const isFieldEmpty = (fieldName: string) => isEmpty(trim(readFormValue(fieldName))) - - const allFieldsEmpty = (fieldNames: Array) => ( - every(fieldNames, isFieldEmpty) - ) - - return { - allFieldsEmpty, - isFieldEmpty, - } -} diff --git a/src/features/FormStore/index.tsx b/src/features/FormStore/index.tsx index bf40ee21..74ded5d6 100644 --- a/src/features/FormStore/index.tsx +++ b/src/features/FormStore/index.tsx @@ -2,16 +2,11 @@ import type { ReactNode } from 'react' import React, { createContext, useContext, - useMemo, } from 'react' import { useFormState } from './hooks/useFormState' -import { useFormValidators } from './hooks/useFormValidators' -type FormStore = ( - ReturnType - & ReturnType -) +type FormStore = ReturnType type Props = { children: ReactNode, } @@ -27,13 +22,8 @@ export const FormStore = ({ children, }: Props) => { const formState = useFormState() - const validators = useFormValidators(formState) - const value = useMemo( - () => ({ ...formState, ...validators }), - [formState, validators], - ) return ( - + {children} ) diff --git a/src/features/UserAccount/config.tsx b/src/features/UserAccount/config.tsx deleted file mode 100644 index c46265ec..00000000 --- a/src/features/UserAccount/config.tsx +++ /dev/null @@ -1,28 +0,0 @@ -export const formIds = { - address_line1: 'address_line1', - address_line2: 'address_line2', - city: 'city', - city_id: 'city_id', - country: 'country', - countryId: 'countryId', - firstname: 'firstname', - formError: 'formError', - lastname: 'lastname', - password: 'password', - phone: 'phone', - postalCode: 'postalCode', - region: 'region', -} - -export const requiredFields = [ - formIds.country, - formIds.firstname, - formIds.lastname, - formIds.phone, -] - -export const simpleValidationFields = [ - formIds.country, - formIds.firstname, - formIds.lastname, -] diff --git a/src/features/UserAccount/hooks/useUserInfo.tsx b/src/features/UserAccount/hooks/useUserInfo.tsx index 6c49eb1c..784705da 100644 --- a/src/features/UserAccount/hooks/useUserInfo.tsx +++ b/src/features/UserAccount/hooks/useUserInfo.tsx @@ -39,7 +39,7 @@ export const useUserInfo = () => { } = useUserInfoForm() const readTrimmedValue = useCallback( - (fieldName: string) => trim(readFormValue(fieldName)), + (fieldName: string) => trim(readFormValue(fieldName)) || null, [readFormValue], ) const handleSubmit = useCallback(() => { diff --git a/src/features/UserAccount/hooks/useValidateForm.tsx b/src/features/UserAccount/hooks/useValidateForm.tsx index de87914f..4194576a 100644 --- a/src/features/UserAccount/hooks/useValidateForm.tsx +++ b/src/features/UserAccount/hooks/useValidateForm.tsx @@ -1,63 +1,34 @@ import trim from 'lodash/trim' -import reduce from 'lodash/reduce' import { useForm } from 'features/FormStore' import { isValidPhone } from 'helpers/isValidPhone' import { isValidPassword } from 'helpers/isValidPassword' -import { - formIds, - requiredFields, - simpleValidationFields, -} from 'config/form' +import { formIds } from 'config/form' export const useValidateForm = () => { const { - allFieldsEmpty, - isFieldEmpty, readFormValue, updateFormError, } = useForm() const readTrimmedValue = (fieldName: string) => trim(readFormValue(fieldName)) - const setErrorOnEmptyFields = (fieldNames: Array, message: string) => ( - reduce( - fieldNames, - (acc, fieldName) => { - if (isFieldEmpty(fieldName)) { - updateFormError(fieldName, message) - return true - } - return acc - }, - false, - ) - ) - const validateForm = () => { - let hasError = false + let isValid = true const phone = readTrimmedValue(formIds.phone) const password = readTrimmedValue(formIds.password) - if (allFieldsEmpty(requiredFields)) { - updateFormError(formIds.formError, 'error_fill_out_required_fields') - setErrorOnEmptyFields(requiredFields, '') - return false - } - - hasError = setErrorOnEmptyFields(simpleValidationFields, 'error_fill_out_this_field') - if (!isValidPassword(password)) { updateFormError(formIds.password, 'error_simple_password') - hasError = true + isValid = false } - if (!isValidPhone(phone)) { + if (phone && !isValidPhone(phone)) { updateFormError(formIds.phone, 'error_invalid_phone_format') - hasError = true + isValid = false } - return !hasError + return isValid } return validateForm diff --git a/src/features/UserAccount/index.tsx b/src/features/UserAccount/index.tsx index 335e7ee0..ef657448 100644 --- a/src/features/UserAccount/index.tsx +++ b/src/features/UserAccount/index.tsx @@ -69,7 +69,6 @@ export const UserAccount = () => { autoComplete='given-name' labelWidth={labelWidth} onChange={updateFormValue(formIds.firstname)} - error={readFormError(formIds.firstname)} editIcon maxLength={500} /> @@ -79,7 +78,6 @@ export const UserAccount = () => { autoComplete='family-name' labelWidth={labelWidth} onChange={updateFormValue(formIds.lastname)} - error={readFormError(formIds.lastname)} editIcon maxLength={500} /> @@ -107,7 +105,6 @@ export const UserAccount = () => { /> { /> { /> { /> { /> { />