Skip to content

Instantly share code, notes, and snippets.

@sujinleeme
Last active November 11, 2017 08:17
Show Gist options
  • Save sujinleeme/d2baf44eb7630b3dc160430471c0aab5 to your computer and use it in GitHub Desktop.
Save sujinleeme/d2baf44eb7630b3dc160430471c0aab5 to your computer and use it in GitHub Desktop.
learnPromise.js
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