-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo-store-ts.ts
60 lines (47 loc) · 1.27 KB
/
todo-store-ts.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { effect, model, reducer } from '../src';
export interface ITodoItemRecord {
id: string;
text: string;
completed: boolean;
}
export interface ITodoState {
[id: string]: ITodoItemRecord;
}
export type TodoArgument = Partial<ITodoItemRecord>;
export class TodoStore {
public name: 'todo' = 'todo';
public state: ITodoState = {};
@effect
public addTodo({ text }: TodoArgument) {
this.ADD_TODO({
id: Math.random().toString().substring(2),
text,
});
}
@effect
public toggleTodo({ id }: TodoArgument) {
this.TOGGLE_TODO({ id });
}
@effect
public removeTodo({ id }: TodoArgument) {
this.REMOVE_TODO({ id });
}
@reducer
private ADD_TODO({ id, text }: TodoArgument) {
this.state[id] = { id, text, completed: false };
}
@reducer
private TOGGLE_TODO({ id }: TodoArgument) {
this.state[id].completed = !this.state[id].completed;
}
@reducer
private REMOVE_TODO({ id }: TodoArgument) {
delete this.state[id];
}
public static withToggled({ todo }: { todo: ITodoState }) {
return {
toggled: Object.keys(todo).filter(f => todo[f].completed),
};
}
}
export default model(TodoStore);