forked from RisingStack/react-easy-state
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todosStore.js
46 lines (44 loc) · 1.06 KB
/
todosStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { store } from '@frontity/react-easy-state';
// a complex global store with a lot of derived data (getters and setters)
// use 'todos' instead of 'this' in the store methods to make them passable as callbacks
const todos = store({
all: [],
filter: 'all',
get isEmpty() {
return todos.all.length === 0;
},
get completed() {
return todos.all.filter((todo) => todo.completed);
},
get hasCompleted() {
return todos.completed.length !== 0;
},
get allCompleted() {
return todos.all.every((todo) => todo.completed);
},
set allCompleted(completed) {
todos.all.forEach((todo) => {
todo.completed = completed;
});
},
get active() {
return todos.all.filter((todo) => !todo.completed);
},
create(title) {
todos.all.push({ title });
},
remove(id) {
todos.all.splice(id, 1);
},
toggle(id) {
const todo = todos.all[id];
todo.completed = !todo.completed;
},
toggleAll() {
todos.allCompleted = !todos.allCompleted;
},
clearCompleted() {
todos.all = todos.active;
},
});
export default todos;