Guest User

Untitled

a guest
Apr 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import React from 'react';
  2. import { Mutation } from "react-apollo";
  3. import '../../styles/Todo.css';
  4.  
  5. import {
  6. QUERY_TODO,
  7. MUTATION_TODO_UPDATE,
  8. MUTATION_TODO_DELETE
  9. } from '../../graphQueries/todoQueries';
  10.  
  11. const handleTodoToggle = (toggleTodo, todo) => {
  12. toggleTodo({
  13. variables: {
  14. todoId: todo.id,
  15. set: {
  16. completed: !todo.completed
  17. }
  18. },
  19. update: (cache, { data: { update_todo }}) => {
  20. const data = cache.readQuery({ query: QUERY_TODO })
  21. const toggledTodo = data.todo.find(t => t.id === todo.id)
  22. toggledTodo.completed = !todo.completed;
  23. cache.writeQuery({
  24. query: QUERY_TODO,
  25. data
  26. })
  27. }
  28. })
  29. }
  30.  
  31. const handleTodoDelete = (deleteTodo, todo) => {
  32. deleteTodo({
  33. variables: {
  34. todoId: todo.id
  35. },
  36. update: (cache, { data: { update_todo }}) => {
  37. const data = cache.readQuery({ query: QUERY_TODO })
  38. data.todo = data.todo.filter(t => {
  39. return t.id !== todo.id
  40. })
  41. cache.writeQuery({
  42. query: QUERY_TODO,
  43. data
  44. })
  45. }
  46. })
  47. }
  48.  
  49. const Todo = ({ todo }) => (
  50. <Mutation mutation={MUTATION_TODO_UPDATE}>
  51. {(updateTodo) => {
  52. console.log(updateTodo);
  53. return (
  54. <div className="parentContainer">
  55. <li className="todoItem"
  56. onClick={e => {
  57. handleTodoToggle(updateTodo, todo)
  58. }}>
  59. {
  60. todo.completed ?
  61. <strike className="todoLabel">{todo.task}</strike> :
  62. <label className="todoLabel">{todo.task}</label>
  63. }
  64. <Mutation mutation={MUTATION_TODO_DELETE}>
  65. {(deleteTodo) => {
  66. return (
  67. <label className="deleteLabel"
  68. onClick={e => {
  69. e.preventDefault();
  70. e.stopPropagation();
  71. handleTodoDelete(deleteTodo, todo)
  72. }}>
  73. Delete
  74. </label>
  75. )
  76. }}
  77. </Mutation>
  78. </li>
  79. </div>
  80. )
  81. }}
  82. </Mutation>
  83. )
  84.  
  85. export default Todo;
Add Comment
Please, Sign In to add comment