SHARE
TWEET

Untitled

a guest Apr 18th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createTodosStore() {
  2.   const [state, setState] = createLocalState({
  3.     counter: 1, todos: [], showMode: 'all'
  4.   });
  5.   createEffect(() => {
  6.     const completedCount = state.todos.filter(
  7.       todo => todo.completed
  8.     ).length;
  9.     setState({
  10.       completedCount,
  11.       remainingCount: state.todos.length - completedCount
  12.     });
  13.   });
  14.   return [
  15.     state, {
  16.     addTodo: ({title}) => setState(
  17.       ['todos', t =>
  18.         [{title, id: state.counter, completed: false}, ...t]
  19.       ],
  20.       ['counter', c => c + 1]
  21.     ),
  22.     removeTodo: todoId => setState('todos',
  23.       t => t.filter(item => item.id !== todoId)
  24.     ),
  25.     editTodo: todo => setState(
  26.       'todos',
  27.       state.todos.findIndex(item => item.id === todo.id),
  28.       todo
  29.     ),
  30.     clearCompleted: () => setState(
  31.       'todos', t => t.filter(todo => !todo.completed)
  32.     ),
  33.     toggleAll: completed => setState(
  34.       'todos', todo => todo.completed !== completed, {completed}
  35.     ),
  36.     setVisibility: showMode => setState('showMode', showMode)
  37.   }];
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top