Last active
November 11, 2017 08:17
-
-
Save sujinleeme/d2baf44eb7630b3dc160430471c0aab5 to your computer and use it in GitHub Desktop.
learnPromise.js
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
const mapDispatchToProps = (dispatch) => { | |
return { | |
deleteCommentBodyContent: (id, category, parentId) => { | |
dispatch(deleteCommentContent(id)) | |
.then(() => dispatch(getComments(parentId))) | |
} | |
} | |
// actions | |
export const deleteCommentContent = (id) => { | |
return (dispatch) => new Promise((res, reject) => { | |
dispatch(deleteComment()) | |
fetch(`${baseurl}/comments/${id}`, { | |
method: 'PUT', headers: headers, body: JSON.stringify({deleted: true}), | |
}).then((response) => { | |
if (!response.ok) { | |
throw Error(response.statusText) | |
} | |
dispatch(deleteCommentSuccess(true)) | |
return response | |
}) | |
.then((response) => response.json()) | |
.catch(() => dispatch(deleteCommentFailure())) | |
}) | |
} | |
export const getComments = (id) => { | |
return (dispatch) => new Promise((resolve, reject) => { | |
dispatch(fetchComments()) | |
fetch(`${baseurl}/posts/${id}/comments`, {headers}).then((response) => { | |
if (!response.ok) { | |
throw Error(response.statusText) | |
} | |
return response | |
}).then((response) => response.json()) | |
.then((comments) => dispatch(fetchCommentsSuccess(comments))) | |
.catch((response) => dispatch(fetchCommentsFailure())) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment