Last active
April 17, 2017 14:07
-
-
Save timcase/aa59a355316e621287c1d560358945bf to your computer and use it in GitHub Desktop.
Todo mutation
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
handleCompletedClick = () => { | |
const originalTodo = Object.assign({}, this.props.todo); | |
let todo = this.props.todo; | |
todo.completed = true | |
this.props.update(todo, originalTodo); | |
} | |
export const updateTodo = (todo, originalTodo) => { | |
const url = 'http://localhost:3001/todos/' + todo.id; | |
return (dispatch, state) => { | |
dispatch(sendUpdateTodo(true)); | |
dispatch(modifyTodo(todo)); | |
fetch(url,{ | |
method: 'PUT', | |
credentials: 'include', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${state().authentication.jwt}` | |
}, | |
body: JSON.stringify({ | |
id: todo.id, | |
text: todo.text, | |
completed: todo.completed, | |
tag: todo.tag | |
}) | |
}) | |
.then(utils.checkStatus) | |
.then(utils.parseJSON) | |
.then((todo) => { | |
dispatch(sendUpdateTodo(false)); | |
dispatch(modifyTodo(todo)); | |
}) | |
.catch((error) => { | |
dispatch(sendUpdateIsFailureTodo(error)); | |
dispatch(modifyTodo(originalTodo)); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment