|
|
|
|
@ -1,4 +1,4 @@ |
|
|
|
|
import type { ChangeEvent, FocusEvent } from 'react' |
|
|
|
|
import type { ChangeEvent } from 'react' |
|
|
|
|
import { |
|
|
|
|
useEffect, |
|
|
|
|
useCallback, |
|
|
|
|
@ -6,14 +6,12 @@ import { |
|
|
|
|
|
|
|
|
|
import trim from 'lodash/trim' |
|
|
|
|
|
|
|
|
|
import { isValidEmail } from 'helpers/isValidEmail' |
|
|
|
|
import { isValidPhone } from 'helpers/isValidPhone' |
|
|
|
|
import { formatPhoneCode } from 'helpers/formatPhoneCode' |
|
|
|
|
|
|
|
|
|
import { formIds } from 'config/form' |
|
|
|
|
|
|
|
|
|
import { useForm } from 'features/FormStore' |
|
|
|
|
import { useLexicsStore } from 'features/LexicsStore' |
|
|
|
|
|
|
|
|
|
import type { SelectedCountry } from './useCountries' |
|
|
|
|
import { useCountries } from './useCountries' |
|
|
|
|
@ -44,15 +42,6 @@ export const useUserInfoForm = () => { |
|
|
|
|
selectedCity, |
|
|
|
|
} = useCities() |
|
|
|
|
|
|
|
|
|
const { suffix } = useLexicsStore() |
|
|
|
|
|
|
|
|
|
const onEmailBlur = useCallback(({ target }: FocusEvent<HTMLInputElement>) => { |
|
|
|
|
const email = trim(target.value) |
|
|
|
|
if (email && !isValidEmail(email)) { |
|
|
|
|
updateFormError(formIds.email, 'error_invalid_email_format') |
|
|
|
|
} |
|
|
|
|
}, [updateFormError]) |
|
|
|
|
|
|
|
|
|
const onPhoneBlur = useCallback(({ target }: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
const phone = target.value |
|
|
|
|
if (phone && !isValidPhone(phone)) { |
|
|
|
|
@ -60,33 +49,40 @@ export const useUserInfoForm = () => { |
|
|
|
|
} |
|
|
|
|
}, [updateFormError]) |
|
|
|
|
|
|
|
|
|
const onCountrySelect = useCallback(( |
|
|
|
|
country: SelectedCountry, |
|
|
|
|
updatePhoneCode: boolean = true, |
|
|
|
|
) => { |
|
|
|
|
const phone = trim(readFormValue(formIds.phone)) |
|
|
|
|
const onCountrySelect = useCallback((country: SelectedCountry) => { |
|
|
|
|
if (country?.id === selectedCountry?.id) return |
|
|
|
|
|
|
|
|
|
setSelectedCountry(country) |
|
|
|
|
const countryName = country ? country[`name_${suffix}` as Name] : '' |
|
|
|
|
updateFormValue(formIds.country)(countryName) |
|
|
|
|
|
|
|
|
|
updateFormValue(formIds.countryId)(country?.id ? String(country?.id) : '') |
|
|
|
|
updateFormValue(formIds.cityId)('') |
|
|
|
|
updateFormValue(formIds.countryId)(`${country?.id}`) |
|
|
|
|
resetCities() |
|
|
|
|
resetSelectedCity() |
|
|
|
|
|
|
|
|
|
if (updatePhoneCode) { |
|
|
|
|
const code = country?.phone_code |
|
|
|
|
? formatPhoneCode(country.phone_code) |
|
|
|
|
: '' |
|
|
|
|
const selectedCountryCode = formatPhoneCode(selectedCountry?.phone_code || '') |
|
|
|
|
const hasPhoneNumber = ( |
|
|
|
|
phone |
|
|
|
|
&& selectedCountryCode |
|
|
|
|
&& phone !== selectedCountryCode |
|
|
|
|
) |
|
|
|
|
if (!hasPhoneNumber) { |
|
|
|
|
const code = formatPhoneCode(country?.phone_code || '') |
|
|
|
|
updateFormValue(formIds.phone)(code) |
|
|
|
|
} |
|
|
|
|
}, [ |
|
|
|
|
phone, |
|
|
|
|
selectedCountry, |
|
|
|
|
resetCities, |
|
|
|
|
resetSelectedCity, |
|
|
|
|
setSelectedCountry, |
|
|
|
|
updateFormValue, |
|
|
|
|
suffix, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
const onCountryBlur = () => { |
|
|
|
|
updateFormValue(formIds.country)(selectedCountry?.name || '') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onRegionOrCityChange = useCallback((fieldName: string) => ( |
|
|
|
|
({ target }: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
if (selectedCountry) { |
|
|
|
|
@ -115,18 +111,16 @@ export const useUserInfoForm = () => { |
|
|
|
|
return { |
|
|
|
|
cities, |
|
|
|
|
countries, |
|
|
|
|
getCities, |
|
|
|
|
onCitySelect, |
|
|
|
|
onCountryBlur, |
|
|
|
|
onCountrySelect, |
|
|
|
|
onEmailBlur, |
|
|
|
|
onPhoneBlur, |
|
|
|
|
onRegionOrCityChange, |
|
|
|
|
readFormError, |
|
|
|
|
readFormValue, |
|
|
|
|
resetCities, |
|
|
|
|
resetSelectedCity, |
|
|
|
|
selectedCity, |
|
|
|
|
updateFormError, |
|
|
|
|
selectedCountry, |
|
|
|
|
setSelectedCountry, |
|
|
|
|
updateFormValue, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|