Skip to content

Instantly share code, notes, and snippets.

@daanpeer
Last active April 11, 2018 12:42
Show Gist options
  • Save daanpeer/e86d713b88b3579e96b55aebc25d16d8 to your computer and use it in GitHub Desktop.
Save daanpeer/e86d713b88b3579e96b55aebc25d16d8 to your computer and use it in GitHub Desktop.
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