-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlogbooks.reducer.ts
109 lines (98 loc) · 2.77 KB
/
logbooks.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
import { createReducer, on, Action } from "@ngrx/store";
import {
initialLogbookState,
LogbookState,
} from "state-management/state/logbooks.store";
import * as fromActions from "state-management/actions/logbooks.actions";
const reducer = createReducer(
initialLogbookState,
on(
fromActions.fetchLogbooksCompleteAction,
(state, { logbooks }): LogbookState => {
const formattedLogbooks = logbooks.map((logbook) => {
const descendingMessages = logbook.messages.reverse();
logbook.messages = descendingMessages;
return logbook;
});
return { ...state, logbooks: formattedLogbooks };
},
),
on(
fromActions.fetchLogbookCompleteAction,
(state, { logbook }): LogbookState => {
const currentLogbook = logbook;
return { ...state, currentLogbook };
},
),
on(
fromActions.fetchLogbookFailedAction,
(state): LogbookState => ({
...state,
currentLogbook: undefined,
}),
),
on(
fromActions.clearLogbookAction,
(state): LogbookState => ({
...state,
currentLogbook: undefined,
}),
),
on(
fromActions.fetchCountCompleteAction,
(state, { count }): LogbookState => ({
...state,
totalCount: count,
}),
),
on(fromActions.prefillFiltersAction, (state, { values }): LogbookState => {
const filters = { ...state.filters, ...values };
return { ...state, filters, hasPrefilledFilters: true };
}),
on(fromActions.setTextFilterAction, (state, { textSearch }): LogbookState => {
const filters = { ...state.filters, textSearch, skip: 0 };
return { ...state, filters };
}),
on(
fromActions.setDisplayFiltersAction,
(
state,
{ showBotMessages, showImages, showUserMessages },
): LogbookState => {
const filters = {
...state.filters,
showBotMessages,
showImages,
showUserMessages,
skip: 0,
};
return { ...state, filters };
},
),
on(fromActions.changePageAction, (state, { page, limit }): LogbookState => {
const skip = page * limit;
const filters = { ...state.filters, skip, limit };
return { ...state, filters };
}),
on(
fromActions.sortByColumnAction,
(state, { column, direction }): LogbookState => {
const sortField = column + (direction ? ":" + direction : "");
const filters = { ...state.filters, sortField, skip: 0 };
return { ...state, filters };
},
),
on(
fromActions.clearLogbooksStateAction,
(): LogbookState => ({ ...initialLogbookState }),
),
);
export const logbooksReducer = (
state: LogbookState | undefined,
action: Action,
) => {
if (action.type.indexOf("[Logbook]") !== -1) {
console.log("Logbook reducer Action came in! " + action.type);
}
return reducer(state, action);
};