Last active
July 19, 2017 01:46
-
-
Save bradparker/bbb8b396eed9b0b98e989300b4507f6f to your computer and use it in GitHub Desktop.
One update cycle on init / success / failure of a network request.
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 finishRegistration = ({ id, attributes }) => (dispatch) => { | |
dispatch({ | |
type: 'FINISH_REGISTRATION', | |
payload: { id } | |
}) | |
return finish({ id, attributes }).then(({ | |
registration, | |
personalCampaign, | |
account | |
}) => { | |
dispatch(finishRegistrationSuccess({ | |
registration, | |
personalCampaign, | |
account | |
})) | |
return { | |
registration, | |
personalCampaign, | |
account | |
} | |
}) | |
} | |
const finishRegistrationSuccess = ({ | |
registration, | |
personalCampaign, | |
account | |
}) => ({ | |
type: 'FINISH_REGISTRATION_SUCCESS', | |
payload: { | |
registration, | |
personalCampaign, | |
account | |
} | |
}) | |
const finishRegistrationFailure = ({ | |
error, | |
registration, | |
personalCampaign, | |
account | |
}) => ({ | |
type: 'FINISH_REGISTRATION_FAILURE', | |
error, | |
payload: { | |
registration, | |
personalCampaign, | |
account | |
} | |
}) | |
const personalCampaignsReducer = (state = {}, { type, payload, error }) => { | |
switch (type) { | |
case 'FINISH_REGISTRATION_SUCCESS': { | |
const { personalCampaign } = payload | |
// ... etc | |
} | |
// ... etc | |
} | |
} | |
const accountsReducer = (state = {}, { type, payload, error }) => { | |
switch (type) { | |
case 'FINISH_REGISTRATION_SUCCESS': { | |
const { account } = payload | |
// ... etc | |
} | |
// ... etc | |
} | |
} |
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
const fetchFoo = (id) => ( | |
get(`http://url/foos/${id}`).then(({ data }) => (data)) | |
) | |
const fetchBar = (id) => ( | |
get(`http://url/bars/${id}`).then(({ data }) => (data)) | |
) | |
const fetchFooBar = ({ fooId, barId }) => ( | |
Promise.all([ | |
fetchFoo(fooId), | |
fetchBar(barId) | |
]).then(([foo, bar]) => ({ | |
foo, | |
bar | |
})) | |
) | |
const fetchRequiredData = (ids) => (dispatch) => { | |
dispatch({ type: 'FETCH_REQUIRED_DATA_REQUEST', payload: ids }) | |
return fetchFooBar(ids).then(payload => { | |
dispatch({ type: 'FETCH_REQUIRED_DATA_SUCCESS', payload }) | |
return result | |
}).catch(error => { | |
dispatch({ type: 'FETCH_REQUIRED_DATA_FAILURE', error }) | |
return Promise.reject(error) | |
}) | |
} | |
const foosReducer = (state = {}, { type, payload, error }) => { | |
switch (type) { | |
case 'FETCH_REQUIRED_DATA': { | |
// update the specific foo in the store! | |
} | |
// ... etc | |
} | |
} | |
const barsReducer = (state = {}, { type, payload, error }) => { | |
switch (type) { | |
case 'FETCH_REQUIRED_DATA': { | |
// update the specific bar in the store! | |
} | |
// ... etc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment