-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBlocklistReducer.js
173 lines (140 loc) · 4.55 KB
/
BlocklistReducer.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Syntax and Convention Reference:
https://github.com/erikras/ducks-modular-redux
http://blog.jakoblind.no/reduce-redux-boilerplate/
*/
import { combineReducers } from 'redux';
import { interval, of } from 'rxjs';
import { switchMap, mapTo, map, takeUntil, catchError, delay, startWith, exhaustMap } from 'rxjs/operators';
import { combineEpics, ofType } from 'redux-observable';
import apiPostgres from 'services/api-postgres';
import { errorLog } from 'helpers/error-logger';
import paramsToQuery from 'helpers/params-to-query';
// IMPORTANT
// Must modify action prefix since action types must be unique in the whole app
const actionPrefix = `BlocklistPage/Blocklist/`;
//Action Type
const FETCH_FULFILLED = actionPrefix + `FETCH_FULFILLED`;
const FETCH_REJECTED = actionPrefix + `FETCH_REJECTED`;
const POLLING_START = actionPrefix + `POLLING_START`;
const POLLING_STOP = actionPrefix + `POLLING_STOP`;
const FILTER_SET = actionPrefix + `FILTER_SET`;
const FILTER_TOGGLE = actionPrefix + `FILTER_TOGGLE`;
const RECORDS_UPDATE = actionPrefix + `RECORDS_UPDATE`;
//Action Creator
export const fetchFulfilled = (payload) => ({ type: FETCH_FULFILLED, payload });
export const fetchRejected = ( payload, error ) => ({ type: FETCH_REJECTED, payload, error });
export const pollingStart = (autoReload) => ({ type: POLLING_START, autoReload });
export const pollingStop = () => ({ type: POLLING_STOP });
export const filterSet = (enabled) => ({ type: FILTER_SET, enabled});
export const filterToggle = () => ({ type: FILTER_TOGGLE });
export const recordsUpdate = (count) => ({ type: RECORDS_UPDATE, recordsCount: count });
//Epic
const pollingEpic = ( action$, state$ ) => action$.pipe(
ofType(POLLING_START),
switchMap(action =>
interval(process.env.REACT_APP_POLLING_INTERVAL_TIME).pipe(
startWith(-1),
exhaustMap(index => {
let { value: { blocklistPage: { blocklist: { filter, records } } }} = state$;
let params = { records_count: records, show_empty: !filter };
let query = paramsToQuery(params);
return apiPostgres(`get_blocks${query}`).pipe(
map(res => fetchFulfilled(res.response)),
catchError(error => {
errorLog("Blocks page/ get block list error",error);
return of(fetchRejected(error.response, { status: error.status }))
})
)
}),
takeUntil(action$.pipe(
ofType(POLLING_STOP, POLLING_START, FETCH_REJECTED),
)),
)
),
);
const autoReloadEpic = action$ => action$.pipe(
ofType(FETCH_REJECTED),
delay(process.env.REACT_APP_AUTO_RELOAD_INTERVAL_TIME),
mapTo(pollingStart(true)),
);
const filterToggleEpic = action$ => action$.pipe(
ofType(FILTER_TOGGLE),
mapTo(pollingStart()),
);
const recordsUpdateEpic = action$ => action$.pipe(
ofType(RECORDS_UPDATE),
mapTo(pollingStart()),
);
export const combinedEpic = combineEpics(
pollingEpic,
autoReloadEpic,
filterToggleEpic,
recordsUpdateEpic
);
//Reducer
const dataInitState = {
payload: [],
error: undefined
}
const dataReducer = (state=dataInitState, action) => {
switch (action.type) {
case POLLING_START:
//If this is a polling started from the auto reload, do not reinit the state.
return !action.autoReload ? dataInitState : state;
case FETCH_FULFILLED:
return {
...state,
payload: action.payload,
error: undefined
};
case FETCH_REJECTED:
return {
...state,
//If current payload is having previous data, do not show the error
error: state.payload.length > 0 ? undefined : action.error
};
case RECORDS_UPDATE:
return {
...state
};
default:
return state;
}
};
const isPollingReducer = (state = false, action) => {
switch (action.type) {
case POLLING_START:
//If this is a polling started from the auto reload, keep the flag false.
return !action.autoReload;
case FETCH_FULFILLED:
case FETCH_REJECTED:
return false;
default:
return state;
}
};
const filterReducer = (state = false, action) => {
switch (action.type) {
case FILTER_SET:
return !!action.enabled;
case FILTER_TOGGLE:
return !state;
default:
return state;
}
};
const recordsReducer = (state = 100, action) => {
switch (action.type) {
case RECORDS_UPDATE:
return action.recordsCount;
default:
return state;
}
};
export const combinedReducer = combineReducers({
data: dataReducer,
isPolling: isPollingReducer,
filter: filterReducer,
records: recordsReducer
})