-
Notifications
You must be signed in to change notification settings - Fork 17
/
reducer.js
70 lines (61 loc) · 1.43 KB
/
reducer.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
import { SET_PAGE } from 'state/pagination/types';
import { isInteger } from '@entando/utils';
const initialState = {
global: {
page: 1,
pageSize: 10,
lastPage: 1,
totalItems: 0,
},
};
const isPageValid = (page, lastPage) => {
if (!isInteger(page) ||
page < 1 ||
page > lastPage
) {
return false;
}
return true;
};
const isPageSizeValid = (pageSize) => {
if (!isInteger(pageSize) || pageSize < 0) {
return false;
}
return true;
};
const isTotalItemsValid = totalItems => isInteger(totalItems) && totalItems > 0;
const isLastPageValid = (lastPage) => {
if (!isInteger(lastPage)) {
return false;
}
return true;
};
const isPayloadValid = (payload) => {
if (!isPageValid(payload.page, payload.lastPage) ||
!isPageSizeValid(payload.pageSize) ||
!isLastPageValid(payload.lastPage) ||
!isTotalItemsValid(payload.totalItems)
) {
return false;
}
return true;
};
const castValues = page => (
{
page: parseFloat(page.page),
pageSize: parseFloat(page.pageSize),
lastPage: parseFloat(page.lastPage),
totalItems: parseFloat(page.totalItems),
}
);
const reducer = (state = initialState, action = {}) => {
switch (action.type) {
case SET_PAGE: {
return isPayloadValid(action.payload.page) ?
{ ...state, [action.payload.namespace]: castValues(action.payload.page) } :
state;
}
default: return state;
}
};
export default reducer;