Skip to content

Instantly share code, notes, and snippets.

@kaueDM
Created September 3, 2019 21:14
Show Gist options
  • Save kaueDM/b2f41ed95b463323cf7b40ea80ad2bcc to your computer and use it in GitHub Desktop.
Save kaueDM/b2f41ed95b463323cf7b40ea80ad2bcc to your computer and use it in GitHub Desktop.
import * as Yup from 'yup'
import { cep } from 'utils/textMask'
import { TextInput } from 'components'
import { AddressService } from 'services'
import removeMask from 'utils/removeMask'
const _fillAddress = async (inputProps: Record<string, any>) => {
const { value: zipCode, setFieldValue } = inputProps
const response = await AddressService(removeMask(zipCode))
if (response.id) {
setFieldValue('address', response.address)
setFieldValue('district', response.district)
setFieldValue('city', response.city)
setFieldValue('state', response.state)
setFieldValue('country', 'Brasil')
}
}
const schema = [
{
name: 'zipCode',
label: 'CEP',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
mask: cep,
applyMarginBottom: true,
blurEvent: async (inputProps: Record<string, any>) => {
await _fillAddress(inputProps)
}
},
{
name: 'address',
label: 'Logradouro',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'number',
label: 'Número',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'district',
label: 'Bairro',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'complement',
label: 'Complemento (opcional)',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'city',
label: 'Cidade',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'state',
label: 'Estado',
validation: Yup.string().required('Obrigatório'),
component: TextInput,
applyMarginBottom: true
},
{
name: 'country',
label: 'País',
validation: Yup.string().required('Obrigatório'),
component: TextInput
}
]
export default schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment