Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { action, thunk } from 'easy-peasy';
- import mockService from './mock-service';
- export default {
- todos: {},
- // actions
- setTodos: action((state, todos) => {
- state.todos = todos.reduce((acc, todo) => {
- acc[todo.id] = todo;
- return acc;
- }, {});
- }),
- // thunks
- fetchTodos: thunk(async actions => {
- const todos = await mockService.fetchTodos();
- actions.setTodos(todos);
- }),
- add: thunk(async (actions, todo) => {
- const updated = await mockService.saveTodo(todo);
- actions.setTodos(updated);
- }),
- toggle: thunk(async (actions, id, { getState }) => {
- const todo = getState().todos[id];
- if (!todo) return;
- const updated = await mockService.updateTodo(id, {
- done: !todo.done,
- });
- actions.setTodos(updated);
- }),
- delete: thunk(async (actions, id, { getState }) => {
- const todo = getState().todos[id];
- if (!todo) return;
- const updated = await mockService.deleteTodo(id);
- actions.setTodos(updated);
- }),
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement