redux-paginator-immutable 17.0.16 → 17.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Info.d.ts +9 -0
- package/lib/Info.js +19 -41
- package/lib/Info.js.map +1 -0
- package/lib/LoadDataItem.d.ts +23 -0
- package/lib/LoadDataItem.js +59 -110
- package/lib/LoadDataItem.js.map +1 -0
- package/lib/LoadPaginator.d.ts +41 -0
- package/lib/LoadPaginator.js +109 -216
- package/lib/LoadPaginator.js.map +1 -0
- package/lib/LoadingButton.d.ts +11 -0
- package/lib/LoadingButton.js +22 -75
- package/lib/LoadingButton.js.map +1 -0
- package/lib/actionTypes.d.ts +9 -0
- package/lib/actionTypes.js +13 -16
- package/lib/actionTypes.js.map +1 -0
- package/lib/actions.d.ts +42 -0
- package/lib/actions.js +82 -136
- package/lib/actions.js.map +1 -0
- package/lib/agent.d.ts +8 -0
- package/lib/agent.js +37 -86
- package/lib/agent.js.map +1 -0
- package/lib/createDataReducer.d.ts +14 -0
- package/lib/createDataReducer.js +60 -79
- package/lib/createDataReducer.js.map +1 -0
- package/lib/createItemReducer.d.ts +9 -0
- package/lib/createItemReducer.js +14 -32
- package/lib/createItemReducer.js.map +1 -0
- package/lib/createPaginator.d.ts +14 -0
- package/lib/createPaginator.js +83 -140
- package/lib/createPaginator.js.map +1 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +30 -58
- package/lib/index.js.map +1 -0
- package/lib/paginatorMiddleware.d.ts +5 -0
- package/lib/paginatorMiddleware.js +68 -102
- package/lib/paginatorMiddleware.js.map +1 -0
- package/lib/reducerItemData.d.ts +18 -0
- package/lib/reducerItemData.js +56 -63
- package/lib/reducerItemData.js.map +1 -0
- package/lib/reducerOperations.d.ts +3 -0
- package/lib/reducerOperations.js +69 -114
- package/lib/reducerOperations.js.map +1 -0
- package/lib/reducers.d.ts +4 -0
- package/lib/reducers.js +75 -87
- package/lib/reducers.js.map +1 -0
- package/lib/selectors.d.ts +58 -0
- package/lib/selectors.js +79 -129
- package/lib/selectors.js.map +1 -0
- package/lib/types/actions.d.ts +85 -0
- package/lib/types/actions.js +3 -1
- package/lib/types/actions.js.map +1 -0
- package/lib/types/index.d.ts +21 -0
- package/lib/types/index.js +16 -29
- package/lib/types/index.js.map +1 -0
- package/lib/types/state.d.ts +6 -0
- package/lib/types/state.js +3 -1
- package/lib/types/state.js.map +1 -0
- package/lib/util.d.ts +9 -0
- package/lib/util.js +49 -73
- package/lib/util.js.map +1 -0
- package/lib/words.d.ts +9 -0
- package/lib/words.js +12 -15
- package/lib/words.js.map +1 -0
- package/package.json +35 -21
- package/src/Info.tsx +29 -0
- package/src/LoadDataItem.tsx +84 -0
- package/src/{LoadPaginator.jsx → LoadPaginator.tsx} +77 -94
- package/src/LoadingButton.tsx +40 -0
- package/src/__tests__/createPaginator.spec.js +6 -6
- package/src/__tests__/reducers.spec.js +28 -28
- package/src/__tests__/selectors.spec.js +80 -81
- package/src/{actionTypes.js → actionTypes.ts} +0 -4
- package/src/actions.ts +157 -0
- package/src/agent.ts +62 -0
- package/src/createDataReducer.ts +91 -0
- package/src/{createItemReducer.js → createItemReducer.ts} +7 -13
- package/src/createPaginator.ts +107 -0
- package/src/{index.js → index.ts} +0 -13
- package/src/paginatorMiddleware.ts +93 -0
- package/src/reducerItemData.ts +74 -0
- package/src/reducerOperations.ts +101 -0
- package/src/{reducers.js → reducers.ts} +9 -18
- package/src/selectors.ts +113 -0
- package/src/types/{actions.js → actions.ts} +27 -39
- package/src/types/{index.js → index.ts} +6 -13
- package/src/types/{state.js → state.ts} +3 -6
- package/src/util.ts +51 -0
- package/src/{words.js → words.ts} +0 -2
- package/lib/__tests__/actions.spec.js +0 -117
- package/lib/__tests__/agent.spec.js +0 -32
- package/lib/__tests__/createPaginator.spec.js +0 -39
- package/lib/__tests__/reducers.spec.js +0 -126
- package/lib/__tests__/selectors.spec.js +0 -162
- package/lib/__tests__/setup.js +0 -3
- package/src/Info.jsx +0 -34
- package/src/LoadDataItem.jsx +0 -116
- package/src/LoadingButton.jsx +0 -55
- package/src/actions.js +0 -153
- package/src/agent.js +0 -55
- package/src/createDataReducer.js +0 -94
- package/src/createPaginator.js +0 -131
- package/src/paginatorMiddleware.js +0 -94
- package/src/reducerItemData.jsx +0 -64
- package/src/reducerOperations.jsx +0 -112
- package/src/selectors.js +0 -143
- package/src/util.js +0 -71
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/* eslint-disable max-lines-per-function, */
|
|
2
|
+
|
|
3
|
+
import { rowsPerLoad as defaultRowsPerLoad } from "x25/utility/others";
|
|
4
|
+
import * as Immutable from "immutable";
|
|
5
|
+
import { pages as pagesReducer, items as itemsReducer, dataItems as dataItemsReducer } from "./reducers";
|
|
6
|
+
import actions from "./actions";
|
|
7
|
+
|
|
8
|
+
import type { PaginatorSettings } from "./types";
|
|
9
|
+
|
|
10
|
+
type EndpointData = {
|
|
11
|
+
path: string;
|
|
12
|
+
cb: () => string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type Endpoint = EndpointData | string;
|
|
16
|
+
type CreatePaginator = (endpoint: Endpoint, info: PaginatorSettings) => any;
|
|
17
|
+
type OnlyForEndpoint = (endpoint: string, reducer: any) => (state: any, action: {
|
|
18
|
+
meta: {
|
|
19
|
+
endpoint: string;
|
|
20
|
+
};
|
|
21
|
+
}) => any;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const getEndpoint = (data: Endpoint) => {
|
|
25
|
+
if (typeof data === "object") {
|
|
26
|
+
return {
|
|
27
|
+
path : data.path,
|
|
28
|
+
cb : data.cb,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
path : data,
|
|
34
|
+
cb : null,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const
|
|
39
|
+
onlyForEndpoint: OnlyForEndpoint = (endpoint, reducer) => (state = Immutable.Map(), action) => {
|
|
40
|
+
if (typeof action.meta === "undefined" || action.meta.endpoint !== endpoint) {
|
|
41
|
+
return state;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return reducer(state, action);
|
|
45
|
+
},
|
|
46
|
+
createPaginator: CreatePaginator = (endpointData: Endpoint, settings: PaginatorSettings) => {
|
|
47
|
+
const
|
|
48
|
+
{
|
|
49
|
+
path: endpoint,
|
|
50
|
+
cb: endpointCb,
|
|
51
|
+
} = getEndpoint(endpointData),
|
|
52
|
+
{
|
|
53
|
+
key,
|
|
54
|
+
normalizeDataItem,
|
|
55
|
+
dataItemURL,
|
|
56
|
+
manageEntity,
|
|
57
|
+
resultsKey,
|
|
58
|
+
totalKey = "Total",
|
|
59
|
+
pageArgName = "page",
|
|
60
|
+
idKey = "ID",
|
|
61
|
+
rowsPerLoad = defaultRowsPerLoad,
|
|
62
|
+
manipulateItems = (items) => items,
|
|
63
|
+
} = settings,
|
|
64
|
+
endpointedActions = {
|
|
65
|
+
requestPage: (page: number, token: string) => actions.requestPage({
|
|
66
|
+
endpoint,
|
|
67
|
+
endpointCb,
|
|
68
|
+
manageEntity,
|
|
69
|
+
resultsKey,
|
|
70
|
+
totalKey,
|
|
71
|
+
pageArgName,
|
|
72
|
+
idKey,
|
|
73
|
+
page,
|
|
74
|
+
token,
|
|
75
|
+
}),
|
|
76
|
+
|
|
77
|
+
resetView (token : any) {
|
|
78
|
+
return actions.resetView(endpoint, token);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
changeView ({ view, token }: { view: number; token: string; }) {
|
|
82
|
+
return actions.changeView(endpoint, {
|
|
83
|
+
view,
|
|
84
|
+
token,
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
clearData () {
|
|
89
|
+
return actions.clearData(endpoint);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
key,
|
|
95
|
+
normalizeDataItem,
|
|
96
|
+
dataItemURL,
|
|
97
|
+
manipulateItems,
|
|
98
|
+
rowsPerLoad,
|
|
99
|
+
manageEntity,
|
|
100
|
+
resultsKey,
|
|
101
|
+
endpoint,
|
|
102
|
+
pagesReducer : onlyForEndpoint(endpoint, pagesReducer),
|
|
103
|
+
itemsReducer : onlyForEndpoint(endpoint, itemsReducer),
|
|
104
|
+
dataItemsReducer : onlyForEndpoint(endpoint, dataItemsReducer),
|
|
105
|
+
...endpointedActions,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
1
|
import { createPaginator } from "./createPaginator";
|
|
4
2
|
import paginatorMiddleware from "./paginatorMiddleware";
|
|
5
|
-
|
|
6
3
|
import paginatorSelectors from "./selectors";
|
|
7
|
-
|
|
8
4
|
import { manageDataEntity, manipulateDataItems, manipulateItems, manageEntity } from "./util";
|
|
9
|
-
|
|
10
5
|
import Info from "./Info";
|
|
11
6
|
import LoadingButton from "./LoadingButton";
|
|
12
7
|
import LoadPaginator from "./LoadPaginator";
|
|
13
8
|
import LoadDataItem from "./LoadDataItem";
|
|
14
|
-
|
|
15
9
|
import createDataReducer from "./createDataReducer";
|
|
16
10
|
import createItemReducer from "./createItemReducer";
|
|
17
11
|
|
|
@@ -20,19 +14,12 @@ export {
|
|
|
20
14
|
LoadingButton,
|
|
21
15
|
LoadPaginator,
|
|
22
16
|
LoadDataItem,
|
|
23
|
-
|
|
24
17
|
createPaginator,
|
|
25
|
-
|
|
26
18
|
paginatorMiddleware,
|
|
27
|
-
|
|
28
19
|
paginatorSelectors,
|
|
29
|
-
|
|
30
|
-
// util
|
|
31
20
|
createDataReducer,
|
|
32
|
-
|
|
33
21
|
manageDataEntity,
|
|
34
22
|
manipulateDataItems,
|
|
35
|
-
|
|
36
23
|
createItemReducer,
|
|
37
24
|
manipulateItems,
|
|
38
25
|
manageEntity,
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* eslint-disable no-console, max-lines-per-function */
|
|
2
|
+
import type { Dispatch, Action } from "./types";
|
|
3
|
+
import { REQUEST_PAGE } from "./actionTypes";
|
|
4
|
+
import actions from "./actions";
|
|
5
|
+
import { fetchPage } from "./agent";
|
|
6
|
+
|
|
7
|
+
const
|
|
8
|
+
{ receivePage } = actions,
|
|
9
|
+
|
|
10
|
+
paginatorMiddleware = ({ dispatch }: { dispatch: Dispatch;}) => (next: any) => (action: Action) => {
|
|
11
|
+
if (action.type === REQUEST_PAGE) {
|
|
12
|
+
const {
|
|
13
|
+
meta: {
|
|
14
|
+
endpoint,
|
|
15
|
+
endpointCb,
|
|
16
|
+
manageEntity,
|
|
17
|
+
resultsKey,
|
|
18
|
+
totalKey,
|
|
19
|
+
pageArgName,
|
|
20
|
+
idKey,
|
|
21
|
+
},
|
|
22
|
+
payload: {
|
|
23
|
+
page,
|
|
24
|
+
token,
|
|
25
|
+
},
|
|
26
|
+
} = action;
|
|
27
|
+
|
|
28
|
+
dispatch((dispatch2) => {
|
|
29
|
+
const
|
|
30
|
+
markAsError = () => {
|
|
31
|
+
dispatch2(receivePage({
|
|
32
|
+
endpoint,
|
|
33
|
+
endpointCb,
|
|
34
|
+
manageEntity,
|
|
35
|
+
pageArgName,
|
|
36
|
+
idKey,
|
|
37
|
+
page,
|
|
38
|
+
error : true,
|
|
39
|
+
token,
|
|
40
|
+
items : [],
|
|
41
|
+
total : 0,
|
|
42
|
+
}));
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
rawPath = typeof endpointCb === "function" ? endpointCb(token) : endpoint,
|
|
46
|
+
path = rawPath === null ? "" : rawPath;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
fetchPage({
|
|
50
|
+
endpoint: path,
|
|
51
|
+
pageArgName,
|
|
52
|
+
page,
|
|
53
|
+
token,
|
|
54
|
+
}).then((res: { response: any; }) => {
|
|
55
|
+
const { response } = res;
|
|
56
|
+
|
|
57
|
+
let
|
|
58
|
+
results = [],
|
|
59
|
+
total = 0;
|
|
60
|
+
|
|
61
|
+
if (typeof resultsKey === "undefined") {
|
|
62
|
+
results = response;
|
|
63
|
+
} else {
|
|
64
|
+
results = response[resultsKey];
|
|
65
|
+
total = response[totalKey];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
dispatch2(receivePage({
|
|
69
|
+
endpoint,
|
|
70
|
+
endpointCb,
|
|
71
|
+
manageEntity,
|
|
72
|
+
pageArgName,
|
|
73
|
+
idKey,
|
|
74
|
+
page,
|
|
75
|
+
error : false,
|
|
76
|
+
token,
|
|
77
|
+
items : results,
|
|
78
|
+
total,
|
|
79
|
+
}));
|
|
80
|
+
}).
|
|
81
|
+
catch(() => {
|
|
82
|
+
markAsError();
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
markAsError();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return next(action);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export default paginatorMiddleware;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
type FetchItemDataFulFilledProps = {
|
|
2
|
+
payload: {
|
|
3
|
+
Data: any;
|
|
4
|
+
};
|
|
5
|
+
meta: {
|
|
6
|
+
id: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
import * as Immutable from "immutable";
|
|
10
|
+
|
|
11
|
+
const
|
|
12
|
+
defaultOptions = Immutable.Map({
|
|
13
|
+
fetching : true,
|
|
14
|
+
fetched : false,
|
|
15
|
+
error : false,
|
|
16
|
+
}),
|
|
17
|
+
|
|
18
|
+
fetchItemDataPending = (state: any, {
|
|
19
|
+
meta: {
|
|
20
|
+
id,
|
|
21
|
+
},
|
|
22
|
+
}: {
|
|
23
|
+
meta: {
|
|
24
|
+
id: string;
|
|
25
|
+
};
|
|
26
|
+
}) => (
|
|
27
|
+
state.update(id, (current : any) => {
|
|
28
|
+
if (typeof current === "undefined") {
|
|
29
|
+
return defaultOptions;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return current.mergeDeep(defaultOptions);
|
|
33
|
+
})
|
|
34
|
+
),
|
|
35
|
+
fetchItemDataRejected = (state: any, {
|
|
36
|
+
meta: {
|
|
37
|
+
id,
|
|
38
|
+
},
|
|
39
|
+
}: {
|
|
40
|
+
meta: {
|
|
41
|
+
id: string;
|
|
42
|
+
};
|
|
43
|
+
}) => state.update(id, (current : any) => {
|
|
44
|
+
if (typeof current === "undefined") {
|
|
45
|
+
return current;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return current.mergeDeep(Immutable.Map({
|
|
49
|
+
fetching : false,
|
|
50
|
+
fetched : false,
|
|
51
|
+
error : true,
|
|
52
|
+
}));
|
|
53
|
+
}),
|
|
54
|
+
fetchItemDataFulFilled = (state: any, {
|
|
55
|
+
payload: {
|
|
56
|
+
Data,
|
|
57
|
+
},
|
|
58
|
+
meta: {
|
|
59
|
+
id,
|
|
60
|
+
},
|
|
61
|
+
}: FetchItemDataFulFilledProps) => state.update(id, (current : any) => {
|
|
62
|
+
if (typeof current === "undefined") {
|
|
63
|
+
return current;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return current.mergeDeep(Immutable.Map({
|
|
67
|
+
Data,
|
|
68
|
+
fetching : false,
|
|
69
|
+
fetched : true,
|
|
70
|
+
error : false,
|
|
71
|
+
}));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export { fetchItemDataPending, fetchItemDataRejected, fetchItemDataFulFilled };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as Immutable from "immutable";
|
|
2
|
+
import type {
|
|
3
|
+
ItemsState,
|
|
4
|
+
PagesState,
|
|
5
|
+
Action,
|
|
6
|
+
ReceivePageAction,
|
|
7
|
+
ChangeViewAction,
|
|
8
|
+
RequestPageAction,
|
|
9
|
+
} from "./types";
|
|
10
|
+
|
|
11
|
+
const requestPage = (state: PagesState, action: RequestPageAction) => {
|
|
12
|
+
const {
|
|
13
|
+
payload: {
|
|
14
|
+
token,
|
|
15
|
+
page,
|
|
16
|
+
},
|
|
17
|
+
} = action,
|
|
18
|
+
elements = Immutable.Map({
|
|
19
|
+
page,
|
|
20
|
+
error : false,
|
|
21
|
+
fetching : true,
|
|
22
|
+
fetched : false,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (state.has(token)) {
|
|
26
|
+
return state.update(token, (current : any) => current.merge(elements));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const init = elements.merge({
|
|
30
|
+
view : 1,
|
|
31
|
+
token,
|
|
32
|
+
ids : Immutable.List(),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return state.set(token, init);
|
|
36
|
+
},
|
|
37
|
+
receivePage = (state: PagesState, action: ReceivePageAction) => {
|
|
38
|
+
const {
|
|
39
|
+
meta: {
|
|
40
|
+
idKey,
|
|
41
|
+
},
|
|
42
|
+
payload: {
|
|
43
|
+
token,
|
|
44
|
+
items,
|
|
45
|
+
total,
|
|
46
|
+
error,
|
|
47
|
+
},
|
|
48
|
+
} = action;
|
|
49
|
+
|
|
50
|
+
if (state.has(token)) {
|
|
51
|
+
const hasError = error === true;
|
|
52
|
+
|
|
53
|
+
return state.update(token, (
|
|
54
|
+
(current : any) => current.update((page : any) => current.merge(Immutable.Map({
|
|
55
|
+
ids : page.get("ids").concat(Immutable.List(items.map((item) => String(item[idKey])))),
|
|
56
|
+
fetching : false,
|
|
57
|
+
error : hasError,
|
|
58
|
+
fetched : true,
|
|
59
|
+
total : hasError ? page.get("total") : total,
|
|
60
|
+
})))
|
|
61
|
+
));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return state;
|
|
65
|
+
},
|
|
66
|
+
performChangeView = (state: PagesState, action: any, view: number) => {
|
|
67
|
+
const {
|
|
68
|
+
payload: {
|
|
69
|
+
token,
|
|
70
|
+
},
|
|
71
|
+
} = action,
|
|
72
|
+
item = state.get(token);
|
|
73
|
+
|
|
74
|
+
if (typeof item === "undefined") {
|
|
75
|
+
return state;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return state.update(token, (current : any) => current.set("view", view));
|
|
79
|
+
},
|
|
80
|
+
changeView = (state: PagesState, action: ChangeViewAction) => (
|
|
81
|
+
performChangeView(state, action, action.payload.view)
|
|
82
|
+
),
|
|
83
|
+
resetView = (state: PagesState, action: Action) => performChangeView(state, action, 1),
|
|
84
|
+
receivePageItems = (state: ItemsState, action: ReceivePageAction) => {
|
|
85
|
+
const
|
|
86
|
+
{ payload, meta } = action,
|
|
87
|
+
{ items: payloadItems } = payload,
|
|
88
|
+
{ idKey, manageEntity } = meta,
|
|
89
|
+
manage = typeof manageEntity === "function" ? manageEntity : (
|
|
90
|
+
(item : any) => Immutable.Map(item)
|
|
91
|
+
),
|
|
92
|
+
newItems = payloadItems.reduce(
|
|
93
|
+
(previous, item) => (
|
|
94
|
+
previous.set(String(item[idKey]), manage(item, state, idKey))
|
|
95
|
+
), Immutable.Map(),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return state.merge(newItems);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { requestPage, receivePage, performChangeView, changeView, resetView, receivePageItems };
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
ItemsState,
|
|
5
|
-
PagesState,
|
|
6
|
-
|
|
7
|
-
Action,
|
|
8
|
-
} from "./types";
|
|
9
|
-
|
|
1
|
+
import * as Immutable from "immutable";
|
|
10
2
|
import {
|
|
11
3
|
REQUEST_PAGE,
|
|
12
4
|
RECEIVE_PAGE,
|
|
@@ -14,18 +6,15 @@ import {
|
|
|
14
6
|
CHANGE_VIEW,
|
|
15
7
|
CLEAR_DATA,
|
|
16
8
|
FETCH_CURRENT_COMPANY_INFO_PENDING,
|
|
17
|
-
|
|
18
9
|
FETCH_ITEM_DATA_PENDING,
|
|
19
10
|
FETCH_ITEM_DATA_REJECTED,
|
|
20
11
|
FETCH_ITEM_DATA_FULFILLED,
|
|
21
12
|
} from "./actionTypes";
|
|
22
|
-
|
|
23
|
-
import * as Immutable from "immutable";
|
|
24
|
-
|
|
13
|
+
import type { ItemsState, PagesState, Action } from "./types";
|
|
25
14
|
import * as reducerItemData from "./reducerItemData";
|
|
26
15
|
import * as reducerOperations from "./reducerOperations";
|
|
27
16
|
|
|
28
|
-
export const pages = (state
|
|
17
|
+
export const pages = (state: PagesState = Immutable.Map(), action: Action) => {
|
|
29
18
|
switch (action.type) {
|
|
30
19
|
case REQUEST_PAGE:
|
|
31
20
|
return reducerOperations.requestPage(state, action);
|
|
@@ -47,8 +36,7 @@ export const pages = (state : PagesState = Immutable.Map(), action : Action) =>
|
|
|
47
36
|
return state;
|
|
48
37
|
}
|
|
49
38
|
};
|
|
50
|
-
|
|
51
|
-
export const items = (state : ItemsState = Immutable.Map(), action : Action) => {
|
|
39
|
+
export const items = (state: ItemsState = Immutable.Map(), action: Action) => {
|
|
52
40
|
switch (action.type) {
|
|
53
41
|
case RECEIVE_PAGE:
|
|
54
42
|
return reducerOperations.receivePageItems(state, action);
|
|
@@ -56,26 +44,29 @@ export const items = (state : ItemsState = Immutable.Map(), action : Action) =>
|
|
|
56
44
|
case FETCH_CURRENT_COMPANY_INFO_PENDING:
|
|
57
45
|
case CLEAR_DATA:
|
|
58
46
|
return state.clear();
|
|
47
|
+
|
|
59
48
|
default:
|
|
60
49
|
return state;
|
|
61
50
|
}
|
|
62
51
|
};
|
|
63
|
-
|
|
64
|
-
export const dataItems = (state : ItemsState = Immutable.Map(), action : Action) => {
|
|
52
|
+
export const dataItems = (state: ItemsState = Immutable.Map(), action: Action) => {
|
|
65
53
|
switch (action.type) {
|
|
66
54
|
case RECEIVE_PAGE:
|
|
67
55
|
return reducerOperations.receivePageItems(state, action);
|
|
68
56
|
|
|
69
57
|
case FETCH_ITEM_DATA_PENDING:
|
|
70
58
|
return reducerItemData.fetchItemDataPending(state, action);
|
|
59
|
+
|
|
71
60
|
case FETCH_ITEM_DATA_REJECTED:
|
|
72
61
|
return reducerItemData.fetchItemDataRejected(state, action);
|
|
62
|
+
|
|
73
63
|
case FETCH_ITEM_DATA_FULFILLED:
|
|
74
64
|
return reducerItemData.fetchItemDataFulFilled(state, action);
|
|
75
65
|
|
|
76
66
|
case FETCH_CURRENT_COMPANY_INFO_PENDING:
|
|
77
67
|
case CLEAR_DATA:
|
|
78
68
|
return state.clear();
|
|
69
|
+
|
|
79
70
|
default:
|
|
80
71
|
return state;
|
|
81
72
|
}
|
package/src/selectors.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import * as Immutable from "immutable";
|
|
2
|
+
import { createSelector } from "reselect";
|
|
3
|
+
import type { State, PagesState } from "./types";
|
|
4
|
+
|
|
5
|
+
const
|
|
6
|
+
pagesSelector = (state: State) => state,
|
|
7
|
+
|
|
8
|
+
rowsPerPage = 50,
|
|
9
|
+
pageSelector = createSelector(
|
|
10
|
+
pagesSelector,
|
|
11
|
+
(_state: State, token: string) => token,
|
|
12
|
+
(pages: PagesState, token: string) => pages.get(token),
|
|
13
|
+
),
|
|
14
|
+
getAllResults = createSelector(
|
|
15
|
+
pageSelector,
|
|
16
|
+
(_state : State, _token : string, items : any) => items,
|
|
17
|
+
(page : any, items : any) => {
|
|
18
|
+
if (typeof page === "undefined") {
|
|
19
|
+
return Immutable.List();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return page.get("ids").map((id : number) => items.get(String(id)));
|
|
23
|
+
},
|
|
24
|
+
),
|
|
25
|
+
getResultsUpToPage = createSelector(
|
|
26
|
+
pageSelector,
|
|
27
|
+
(_state : State, _token : string, items : any) => items,
|
|
28
|
+
(_state : State, _token : string, _items: any, target: any) => target,
|
|
29
|
+
(_state : State, _token : string, _items : any, _target : any, perPage : number) => (
|
|
30
|
+
perPage || rowsPerPage
|
|
31
|
+
),
|
|
32
|
+
(page : any, items : any, target : any, perPage : any) => {
|
|
33
|
+
if (typeof page === "undefined") {
|
|
34
|
+
return Immutable.List();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ids = page.get("ids").slice(0, perPage * target);
|
|
38
|
+
|
|
39
|
+
return ids.map((id : number) => items.get(String(id)));
|
|
40
|
+
}),
|
|
41
|
+
getCurrentTotalResultsCount = createSelector(
|
|
42
|
+
pageSelector,
|
|
43
|
+
(page : any) => {
|
|
44
|
+
if (typeof page === "undefined") {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return page.get("total");
|
|
49
|
+
}),
|
|
50
|
+
isPageFetching = createSelector(
|
|
51
|
+
pageSelector,
|
|
52
|
+
(_state : State, _token : string, target : any) => target,
|
|
53
|
+
(page : any, target : any) => {
|
|
54
|
+
if (typeof page === "undefined") {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return page.get("fetching") && target === page.get("page");
|
|
59
|
+
}),
|
|
60
|
+
isPageFetched = createSelector(
|
|
61
|
+
pageSelector,
|
|
62
|
+
(_state : State, _token : string, target : any) => target,
|
|
63
|
+
(page : any, target : any) => {
|
|
64
|
+
if (typeof page === "undefined") {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return page.get("fetched") && target <= page.get("page");
|
|
69
|
+
}),
|
|
70
|
+
hasPageProblems = createSelector(
|
|
71
|
+
pageSelector,
|
|
72
|
+
(page : any) => {
|
|
73
|
+
if (typeof page === "undefined") {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return page.get("error");
|
|
78
|
+
}),
|
|
79
|
+
getCurrentView = createSelector(
|
|
80
|
+
pageSelector,
|
|
81
|
+
(page : any) => {
|
|
82
|
+
if (typeof page === "undefined") {
|
|
83
|
+
return 1;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return page.get("view");
|
|
87
|
+
}),
|
|
88
|
+
|
|
89
|
+
// these selectors are used for the entire app
|
|
90
|
+
getPaginatorPaginations = (state: any, name: string) => state.getIn(["paginations", name]),
|
|
91
|
+
getPaginatorEntities = (state: any, name: string) => state.getIn(["entities", name]),
|
|
92
|
+
getPaginators = createSelector(
|
|
93
|
+
getPaginatorPaginations,
|
|
94
|
+
getPaginatorEntities,
|
|
95
|
+
(list : any, entities : any) => ({
|
|
96
|
+
list,
|
|
97
|
+
entities,
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
export default {
|
|
102
|
+
getAllResults,
|
|
103
|
+
getCurrentTotalResultsCount,
|
|
104
|
+
getResultsUpToPage,
|
|
105
|
+
isPageFetching,
|
|
106
|
+
isPageFetched,
|
|
107
|
+
hasPageProblems,
|
|
108
|
+
getCurrentView,
|
|
109
|
+
// for state
|
|
110
|
+
getPaginatorPaginations,
|
|
111
|
+
getPaginatorEntities,
|
|
112
|
+
getPaginators,
|
|
113
|
+
};
|