Ott 487 registration fields (#183)

* refactor(#487): left only name, lastname, email and password fields in registration (#180)

* refactor(#487): moved all User account feauter components into components folder (#182)

* refactor(#487): moved back global hooks into user account feature (#181)
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Mirlan 5 years ago committed by GitHub
parent 64966e942e
commit b0bd43aad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      src/features/Register/components/RegistrationStep/hooks/index.tsx
  2. 31
      src/features/Register/components/RegistrationStep/hooks/useSubmitHandler.tsx
  3. 50
      src/features/Register/components/RegistrationStep/hooks/useValidateForm.tsx
  4. 65
      src/features/Register/components/RegistrationStep/index.tsx
  5. 0
      src/features/UserAccount/components/CardNumber/index.tsx
  6. 0
      src/features/UserAccount/components/CardNumber/styled.tsx
  7. 0
      src/features/UserAccount/components/PageTitle/index.tsx
  8. 0
      src/features/UserAccount/components/PageTitle/styled.tsx
  9. 0
      src/features/UserAccount/components/PlusIcon/index.tsx
  10. 0
      src/features/UserAccount/components/PlusIcon/styled.tsx
  11. 0
      src/features/UserAccount/components/TextNoBorder/index.tsx
  12. 0
      src/features/UserAccount/components/TextNoBorder/styled.tsx
  13. 0
      src/features/UserAccount/components/UserAccountButton/index.tsx
  14. 0
      src/features/UserAccount/components/UserAccountButton/styled.tsx
  15. 0
      src/features/UserAccount/components/UserAccountSubscription/index.tsx
  16. 0
      src/features/UserAccount/components/UserAccountSubscription/styled.tsx
  17. 0
      src/features/UserAccount/components/VisaLogo/index.tsx
  18. 0
      src/features/UserAccount/components/VisaLogo/styled.tsx
  19. 7
      src/features/UserAccount/hooks/index.tsx
  20. 0
      src/features/UserAccount/hooks/useCities.tsx
  21. 0
      src/features/UserAccount/hooks/useCountries.tsx
  22. 16
      src/features/UserAccount/hooks/useUserInfoForm.tsx
  23. 0
      src/features/UserAccount/hooks/useValidateForm.tsx
  24. 379
      src/features/UserAccount/index.tsx
  25. 8
      src/helpers/isValidPassword/__tests__/index.tsx
  26. 13
      src/helpers/isValidPassword/index.tsx
  27. 69
      src/hooks/useValidateForm.tsx
  28. 24
      src/requests/register.tsx

@ -0,0 +1,39 @@
import type { FocusEvent } from 'react'
import { useCallback } from 'react'
import trim from 'lodash/trim'
import { isValidEmail } from 'helpers/isValidEmail'
import { formIds } from 'config/form'
import { useForm } from 'features/FormStore'
import { useSubmitHandler } from './useSubmitHandler'
export const useRegistrationForm = () => {
const {
readFormError,
readFormValue,
updateFormError,
updateFormValue,
} = useForm()
const handleSubmit = useSubmitHandler()
const onEmailBlur = useCallback(({ target }: FocusEvent<HTMLInputElement>) => {
const email = trim(target.value)
if (email && !isValidEmail(email)) {
updateFormError(formIds.email, 'error_invalid_email_format')
}
}, [updateFormError])
return {
handleSubmit,
onEmailBlur,
readFormError,
readFormValue,
updateFormError,
updateFormValue,
}
}

@ -2,25 +2,14 @@ import type { FormEvent } from 'react'
import trim from 'lodash/trim' import trim from 'lodash/trim'
import type { City } from 'requests'
import { formIds } from 'config/form' import { formIds } from 'config/form'
import { useAuthStore } from 'features/AuthStore' import { useAuthStore } from 'features/AuthStore'
import { useForm } from 'features/FormStore' import { useForm } from 'features/FormStore'
import type { SelectedCountry } from 'hooks/useCountries' import { useValidateForm } from './useValidateForm'
import { useValidateForm } from 'hooks/useValidateForm'
type Args = {
selectedCity: City | null,
selectedCountry: SelectedCountry,
}
export const useSubmitHandler = ({ export const useSubmitHandler = () => {
selectedCity,
selectedCountry,
}: Args) => {
const { register } = useAuthStore() const { register } = useAuthStore()
const { readFormValue } = useForm() const { readFormValue } = useForm()
const validateForm = useValidateForm() const validateForm = useValidateForm()
@ -30,31 +19,17 @@ export const useSubmitHandler = ({
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => { const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault() event.preventDefault()
if (validateForm() && selectedCountry) { if (validateForm()) {
const firstname = readTrimmedValue(formIds.firstname) const firstname = readTrimmedValue(formIds.firstname)
const lastname = readTrimmedValue(formIds.lastname) const lastname = readTrimmedValue(formIds.lastname)
const phone = readTrimmedValue(formIds.phone)
const email = readTrimmedValue(formIds.email) const email = readTrimmedValue(formIds.email)
const password = readTrimmedValue(formIds.password) const password = readTrimmedValue(formIds.password)
const postalCode = Number(readTrimmedValue(formIds.postalCode))
const region = readTrimmedValue(formIds.region)
const address1 = readTrimmedValue(formIds.address1)
const address2 = readTrimmedValue(formIds.address2)
const city = readTrimmedValue(formIds.city)
register({ register({
address1,
address2,
city,
cityId: selectedCity?.id,
countryId: selectedCountry.id,
email, email,
firstname, firstname,
lastname, lastname,
password, password,
phone,
postalCode,
region,
}) })
} }
} }

@ -0,0 +1,50 @@
import trim from 'lodash/trim'
import { formIds } from 'config/form'
import { isValidEmail } from 'helpers/isValidEmail'
import { isValidPassword } from 'helpers/isValidPassword'
import { useForm } from 'features/FormStore'
export const useValidateForm = () => {
const {
readFormValue,
updateFormError,
} = useForm()
const readTrimmedValue = (fieldName: string) => trim(readFormValue(fieldName))
const validateForm = () => {
let hasError = false
const firstname = readTrimmedValue(formIds.firstname)
const lastname = readTrimmedValue(formIds.lastname)
const email = readTrimmedValue(formIds.email)
const password = readTrimmedValue(formIds.password)
if (!firstname) {
updateFormError(formIds.firstname, 'error_fill_out_this_field')
hasError = true
}
if (!lastname) {
updateFormError(formIds.lastname, 'error_fill_out_this_field')
hasError = true
}
if (!email) {
updateFormError(formIds.email, 'error_empty_email')
hasError = true
} else if (!isValidEmail(email)) {
updateFormError(formIds.email, 'error_invalid_email_format')
hasError = true
}
if (!password) {
updateFormError(formIds.password, 'error_empty_password')
hasError = true
} else if (!isValidPassword(password)) {
updateFormError(formIds.password, 'error_simple_password')
hasError = true
}
return !hasError
}
return validateForm
}

@ -3,7 +3,6 @@ import React from 'react'
import { formIds } from 'config/form' import { formIds } from 'config/form'
import { T9n } from 'features/T9n' import { T9n } from 'features/T9n'
import { Combobox } from 'features/Combobox'
import { Input, ButtonSolid } from 'features/Common' import { Input, ButtonSolid } from 'features/Common'
import { Error } from 'features/Common/Input/styled' import { Error } from 'features/Common/Input/styled'
import { import {
@ -13,20 +12,14 @@ import {
} from 'features/Login/styled' } from 'features/Login/styled'
import { FormStore } from 'features/FormStore' import { FormStore } from 'features/FormStore'
import { useRegistrationForm } from 'hooks/useForm' import { useRegistrationForm } from './hooks'
const labelWidth = 116 const labelWidth = 116
const Registration = () => { const Registration = () => {
const { const {
cities,
countries,
handleSubmit, handleSubmit,
onCitySelect,
onCountrySelect,
onEmailBlur, onEmailBlur,
onPhoneBlur,
onRegionOrCityChange,
readFormError, readFormError,
readFormValue, readFormValue,
updateFormValue, updateFormValue,
@ -68,62 +61,6 @@ const Registration = () => {
labelWidth={labelWidth} labelWidth={labelWidth}
onChange={updateFormValue(formIds.password)} onChange={updateFormValue(formIds.password)}
/> />
<Combobox
withArrow
value={readFormValue(formIds.country)}
error={readFormError(formIds.country)}
labelLexic='form_country'
labelWidth={labelWidth}
options={countries}
onChange={updateFormValue(formIds.country)}
onSelect={onCountrySelect}
/>
<Input
value={readFormValue(formIds.region)}
error={readFormError(formIds.region)}
labelLexic='form_region'
labelWidth={labelWidth}
onChange={onRegionOrCityChange(formIds.region)}
/>
<Combobox
value={readFormValue(formIds.city)}
error={readFormError(formIds.city)}
labelLexic='form_city'
labelWidth={labelWidth}
onChange={onRegionOrCityChange(formIds.city)}
options={cities}
onSelect={onCitySelect}
/>
<Input
value={readFormValue(formIds.postalCode)}
error={readFormError(formIds.postalCode)}
labelLexic='form_postal_code'
labelWidth={labelWidth}
onChange={updateFormValue(formIds.postalCode)}
/>
<Input
value={readFormValue(formIds.address1)}
error={readFormError(formIds.address1)}
labelLexic='form_address1'
labelWidth={labelWidth}
onChange={updateFormValue(formIds.address1)}
/>
<Input
value={readFormValue(formIds.address2)}
error={readFormError(formIds.address2)}
labelLexic='form_address2'
labelWidth={labelWidth}
onChange={updateFormValue(formIds.address2)}
/>
<Input
value={readFormValue(formIds.phone)}
error={readFormError(formIds.phone)}
type='tel'
labelLexic='form_phone'
labelWidth={labelWidth}
onChange={updateFormValue(formIds.phone)}
onBlur={onPhoneBlur}
/>
<ButtonsBlock> <ButtonsBlock>
<ButtonSolid type='submit'> <ButtonSolid type='submit'>
<T9n t='next' /> <T9n t='next' />

@ -13,9 +13,8 @@ import type { UserInfo } from 'requests/saveUserInfo'
import { getUserInfo } from 'requests/getUserInfo' import { getUserInfo } from 'requests/getUserInfo'
import { saveUserInfo } from 'requests/saveUserInfo' import { saveUserInfo } from 'requests/saveUserInfo'
import type { SelectedCountry } from 'hooks/useCountries' import type { SelectedCountry } from './useCountries'
import { useRegistrationForm } from 'hooks/useForm' import { useUserInfoForm } from './useUserInfoForm'
import { useValidateForm } from './useValidateForm' import { useValidateForm } from './useValidateForm'
export const useUserInfo = () => { export const useUserInfo = () => {
@ -37,7 +36,7 @@ export const useUserInfo = () => {
selectedCity, selectedCity,
updateFormError, updateFormError,
updateFormValue, updateFormValue,
} = useRegistrationForm() } = useUserInfoForm()
const readTrimmedValue = useCallback( const readTrimmedValue = useCallback(
(fieldName: string) => trim(readFormValue(fieldName)), (fieldName: string) => trim(readFormValue(fieldName)),

@ -10,19 +10,18 @@ import { isValidEmail } from 'helpers/isValidEmail'
import { isValidPhone } from 'helpers/isValidPhone' import { isValidPhone } from 'helpers/isValidPhone'
import { formatPhoneCode } from 'helpers/formatPhoneCode' import { formatPhoneCode } from 'helpers/formatPhoneCode'
import type { SelectedCountry } from 'hooks/useCountries'
import { useCountries } from 'hooks/useCountries'
import { useCities } from 'hooks/useCities'
import { formIds } from 'config/form' import { formIds } from 'config/form'
import { useForm } from 'features/FormStore' import { useForm } from 'features/FormStore'
import { useSubmitHandler } from 'features/Register/components/RegistrationStep/hooks/useSubmitHandler'
import { useLexicsStore } from 'features/LexicsStore' import { useLexicsStore } from 'features/LexicsStore'
import type { SelectedCountry } from './useCountries'
import { useCountries } from './useCountries'
import { useCities } from './useCities'
type Name = 'name_rus' | 'name_eng' type Name = 'name_rus' | 'name_eng'
export const useRegistrationForm = () => { export const useUserInfoForm = () => {
const { const {
readFormError, readFormError,
readFormValue, readFormValue,
@ -45,10 +44,6 @@ export const useRegistrationForm = () => {
selectedCity, selectedCity,
} = useCities() } = useCities()
const handleSubmit = useSubmitHandler({
selectedCity,
selectedCountry,
})
const { suffix } = useLexicsStore() const { suffix } = useLexicsStore()
const onEmailBlur = useCallback(({ target }: FocusEvent<HTMLInputElement>) => { const onEmailBlur = useCallback(({ target }: FocusEvent<HTMLInputElement>) => {
@ -121,7 +116,6 @@ export const useRegistrationForm = () => {
cities, cities,
countries, countries,
getCities, getCities,
handleSubmit,
onCitySelect, onCitySelect,
onCountrySelect, onCountrySelect,
onEmailBlur, onEmailBlur,

@ -3,7 +3,6 @@ import React from 'react'
import { userAccountLexics } from 'config/lexics/userAccount' import { userAccountLexics } from 'config/lexics/userAccount'
import { formIds } from 'config/form' import { formIds } from 'config/form'
import { Background } from 'features/Background'
import { Combobox } from 'features/Combobox' import { Combobox } from 'features/Combobox'
import { Input } from 'features/Common' import { Input } from 'features/Common'
import { Form } from 'features/Login/styled' import { Form } from 'features/Login/styled'
@ -11,11 +10,11 @@ import { T9n } from 'features/T9n'
import { Error } from 'features/Common/Input/styled' import { Error } from 'features/Common/Input/styled'
import { useLexicsStore, useLexicsConfig } from 'features/LexicsStore' import { useLexicsStore, useLexicsConfig } from 'features/LexicsStore'
import { CardNumber } from './CardNumber' import { CardNumber } from './components/CardNumber'
import { UserAccountButton } from './UserAccountButton' import { UserAccountButton } from './components/UserAccountButton'
import { PageTitle } from './PageTitle' import { PageTitle } from './components/PageTitle'
import { UserAccountSubscription } from './UserAccountSubscription' import { UserAccountSubscription } from './components/UserAccountSubscription'
import { TextNoBorder } from './TextNoBorder' import { TextNoBorder } from './components/TextNoBorder'
import { useUserInfo } from './hooks' import { useUserInfo } from './hooks'
import { import {
@ -51,191 +50,189 @@ export const UserAccount = () => {
return ( return (
<UserAccountComponentWrapper> <UserAccountComponentWrapper>
<Background> <UserAccountWrapper>
<UserAccountWrapper> <PageTitle titleText={translate('user_account')} />
<PageTitle titleText={translate('user_account')} /> <UserAccountFormWrapper>
<UserAccountFormWrapper> <FormWrapper>
<FormWrapper> <Form>
<Form> <UserAccountBlockTitle>
<UserAccountBlockTitle> <T9n t='main' />
<T9n t='main' /> </UserAccountBlockTitle>
</UserAccountBlockTitle> <Input
<Input value={readFormValue(formIds.firstname)}
value={readFormValue(formIds.firstname)} labelLexic='name'
labelLexic='name' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.firstname)}
onChange={updateFormValue(formIds.firstname)} error={readFormError(formIds.firstname)}
error={readFormError(formIds.firstname)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <Input
<Input value={readFormValue(formIds.lastname)}
value={readFormValue(formIds.lastname)} labelLexic='lastname'
labelLexic='lastname' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.lastname)}
onChange={updateFormValue(formIds.lastname)} error={readFormError(formIds.lastname)}
error={readFormError(formIds.lastname)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <Input
<Input onBlur={onPhoneBlur}
onBlur={onPhoneBlur} value={readFormValue(formIds.phone)}
value={readFormValue(formIds.phone)} labelLexic='phone'
labelLexic='phone' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.phone)}
onChange={updateFormValue(formIds.phone)} error={readFormError(formIds.phone)}
error={readFormError(formIds.phone)} editIcon
editIcon maxLength={100}
maxLength={100} />
/> <Input
<Input value={readFormValue(formIds.password)}
value={readFormValue(formIds.password)} error={readFormError(formIds.password)}
error={readFormError(formIds.password)} type='password'
type='password' labelLexic='form_password'
labelLexic='form_password' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.password)}
onChange={updateFormValue(formIds.password)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <Combobox
<Combobox value={readFormValue(formIds.country)}
value={readFormValue(formIds.country)} error={readFormError(formIds.country)}
error={readFormError(formIds.country)} labelLexic='form_country'
labelLexic='form_country' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.country)}
onChange={updateFormValue(formIds.country)} options={countries}
options={countries} onSelect={onCountrySelect}
onSelect={onCountrySelect} withArrow
withArrow />
/> <Input
<Input value={readFormValue(formIds.postalCode)}
value={readFormValue(formIds.postalCode)} error={readFormError(formIds.postalCode)}
error={readFormError(formIds.postalCode)} labelLexic='form_postal_code'
labelLexic='form_postal_code' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.postalCode)}
onChange={updateFormValue(formIds.postalCode)} editIcon
editIcon />
/> <Input
<Input value={readFormValue(formIds.region)}
value={readFormValue(formIds.region)} error={readFormError(formIds.region)}
error={readFormError(formIds.region)} labelLexic='form_region'
labelLexic='form_region' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={onRegionOrCityChange(formIds.region)}
onChange={onRegionOrCityChange(formIds.region)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <Combobox
<Combobox value={readFormValue(formIds.city)}
value={readFormValue(formIds.city)} error={readFormError(formIds.city)}
error={readFormError(formIds.city)} labelLexic='form_city'
labelLexic='form_city' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={onRegionOrCityChange(formIds.city)}
onChange={onRegionOrCityChange(formIds.city)} options={cities}
options={cities} onSelect={onCitySelect}
onSelect={onCitySelect} maxLength={500}
maxLength={500} />
/> <Input
<Input value={readFormValue(formIds.address1)}
value={readFormValue(formIds.address1)} error={readFormError(formIds.address1)}
error={readFormError(formIds.address1)} labelLexic='form_address1'
labelLexic='form_address1' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.address1)}
onChange={updateFormValue(formIds.address1)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <Input
<Input value={readFormValue(formIds.address2)}
value={readFormValue(formIds.address2)} error={readFormError(formIds.address2)}
error={readFormError(formIds.address2)} labelLexic='form_address2'
labelLexic='form_address2' labelWidth={labelWidth}
labelWidth={labelWidth} onChange={updateFormValue(formIds.address2)}
onChange={updateFormValue(formIds.address2)} editIcon
editIcon maxLength={500}
maxLength={500} />
/> <ButtonWrapper>
<ButtonWrapper> <OutlinedButton
<OutlinedButton type='button'
type='button' onClick={handleSubmit}
onClick={handleSubmit} ref={saveButton}
ref={saveButton} >
> <T9n t='save_changes' />
<T9n t='save_changes' /> </OutlinedButton>
</OutlinedButton> <Error t={readFormError(formIds.formError) || ''} />
<Error t={readFormError(formIds.formError) || ''} /> </ButtonWrapper>
</ButtonWrapper> </Form>
</Form> </FormWrapper>
</FormWrapper> <FormWrapper>
<FormWrapper> <Form>
<Form> <UserAccountBlockTitle>
<UserAccountBlockTitle> <T9n t='payment' />
<T9n t='payment' /> </UserAccountBlockTitle>
</UserAccountBlockTitle> <CardNumber visa label='1234 1234 1234 1234' />
<CardNumber visa label='1234 1234 1234 1234' /> <CardNumber checked label='1234 1234 1234 1234' />
<CardNumber checked label='1234 1234 1234 1234' /> <CardNumber label='1234 1234 1234 1234' />
<CardNumber label='1234 1234 1234 1234' /> <UserAccountButton text={translate('add_card')} />
<UserAccountButton text={translate('add_card')} /> </Form>
</Form> </FormWrapper>
</FormWrapper> <FormWrapper>
<FormWrapper> <Form>
<Form> <UserAccountBlockTitle>
<UserAccountBlockTitle> <T9n t='subscriptions' />
<T9n t='subscriptions' /> </UserAccountBlockTitle>
</UserAccountBlockTitle> <UserAccountSubscription
<UserAccountSubscription amount={1999}
amount={1999} checked
checked inputType='radio'
inputType='radio' packageName='Базовая'
packageName='Базовая' packageAction={translate('add_card')}
packageAction={translate('add_card')} />
/> <UserAccountSubscription
<UserAccountSubscription noMarginBottom
noMarginBottom amount={1999}
amount={1999} checked
checked inputType='checkbox'
inputType='checkbox' label='Российская Премьер-Лига'
label='Российская Премьер-Лига' />
/> <UserAccountSubscription
<UserAccountSubscription noMarginTop
noMarginTop noMarginBottom
noMarginBottom amount={499}
amount={499} checked
checked inputType='checkbox'
inputType='checkbox' label='Primera División'
label='Primera División' />
/> <UserAccountSubscription
<UserAccountSubscription noMarginTop
noMarginTop noMarginBottom
noMarginBottom amount={499}
amount={499} checked
checked inputType='checkbox'
inputType='checkbox' label='Primera División'
label='Primera División' />
/> <UserAccountSubscription
<UserAccountSubscription noMarginTop
noMarginTop noMarginBottom
noMarginBottom amount={499}
amount={499} checked
checked inputType='checkbox'
inputType='checkbox' label='Primera División'
label='Primera División' />
/> <UserAccountSubscription
<UserAccountSubscription noMarginTop
noMarginTop amount={299}
amount={299} checked
checked inputType='checkbox'
inputType='checkbox' label='Manchester United'
label='Manchester United' />
/> <UserAccountButton text={translate('select_subscription')} />
<UserAccountButton text={translate('select_subscription')} /> <TextNoBorder
<TextNoBorder text={`${translate('next_debit')} 31.02.2020`}
text={`${translate('next_debit')} 31.02.2020`} amount={4796}
amount={4796} />
/> </Form>
</Form> </FormWrapper>
</FormWrapper> </UserAccountFormWrapper>
</UserAccountFormWrapper> </UserAccountWrapper>
</UserAccountWrapper>
</Background>
</UserAccountComponentWrapper> </UserAccountComponentWrapper>
) )
} }

@ -2,15 +2,11 @@ import { isValidPassword } from '..'
it('invalid passwords', () => { it('invalid passwords', () => {
expect(isValidPassword('')).toBeFalsy() expect(isValidPassword('')).toBeFalsy()
expect(isValidPassword('a')).toBeFalsy()
expect(isValidPassword('1')).toBeFalsy()
expect(isValidPassword('abcdef')).toBeFalsy()
expect(isValidPassword('abcdef@')).toBeFalsy()
expect(isValidPassword('abcdef@123')).toBeFalsy()
expect(isValidPassword('ABcd12$')).toBeFalsy()
}) })
it('valid passwords', () => { it('valid passwords', () => {
expect(isValidPassword('a')).toBeTruthy()
expect(isValidPassword('1')).toBeTruthy()
expect(isValidPassword('aASbc!def123$')).toBeTruthy() expect(isValidPassword('aASbc!def123$')).toBeTruthy()
expect(isValidPassword('Abcdef@123$')).toBeTruthy() expect(isValidPassword('Abcdef@123$')).toBeTruthy()
expect(isValidPassword('Abcd1234')).toBeTruthy() expect(isValidPassword('Abcd1234')).toBeTruthy()

@ -1,13 +1,6 @@
export const passwordRegex = '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,500}$' import trim from 'lodash/trim'
import isEmpty from 'lodash/isEmpty'
const passwordRegExp = new RegExp(passwordRegex)
/**
* At least one upper case English letter, (?=.*?[A-Z])
* At least one lower case English letter, (?=.*?[a-z])
* At least one digit, (?=.*?[0-9])
* Minimum eight in length .{8,} (with the anchors)
*/
export const isValidPassword = (password: string) => ( export const isValidPassword = (password: string) => (
passwordRegExp.test(password) !isEmpty(trim(password))
) )

@ -1,69 +0,0 @@
import trim from 'lodash/trim'
import reduce from 'lodash/reduce'
import { useForm } from 'features/FormStore'
import { isValidEmail } from 'helpers/isValidEmail'
import { isValidPassword } from 'helpers/isValidPassword'
import { isValidPhone } from 'helpers/isValidPhone'
import {
formIds,
requiredFields,
simpleValidationFields,
} from 'config/form'
export const useValidateForm = () => {
const {
allFieldsEmpty,
isFieldEmpty,
readFormValue,
updateFormError,
} = useForm()
const readTrimmedValue = (fieldName: string) => trim(readFormValue(fieldName))
const setErrorOnEmptyFields = (fieldNames: Array<string>, message: string) => (
reduce(
fieldNames,
(acc, fieldName) => {
if (isFieldEmpty(fieldName)) {
updateFormError(fieldName, message)
return true
}
return acc
},
false,
)
)
const validateForm = () => {
let hasError = false
const email = readTrimmedValue(formIds.email)
const password = readTrimmedValue(formIds.password)
const phone = readTrimmedValue(formIds.phone)
if (allFieldsEmpty(requiredFields)) {
updateFormError(formIds.formError, 'error_fill_out_required_fields')
setErrorOnEmptyFields(requiredFields, '')
return false
}
hasError = setErrorOnEmptyFields(simpleValidationFields, 'error_fill_out_this_field')
if (isFieldEmpty(formIds.email)) {
updateFormError(formIds.email, 'error_empty_email')
hasError = true
} else if (!isValidEmail(email)) {
updateFormError(formIds.email, 'error_invalid_email_format')
hasError = true
}
if (!isValidPassword(password)) {
updateFormError(formIds.password, 'error_simple_password')
hasError = true
}
if (!isValidPhone(phone)) {
updateFormError(formIds.phone, 'error_invalid_phone_format')
hasError = true
}
return !hasError
}
return validateForm
}

@ -14,49 +14,25 @@ type Response = {
} }
type Args = { type Args = {
address1: string,
address2?: string,
city?: string,
cityId?: number,
countryId: number,
email: string, email: string,
firstname: string, firstname: string,
lastname: string, lastname: string,
password: string, password: string,
phone: string,
postalCode: number,
region: string,
} }
export const register = async ({ export const register = async ({
address1,
address2,
city,
cityId,
countryId,
email, email,
firstname, firstname,
lastname, lastname,
password, password,
phone,
postalCode,
region,
}: Args) => { }: Args) => {
const config = { const config = {
body: { body: {
params: { params: {
_p_address_line1: address1,
_p_address_line2: address2,
_p_city: city,
_p_city_id: cityId,
_p_country_id: countryId,
_p_email: email, _p_email: email,
_p_firstname: firstname, _p_firstname: firstname,
_p_lastname: lastname, _p_lastname: lastname,
_p_password: password, _p_password: password,
_p_phone: phone,
_p_postal_code: postalCode,
_p_region: region,
}, },
proc, proc,
}, },

Loading…
Cancel
Save