-
Notifications
You must be signed in to change notification settings - Fork 378
Use fetch instead of $.ajax #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "preset": "airbnb", | ||
| "fileExtensions": [".js", ".jsx"], | ||
| "excludeFiles": ["build/**", "node_modules/**"] | ||
| "excludeFiles": ["build/**", "node_modules/**"], | ||
| "disallowQuotedKeysInObjects": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,9 @@ export function submitCommentFailure(error) { | |
|
|
||
| export function fetchComments() { | ||
| return dispatch => { | ||
| return CommentsManager.fetchComments().then( | ||
| comments => dispatch(fetchCommentsSuccess(comments)), | ||
| error => dispatch(fetchCommentsFailure(error)) | ||
| ); | ||
| return CommentsManager.fetchComments() | ||
| .then(comments => dispatch(fetchCommentsSuccess(comments))) | ||
| .catch(error => dispatch(fetchCommentsFailure(error))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| }; | ||
| } | ||
|
|
||
|
|
@@ -58,9 +57,8 @@ export function submitComment(comment) { | |
| return dispatch => { | ||
| dispatch(incrementAjaxCounter()); | ||
| return CommentsManager.submitComment(comment) | ||
| .then( | ||
| _comment => dispatch(submitCommentSuccess(_comment)), | ||
| error => dispatch(submitCommentFailure(error))) | ||
| .then(commentFromServer => dispatch(submitCommentSuccess(commentFromServer))) | ||
| .catch(error => dispatch(submitCommentFailure(error))) | ||
| .then(() => dispatchDecrementAjaxCounter(dispatch)); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,48 @@ | ||
| import $ from 'jquery'; | ||
|
|
||
| const API_URL = 'comments.json'; | ||
|
|
||
| // fetch won't reject on HTTP error status | ||
| // https://github.com/github/fetch#handling-http-error-statuses | ||
| function checkStatus(response) { | ||
| if (response.status >= 200 && response.status < 300) { | ||
| return response; | ||
| } | ||
|
|
||
| const error = new Error(response.statusText); | ||
| error.response = response; | ||
| throw error; | ||
| } | ||
|
|
||
| function parseJSON(response) { | ||
| return response.json(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this does a parsing and throws if there is an error. |
||
| } | ||
|
|
||
| const CommentsManager = { | ||
|
|
||
| /** | ||
| * Retrieve comments from server using AJAX call. | ||
| * | ||
| * @returns {Promise} - jqXHR result of ajax call. | ||
| * @returns {Promise} - response of fetch request. | ||
| */ | ||
| fetchComments() { | ||
| return Promise.resolve($.ajax({ | ||
| url: API_URL, | ||
| dataType: 'json', | ||
| })); | ||
| return fetch(API_URL).then(checkStatus).then(parseJSON); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. The caller is responsible for handling the promise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see how others use a global polyfill for fetch, but I think this is really awkward. I'd rather see something like what lodash has. The way that webpack has to use both the import and export loaders is a real brain twister. I get it and am posting to the forum on how this works. However, this sort of goes against my philosophy of "Don't make me think". Looking for more discussion on this one. @alexfedoseev ? @skv-headless ? |
||
| }, | ||
|
|
||
| /** | ||
| * Submit new comment to server using AJAX call. | ||
| * | ||
| * @param {Object} comment - Comment body to post. | ||
| * @returns {Promise} - jqXHR result of ajax call. | ||
| * @returns {Promise} - response of fetch request. | ||
| */ | ||
|
|
||
| submitComment(comment) { | ||
| return Promise.resolve($.ajax({ | ||
| url: API_URL, | ||
| dataType: 'json', | ||
| type: 'POST', | ||
| data: {comment: comment}, | ||
| })); | ||
| return fetch(API_URL, { | ||
| method: 'post', | ||
| headers: { | ||
| 'Accept': 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({comment}), | ||
| }).then(checkStatus).then(parseJSON); | ||
| }, | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,8 @@ | |
| "redux-promise": "^0.5.0", | ||
| "redux-thunk": "^0.1.0", | ||
| "sleep": "^3.0.0", | ||
| "webpack": "^1.10.5" | ||
| "webpack": "^1.10.5", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexfedoseev @mapreal19 I'm pretty sure some of these dependencies are not needed for the rails deployment. It would be good to see see if the hot only dependencies were moved to devDependencies. The problem with the build is copied here: CC: @dylangrafmyre |
||
| "whatwg-fetch": "^0.9.0" | ||
| }, | ||
| "devDependencies": { | ||
| "babel-eslint": "^4.0.5", | ||
|
|
||