-
Notifications
You must be signed in to change notification settings - Fork 400
/
todo.state.ts
106 lines (92 loc) 路 2.23 KB
/
todo.state.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { NgxsOnInit } from './../../packages/store/src/symbols';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { of } from 'rxjs';
import { tap } from 'rxjs/operators';
export class AddTodo {
static type = 'AddTodo';
constructor(public readonly payload: string) {}
}
export class RemoveTodo {
static type = 'RemoveTodo';
constructor(public readonly payload: number) {}
}
export class TodoStateModel {
todo: string[];
pizza: { model: any };
}
@State<string[]>({
name: 'todo',
defaults: []
})
export class TodoState implements NgxsOnInit {
@Selector()
static pandas(state: string[]) {
return state.filter(s => s.indexOf('panda') > -1);
}
ngxsOnInit({ getState, setState }: StateContext<string[]>) {
const state: string[] = getState();
const payload = 'NgxsOnInit todo';
if (!state.includes(payload)) {
setState([...state, payload]);
}
}
@Action(AddTodo)
addTodo({ getState, setState }: StateContext<string[]>, { payload }: AddTodo) {
setState([...getState(), payload]);
}
@Action(RemoveTodo)
removeTodo({ getState, setState }: StateContext<string[]>, { payload }: RemoveTodo) {
setState(getState().filter((_, i) => i !== payload));
}
}
export class SetPrefix {
static type = 'SetPrefix';
}
export class LoadData {
static type = 'LoadData';
}
@State<TodoStateModel>({
name: 'todos',
defaults: {
todo: [],
pizza: { model: undefined }
},
children: [TodoState]
})
export class TodosState {
@Selector()
static pizza(state: TodoStateModel) {
return state.pizza;
}
@Action(SetPrefix)
setPrefix({ getState, setState, patchState }: StateContext<TodoStateModel>) {
const state = getState();
const pizza1 = state.pizza.model.toppings;
patchState({
pizza: {
model: {
toppings: 'Mr. ' + pizza1
}
}
});
}
@Action(LoadData)
loadData({ patchState }: StateContext<TodoStateModel>) {
const data = {
toppings: 'pineapple',
crust: 'medium',
extras: [false, false, true]
};
return of(data).pipe(
tap((vals: any) => {
patchState({
pizza: {
model: {
...vals
}
}
});
})
);
}
}