Created
August 22, 2016 21:27
-
-
Save jwo/f21fdbbfabd0019fed38b0d5ebd3ee9a to your computer and use it in GitHub Desktop.
Getting React and Redux React and Rails and Fetch and Safari to play nice.
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 function saveOfficeStaff(section, staff) { | |
return (dispatch, getState) => { | |
const issue = getState().newsletterId; | |
const theData = { | |
staff: staff, | |
section_id: section.id | |
} | |
fetch(`/api/newsletters/${issue}/office_staff`, { | |
method: 'POST', | |
credentials: 'include', | |
body: JSON.stringify(theData), | |
headers: { | |
'Content-Type': 'application/json', | |
'Accepts': 'application/json', | |
'X-CSRF-Token': metaTagsManager.getCSRFToken(), | |
} | |
}) | |
// handle if 422 | |
.then( (response) => { | |
if (response.ok) { | |
dispatch(refreshNewsletter()) | |
dispatch(successMessage("Saved Correctly")) | |
} else { | |
response.json().then( (json) => { | |
const errorSentences = Object.keys(json.errors).map( function(keyName) { | |
return keyName + " " + json.errors[keyName].join(", "); | |
}); | |
dispatch(errorMessage(errorSentences.join(". "))) | |
}) | |
} | |
}) | |
} |
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
"exports-loader": "^0.6.3", | |
"imports-loader": "^0.6.5", | |
"whatwg-fetch": "^1.0.0" |
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
plugins: [ | |
new webpack.ProvidePlugin({ | |
'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch' | |
}) | |
], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment