Skip to content

Commit 771ce17

Browse files
committed
Merge pull request shakacode#78 from shakacode/revert-73-fetch-for-async-calls
Revert "Use fetch instead of $.ajax"
2 parents 8f1f8d2 + 72a424b commit 771ce17

File tree

5 files changed

+24
-38
lines changed

5 files changed

+24
-38
lines changed

client/.jscsrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"preset": "airbnb",
33
"fileExtensions": [".js", ".jsx"],
4-
"excludeFiles": ["build/**", "node_modules/**"],
5-
"disallowQuotedKeysInObjects": false
4+
"excludeFiles": ["build/**", "node_modules/**"]
65
}

client/assets/javascripts/actions/CommentActionCreators.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ export function submitCommentFailure(error) {
4343

4444
export function fetchComments() {
4545
return dispatch => {
46-
return CommentsManager.fetchComments()
47-
.then(comments => dispatch(fetchCommentsSuccess(comments)))
48-
.catch(error => dispatch(fetchCommentsFailure(error)));
46+
return CommentsManager.fetchComments().then(
47+
comments => dispatch(fetchCommentsSuccess(comments)),
48+
error => dispatch(fetchCommentsFailure(error))
49+
);
4950
};
5051
}
5152

@@ -57,8 +58,9 @@ export function submitComment(comment) {
5758
return dispatch => {
5859
dispatch(incrementAjaxCounter());
5960
return CommentsManager.submitComment(comment)
60-
.then(commentFromServer => dispatch(submitCommentSuccess(commentFromServer)))
61-
.catch(error => dispatch(submitCommentFailure(error)))
61+
.then(
62+
_comment => dispatch(submitCommentSuccess(_comment)),
63+
error => dispatch(submitCommentFailure(error)))
6264
.then(() => dispatchDecrementAjaxCounter(dispatch));
6365
};
6466
}

client/assets/javascripts/utils/CommentsManager.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
1-
const API_URL = 'comments.json';
2-
3-
// fetch won't reject on HTTP error status
4-
// https://github.com/github/fetch#handling-http-error-statuses
5-
function checkStatus(response) {
6-
if (response.status >= 200 && response.status < 300) {
7-
return response;
8-
}
9-
10-
const error = new Error(response.statusText);
11-
error.response = response;
12-
throw error;
13-
}
1+
import $ from 'jquery';
142

15-
function parseJSON(response) {
16-
return response.json();
17-
}
3+
const API_URL = 'comments.json';
184

195
const CommentsManager = {
206

217
/**
228
* Retrieve comments from server using AJAX call.
239
*
24-
* @returns {Promise} - response of fetch request.
10+
* @returns {Promise} - jqXHR result of ajax call.
2511
*/
2612
fetchComments() {
27-
return fetch(API_URL).then(checkStatus).then(parseJSON);
13+
return Promise.resolve($.ajax({
14+
url: API_URL,
15+
dataType: 'json',
16+
}));
2817
},
2918

3019
/**
3120
* Submit new comment to server using AJAX call.
3221
*
3322
* @param {Object} comment - Comment body to post.
34-
* @returns {Promise} - response of fetch request.
23+
* @returns {Promise} - jqXHR result of ajax call.
3524
*/
36-
3725
submitComment(comment) {
38-
return fetch(API_URL, {
39-
method: 'post',
40-
headers: {
41-
'Accept': 'application/json',
42-
'Content-Type': 'application/json',
43-
},
44-
body: JSON.stringify({comment}),
45-
}).then(checkStatus).then(parseJSON);
26+
return Promise.resolve($.ajax({
27+
url: API_URL,
28+
dataType: 'json',
29+
type: 'POST',
30+
data: {comment: comment},
31+
}));
4632
},
4733
};
4834

client/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"redux-promise": "^0.5.0",
4141
"redux-thunk": "^0.1.0",
4242
"sleep": "^3.0.0",
43-
"webpack": "^1.10.5",
44-
"whatwg-fetch": "^0.9.0"
43+
"webpack": "^1.10.5"
4544
},
4645
"devDependencies": {
4746
"babel-eslint": "^4.0.5",

client/webpack.common.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66

77
// the project dir
88
context: __dirname,
9-
entry: ['whatwg-fetch', './assets/javascripts/App'],
9+
entry: ['./assets/javascripts/App'],
1010

1111
// In case you wanted to load jQuery from the CDN, this is how you would do it:
1212
// externals: {

0 commit comments

Comments
 (0)