-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add asynchronous validation for ZipCode input
Related to issue #4 Signed-off-by: antoniocoj <[email protected]> Signed-off-by: Lucas Amoêdo <[email protected]>
- Loading branch information
1 parent
ff6b44b
commit 46fdb48
Showing
3 changed files
with
74 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import $ from 'jquery'; | ||
import { change } from 'redux-form'; | ||
|
||
const fillAddressFields = (address, dispatch) => { | ||
dispatch( | ||
change( | ||
'record-form', | ||
'publicPlace', | ||
`${address.tipoDeLogradouro} ${address.logradouro}` | ||
) | ||
); | ||
dispatch( | ||
change( | ||
'record-form', | ||
'neighborhood', | ||
address.bairro | ||
) | ||
); | ||
dispatch( | ||
change( | ||
'record-form', | ||
'city', | ||
address.cidade | ||
) | ||
); | ||
dispatch( | ||
change( | ||
'record-form', | ||
'district', | ||
address.estado | ||
) | ||
); | ||
dispatch( | ||
change( | ||
'record-form', | ||
'addressNumber', | ||
'' | ||
) | ||
); | ||
dispatch( | ||
change( | ||
'record-form', | ||
'complement', | ||
'' | ||
) | ||
); | ||
|
||
} | ||
|
||
export const AsyncValidate = (values, dispatch, props, blurredField) => { | ||
const zipCode = values.zipCode.replace(/[^\d]/, ''); | ||
return $.ajax({ | ||
url: `http://correiosapi.apphb.com/cep/${zipCode}`, | ||
dataType: 'jsonp', | ||
crossDomain: true, | ||
contentType: 'application/json' | ||
}).then( | ||
(response) => { | ||
fillAddressFields(response, dispatch); | ||
}, | ||
(f) => { | ||
const error = {zipCode: 'CEP não encontrado'}; | ||
throw error; | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters