-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpolicies.reducer.ts
117 lines (107 loc) · 3.18 KB
/
policies.reducer.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
107
108
109
110
111
112
113
114
115
116
117
import { createReducer, on, Action } from "@ngrx/store";
import {
initialPolicyState,
PolicyState,
} from "state-management/state/policies.store";
import * as fromActions from "state-management/actions/policies.actions";
const reducer = createReducer(
initialPolicyState,
on(
fromActions.fetchPoliciesCompleteAction,
(state, { policies }): PolicyState => ({
...state,
policies,
}),
),
on(
fromActions.fetchCountCompleteAction,
(state, { count }): PolicyState => ({
...state,
totalCount: count,
}),
),
on(
fromActions.fetchEditablePoliciesCompleteAction,
(state, { policies }): PolicyState => ({
...state,
editablePolicies: policies,
}),
),
on(
fromActions.fetchEditableCountCompleteAction,
(state, { count }): PolicyState => ({
...state,
editableCount: count,
}),
),
on(fromActions.selectPolicyAction, (state, { policy }): PolicyState => {
const alreadySelected = state.selectedPolicies.find(
(existing) => existing.id === policy.id,
);
if (alreadySelected) {
return state;
} else {
const selectedPolicies = state.selectedPolicies.concat(policy);
return { ...state, selectedPolicies };
}
}),
on(fromActions.deselectPolicyAction, (state, { policy }): PolicyState => {
const selectedPolicies = state.selectedPolicies.filter(
(selectedPolicy) => selectedPolicy.id !== policy.id,
);
return { ...state, selectedPolicies };
}),
on(fromActions.selectAllPoliciesAction, (state): PolicyState => {
const selectedPolicies = state.editablePolicies;
return { ...state, selectedPolicies };
}),
on(
fromActions.clearSelectionAction,
(state): PolicyState => ({
...state,
selectedPolicies: [],
}),
),
on(fromActions.changePageAction, (state, { page, limit }): PolicyState => {
const skip = page * limit;
const policiesFilters = { ...state.policiesFilters, skip, limit };
return { ...state, policiesFilters };
}),
on(
fromActions.changeEditablePageAction,
(state, { page, limit }): PolicyState => {
const skip = page * limit;
const editableFilters = { ...state.editableFilters, skip, limit };
return { ...state, editableFilters };
},
),
on(
fromActions.sortByColumnAction,
(state, { column, direction }): PolicyState => {
const sortField = column + (direction ? ":" + direction : "");
const policiesFilters = { ...state.policiesFilters, sortField, skip: 0 };
return { ...state, policiesFilters };
},
),
on(
fromActions.sortEditableByColumnAction,
(state, { column, direction }): PolicyState => {
const sortField = column + (direction ? ":" + direction : "");
const editableFilters = { ...state.editableFilters, sortField, skip: 0 };
return { ...state, editableFilters };
},
),
on(
fromActions.clearPoliciesStateAction,
(): PolicyState => ({ ...initialPolicyState }),
),
);
export const policiesReducer = (
state: PolicyState | undefined,
action: Action,
) => {
if (action.type.indexOf("[Policy]") !== -1) {
console.log("Action came in! " + action.type);
}
return reducer(state, action);
};