Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import { action, thunk } from 'easy-peasy';
  2. import mockService from './mock-service';
  3.  
  4. export default {
  5. todos: {},
  6. // actions
  7. setTodos: action((state, todos) => {
  8. state.todos = todos.reduce((acc, todo) => {
  9. acc[todo.id] = todo;
  10. return acc;
  11. }, {});
  12. }),
  13. // thunks
  14. fetchTodos: thunk(async actions => {
  15. const todos = await mockService.fetchTodos();
  16. actions.setTodos(todos);
  17. }),
  18. add: thunk(async (actions, todo) => {
  19. const updated = await mockService.saveTodo(todo);
  20. actions.setTodos(updated);
  21. }),
  22. toggle: thunk(async (actions, id, { getState }) => {
  23. const todo = getState().todos[id];
  24. if (!todo) return;
  25. const updated = await mockService.updateTodo(id, {
  26. done: !todo.done,
  27. });
  28. actions.setTodos(updated);
  29. }),
  30. delete: thunk(async (actions, id, { getState }) => {
  31. const todo = getState().todos[id];
  32. if (!todo) return;
  33. const updated = await mockService.deleteTodo(id);
  34. actions.setTodos(updated);
  35. }),
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement