Skip to content

Instantly share code, notes, and snippets.

@timcase
Last active April 17, 2017 14:07
Show Gist options
  • Save timcase/aa59a355316e621287c1d560358945bf to your computer and use it in GitHub Desktop.
Save timcase/aa59a355316e621287c1d560358945bf to your computer and use it in GitHub Desktop.
Todo mutation
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