Skip to content

Instantly share code, notes, and snippets.

@timbuckley
Created September 22, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbuckley/bf695657f0fecccf7d23a42b5fa195a7 to your computer and use it in GitHub Desktop.
Save timbuckley/bf695657f0fecccf7d23a42b5fa195a7 to your computer and use it in GitHub Desktop.
// === Action Creators ===
export function posItemRequest(itemBody) {
return {
type: 'ITEM_POST_REQUEST',
payload: {
itemBody: itemBody
}
}
}
export function posItemSuccess(itemBody) {
return {
type: 'ITEM_POST_SUCCESS',
payload: {
itemBody: itemBody
}
}
}
export function posItemFail(errorMsg) {
return {
type: 'ITEM_POST_FAIL',
payload: {
errorMsg: errorMsg
}
}
}
// === Thunk ===
export function postItem(itemBody, userToken) {
const options = {
method: 'POST',
body: JSON.stringify(itemBody)
headers: {'User-Token': userToken}
}
return (dispatch, getState) => {
return fetch('http://www.urlhere.com', options)
.then(response => response.json())
.then(json => dispatch(posItemSuccess(itemBody)))
.catch(err => dispatch(posItemFail(err.message)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment