Last active
April 11, 2018 12:42
-
-
Save daanpeer/e86d713b88b3579e96b55aebc25d16d8 to your computer and use it in GitHub Desktop.
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
export const fetchAddress = ( | |
fieldName: string, | |
): Function => | |
async (dispatch, getState) => { | |
const state = getState(); | |
const zipcode = selector(state, `${fieldName}.zipcode`); | |
const housenumber = selector(state, `${fieldName}.housenumber`); | |
if (!zipcode || !housenumber) { | |
return; | |
} | |
try { | |
const data = await fetchData({ | |
endpoint: '/check-zipcode', | |
method: 'POST', | |
body: { | |
zipcode, | |
housenumber, | |
}, | |
}); | |
// change the field to the newly retrieved values | |
dispatch(change('order', `${fieldName}.city`, data.city)); | |
dispatch(change('order', `${fieldName}.street`, data.street)); | |
} catch (err) { | |
console.log('Error fetching zipcode', err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment