Skip to content

Instantly share code, notes, and snippets.

@jwo
Created August 22, 2016 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwo/f21fdbbfabd0019fed38b0d5ebd3ee9a to your computer and use it in GitHub Desktop.
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.
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(". ")))
})
}
})
}
import _ from 'lodash';
export default {
/**
* Get CSRF Token from the DOM.
*
* @returns {String} - CSRF Token.
*/
getCSRFToken() {
const token = _.find(document.querySelectorAll('meta'), ['name', 'csrf-token']);
return token ? token.content : null;
},
};
"exports-loader": "^0.6.3",
"imports-loader": "^0.6.5",
"whatwg-fetch": "^1.0.0"
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