redux-paginator-immutable 17.0.16 → 17.0.18
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/__tests__/createPaginator.spec.js +2 -2
- package/lib/__tests__/reducers.spec.js +2 -3
- package/lib/__tests__/selectors.spec.js +11 -13
- package/package.json +14 -7
- 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 +98 -0
- package/src/{createItemReducer.js → createItemReducer.ts} +6 -12
- 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} +3 -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/Info.js +0 -41
- package/lib/LoadDataItem.js +0 -110
- package/lib/LoadPaginator.js +0 -216
- package/lib/LoadingButton.js +0 -75
- package/lib/actionTypes.js +0 -16
- package/lib/actions.js +0 -136
- package/lib/agent.js +0 -86
- package/lib/createDataReducer.js +0 -83
- package/lib/createItemReducer.js +0 -36
- package/lib/createPaginator.js +0 -141
- package/lib/index.js +0 -58
- package/lib/paginatorMiddleware.js +0 -102
- package/lib/reducerItemData.js +0 -63
- package/lib/reducerOperations.js +0 -114
- package/lib/reducers.js +0 -87
- package/lib/selectors.js +0 -129
- package/lib/types/actions.js +0 -1
- package/lib/types/index.js +0 -29
- package/lib/types/state.js +0 -1
- package/lib/util.js +0 -73
- package/lib/words.js +0 -15
- 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
package/src/createDataReducer.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable max-lines-per-function, */
|
|
3
|
-
|
|
4
|
-
import type { PaginatorSettings } from "./types";
|
|
5
|
-
|
|
6
|
-
import { createSelector } from "reselect";
|
|
7
|
-
import * as Immutable from "immutable";
|
|
8
|
-
|
|
9
|
-
/*
|
|
10
|
-
Use to create a complex list where you need more information about
|
|
11
|
-
the item. For instance, if you DO need to fetch more data or to
|
|
12
|
-
manipulate it
|
|
13
|
-
*/
|
|
14
|
-
const createDataReducer = ({ key } : PaginatorSettings) => {
|
|
15
|
-
const
|
|
16
|
-
getItems = (state : any) => state.getIn([
|
|
17
|
-
"entities",
|
|
18
|
-
key,
|
|
19
|
-
]),
|
|
20
|
-
getItem = createSelector(
|
|
21
|
-
getItems,
|
|
22
|
-
(state, id) => id,
|
|
23
|
-
(data, id) => data.get(id) || Immutable.Map()
|
|
24
|
-
),
|
|
25
|
-
getItemsList = createSelector(
|
|
26
|
-
getItems,
|
|
27
|
-
(data) => data.map((current) => current.get("Data")).toList()
|
|
28
|
-
),
|
|
29
|
-
getItemData = createSelector(
|
|
30
|
-
getItems,
|
|
31
|
-
(state, id) => id,
|
|
32
|
-
(data, id) => {
|
|
33
|
-
if (typeof data === "undefined") {
|
|
34
|
-
return Immutable.Map();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return data.getIn([
|
|
38
|
-
id,
|
|
39
|
-
"Data",
|
|
40
|
-
]);
|
|
41
|
-
}
|
|
42
|
-
),
|
|
43
|
-
getItemHasError = createSelector(
|
|
44
|
-
getItem,
|
|
45
|
-
(data) => {
|
|
46
|
-
if (typeof data === "undefined") {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return data.get("error") === true;
|
|
51
|
-
}
|
|
52
|
-
),
|
|
53
|
-
getIsFetchingItemInfo = createSelector(
|
|
54
|
-
getItem,
|
|
55
|
-
(data) => {
|
|
56
|
-
if (typeof data === "undefined") {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return data.get("fetching");
|
|
61
|
-
}
|
|
62
|
-
),
|
|
63
|
-
getItemIsFetched = createSelector(
|
|
64
|
-
getItem,
|
|
65
|
-
(data) => {
|
|
66
|
-
if (typeof data === "undefined") {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return data.get("fetched");
|
|
71
|
-
}
|
|
72
|
-
),
|
|
73
|
-
getShouldFetchItemInfo = createSelector(
|
|
74
|
-
getIsFetchingItemInfo,
|
|
75
|
-
getItemIsFetched,
|
|
76
|
-
getItemHasError,
|
|
77
|
-
(isFetching, isFetched, hasError) => (
|
|
78
|
-
!isFetched && !isFetching && !hasError
|
|
79
|
-
)
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
getItems,
|
|
84
|
-
getItem,
|
|
85
|
-
getItemsList,
|
|
86
|
-
getItemData,
|
|
87
|
-
getItemHasError,
|
|
88
|
-
getIsFetchingItemInfo,
|
|
89
|
-
getItemIsFetched,
|
|
90
|
-
getShouldFetchItemInfo,
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
export default createDataReducer;
|
package/src/createPaginator.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable max-lines-per-function, */
|
|
3
|
-
|
|
4
|
-
import type { PaginatorSettings } from "./types";
|
|
5
|
-
|
|
6
|
-
type EndpointData = {
|
|
7
|
-
path : string;
|
|
8
|
-
cb : () => string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/*
|
|
13
|
-
if (typeof Endpoint === "object") {
|
|
14
|
-
return {
|
|
15
|
-
path : Endpoint.path,
|
|
16
|
-
cb : Endpoint.cb,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return {
|
|
21
|
-
path : Endpoint,
|
|
22
|
-
};
|
|
23
|
-
*/
|
|
24
|
-
type Endpoint = EndpointData | string;
|
|
25
|
-
|
|
26
|
-
type CreatePaginator = (endpoint: Endpoint, info: PaginatorSettings) => any;
|
|
27
|
-
|
|
28
|
-
type OnlyForEndpoint = (endpoint : string, reducer : any) =>
|
|
29
|
-
(state? : any, action : { meta: { endpoint : string }}) => any;
|
|
30
|
-
|
|
31
|
-
import {
|
|
32
|
-
pages as pagesReducer,
|
|
33
|
-
items as itemsReducer,
|
|
34
|
-
dataItems as dataItemsReducer,
|
|
35
|
-
} from "./reducers";
|
|
36
|
-
|
|
37
|
-
import actions from "./actions";
|
|
38
|
-
|
|
39
|
-
import * as Immutable from "immutable";
|
|
40
|
-
|
|
41
|
-
import { rowsPerLoad as defaultRowsPerLoad } from "x25/utility/others";
|
|
42
|
-
|
|
43
|
-
export const onlyForEndpoint : OnlyForEndpoint = (endpoint, reducer) => (
|
|
44
|
-
(state = Immutable.Map(), action) => {
|
|
45
|
-
if (typeof action.meta === "undefined" || action.meta.endpoint !== endpoint) {
|
|
46
|
-
return state;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return reducer(state, action);
|
|
50
|
-
}
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const getEndpoint = (data : Endpoint) => {
|
|
54
|
-
if (typeof data === "object") {
|
|
55
|
-
return {
|
|
56
|
-
path : data.path,
|
|
57
|
-
cb : data.cb,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
path : data,
|
|
63
|
-
cb : null,
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const createPaginator : CreatePaginator = (
|
|
68
|
-
(endpointData : Endpoint, settings : PaginatorSettings) => {
|
|
69
|
-
const {
|
|
70
|
-
path: endpoint,
|
|
71
|
-
cb: endpointCb,
|
|
72
|
-
} = getEndpoint(endpointData);
|
|
73
|
-
|
|
74
|
-
const {
|
|
75
|
-
key,
|
|
76
|
-
normalizeDataItem,
|
|
77
|
-
dataItemURL,
|
|
78
|
-
manageEntity,
|
|
79
|
-
resultsKey,
|
|
80
|
-
totalKey = "Total",
|
|
81
|
-
pageArgName = "page",
|
|
82
|
-
idKey = "ID",
|
|
83
|
-
rowsPerLoad = defaultRowsPerLoad,
|
|
84
|
-
manipulateItems = (items) => items,
|
|
85
|
-
} = settings;
|
|
86
|
-
|
|
87
|
-
const endpointedActions = ({
|
|
88
|
-
requestPage: (page : number, token : string) => actions.requestPage({
|
|
89
|
-
endpoint,
|
|
90
|
-
endpointCb,
|
|
91
|
-
manageEntity,
|
|
92
|
-
resultsKey,
|
|
93
|
-
totalKey,
|
|
94
|
-
pageArgName,
|
|
95
|
-
idKey,
|
|
96
|
-
page,
|
|
97
|
-
token,
|
|
98
|
-
}),
|
|
99
|
-
resetView (token) {
|
|
100
|
-
return actions.resetView(endpoint, token);
|
|
101
|
-
},
|
|
102
|
-
changeView ({ view, token } : { view : number; token : string; }) {
|
|
103
|
-
return actions.changeView(endpoint, {
|
|
104
|
-
view,
|
|
105
|
-
token,
|
|
106
|
-
});
|
|
107
|
-
},
|
|
108
|
-
clearData () {
|
|
109
|
-
return actions.clearData(endpoint);
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
return ({
|
|
114
|
-
key,
|
|
115
|
-
normalizeDataItem,
|
|
116
|
-
dataItemURL,
|
|
117
|
-
manipulateItems,
|
|
118
|
-
rowsPerLoad,
|
|
119
|
-
manageEntity,
|
|
120
|
-
resultsKey,
|
|
121
|
-
endpoint,
|
|
122
|
-
|
|
123
|
-
pagesReducer : onlyForEndpoint(endpoint, pagesReducer),
|
|
124
|
-
itemsReducer : onlyForEndpoint(endpoint, itemsReducer),
|
|
125
|
-
|
|
126
|
-
dataItemsReducer: onlyForEndpoint(endpoint, dataItemsReducer),
|
|
127
|
-
|
|
128
|
-
...endpointedActions,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
);
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable no-console, max-lines-per-function */
|
|
3
|
-
|
|
4
|
-
import type { Dispatch, Action } from "./types";
|
|
5
|
-
|
|
6
|
-
import { REQUEST_PAGE } from "./actionTypes";
|
|
7
|
-
import actions from "./actions";
|
|
8
|
-
import { fetchPage } from "./agent";
|
|
9
|
-
|
|
10
|
-
const { receivePage } = actions;
|
|
11
|
-
|
|
12
|
-
const paginatorMiddleware = ({ dispatch } : { dispatch : Dispatch}) => (
|
|
13
|
-
(next : any) => (action : Action) => {
|
|
14
|
-
if (action.type === REQUEST_PAGE) {
|
|
15
|
-
const {
|
|
16
|
-
meta: {
|
|
17
|
-
endpoint,
|
|
18
|
-
endpointCb,
|
|
19
|
-
manageEntity,
|
|
20
|
-
resultsKey,
|
|
21
|
-
totalKey,
|
|
22
|
-
pageArgName,
|
|
23
|
-
idKey,
|
|
24
|
-
},
|
|
25
|
-
payload: {
|
|
26
|
-
page,
|
|
27
|
-
token,
|
|
28
|
-
},
|
|
29
|
-
} = action;
|
|
30
|
-
|
|
31
|
-
dispatch((dispatch2) => {
|
|
32
|
-
const markAsError = () => {
|
|
33
|
-
dispatch2(receivePage({
|
|
34
|
-
endpoint,
|
|
35
|
-
endpointCb,
|
|
36
|
-
manageEntity,
|
|
37
|
-
pageArgName,
|
|
38
|
-
idKey,
|
|
39
|
-
page,
|
|
40
|
-
error : true,
|
|
41
|
-
token,
|
|
42
|
-
items : [],
|
|
43
|
-
total : 0,
|
|
44
|
-
}));
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const rawPath = typeof endpointCb === "function" ? endpointCb(token) : endpoint;
|
|
48
|
-
const path = rawPath === null ? "" : rawPath;
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
fetchPage({
|
|
52
|
-
endpoint: path,
|
|
53
|
-
pageArgName,
|
|
54
|
-
page,
|
|
55
|
-
token,
|
|
56
|
-
}).
|
|
57
|
-
then((res) => {
|
|
58
|
-
const { response } = res;
|
|
59
|
-
let
|
|
60
|
-
results = [],
|
|
61
|
-
total = 0;
|
|
62
|
-
|
|
63
|
-
if (typeof resultsKey === "undefined") {
|
|
64
|
-
results = response;
|
|
65
|
-
} else {
|
|
66
|
-
results = response[resultsKey];
|
|
67
|
-
total = response[totalKey];
|
|
68
|
-
}
|
|
69
|
-
dispatch2(receivePage({
|
|
70
|
-
endpoint,
|
|
71
|
-
endpointCb,
|
|
72
|
-
manageEntity,
|
|
73
|
-
pageArgName,
|
|
74
|
-
idKey,
|
|
75
|
-
page,
|
|
76
|
-
error : false,
|
|
77
|
-
token,
|
|
78
|
-
items : results,
|
|
79
|
-
total,
|
|
80
|
-
}));
|
|
81
|
-
}).
|
|
82
|
-
catch(() => {
|
|
83
|
-
markAsError();
|
|
84
|
-
});
|
|
85
|
-
} catch (err) {
|
|
86
|
-
markAsError();
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
return next(action);
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
export default paginatorMiddleware;
|
package/src/reducerItemData.jsx
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
type FetchItemDataFulFilledProps = {
|
|
4
|
-
payload: {
|
|
5
|
-
Data: any;
|
|
6
|
-
};
|
|
7
|
-
meta: {
|
|
8
|
-
id: string;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
import * as Immutable from "immutable";
|
|
13
|
-
|
|
14
|
-
const defaultOptions = Immutable.Map({
|
|
15
|
-
fetching : true,
|
|
16
|
-
fetched : false,
|
|
17
|
-
error : false,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
fetchItemDataPending = (state : any, { meta : { id } } : { meta : { id : string }}) => (
|
|
22
|
-
state.update(id, (current) => {
|
|
23
|
-
if (typeof current === "undefined") {
|
|
24
|
-
return defaultOptions;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return current.mergeDeep(defaultOptions);
|
|
28
|
-
})
|
|
29
|
-
),
|
|
30
|
-
fetchItemDataRejected = (state : any, { meta : { id } } : { meta : { id : string }}) => (
|
|
31
|
-
state.update((id), (current) => {
|
|
32
|
-
if (typeof current === "undefined") {
|
|
33
|
-
return current;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return current.mergeDeep(Immutable.Map({
|
|
37
|
-
fetching : false,
|
|
38
|
-
fetched : false,
|
|
39
|
-
error : true,
|
|
40
|
-
}));
|
|
41
|
-
})
|
|
42
|
-
),
|
|
43
|
-
fetchItemDataFulFilled = (state : any,
|
|
44
|
-
{ payload : { Data }, meta : { id } } : FetchItemDataFulFilledProps
|
|
45
|
-
) => (
|
|
46
|
-
state.update((id), (current) => {
|
|
47
|
-
if (typeof current === "undefined") {
|
|
48
|
-
return current;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return current.mergeDeep(Immutable.Map({
|
|
52
|
-
Data,
|
|
53
|
-
fetching : false,
|
|
54
|
-
fetched : true,
|
|
55
|
-
error : false,
|
|
56
|
-
}));
|
|
57
|
-
})
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
export {
|
|
61
|
-
fetchItemDataPending,
|
|
62
|
-
fetchItemDataRejected,
|
|
63
|
-
fetchItemDataFulFilled,
|
|
64
|
-
};
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
ItemsState,
|
|
5
|
-
PagesState,
|
|
6
|
-
|
|
7
|
-
Action,
|
|
8
|
-
ReceivePageAction,
|
|
9
|
-
ChangeViewAction,
|
|
10
|
-
RequestPageAction,
|
|
11
|
-
} from "./types";
|
|
12
|
-
|
|
13
|
-
import * as Immutable from "immutable";
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
requestPage = (state : PagesState, action : RequestPageAction) => {
|
|
17
|
-
const { payload : { token, page } } = action;
|
|
18
|
-
|
|
19
|
-
const elements = Immutable.Map({
|
|
20
|
-
page,
|
|
21
|
-
error : false,
|
|
22
|
-
fetching : true,
|
|
23
|
-
fetched : false,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (state.has(token)) {
|
|
27
|
-
return state.update(token, (current) => (
|
|
28
|
-
current.merge(elements)
|
|
29
|
-
));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const init = elements.merge({
|
|
33
|
-
view : 1,
|
|
34
|
-
token,
|
|
35
|
-
ids : Immutable.List(),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
return state.set(token, init);
|
|
39
|
-
},
|
|
40
|
-
receivePage = (state : PagesState, action : ReceivePageAction) => {
|
|
41
|
-
const {
|
|
42
|
-
meta: { idKey },
|
|
43
|
-
payload : { token, items, total, error },
|
|
44
|
-
} = action;
|
|
45
|
-
|
|
46
|
-
if (state.has(token)) {
|
|
47
|
-
const hasError = error === true;
|
|
48
|
-
|
|
49
|
-
return state.update(token, (current) => (
|
|
50
|
-
current.update((page) => (
|
|
51
|
-
current.merge(
|
|
52
|
-
Immutable.Map({
|
|
53
|
-
ids: page.get("ids").concat(
|
|
54
|
-
Immutable.List(
|
|
55
|
-
items.map((item) => String(item[idKey]))
|
|
56
|
-
)
|
|
57
|
-
),
|
|
58
|
-
fetching : false,
|
|
59
|
-
error : hasError,
|
|
60
|
-
fetched : true,
|
|
61
|
-
total : hasError ? page.get("total") : total,
|
|
62
|
-
})
|
|
63
|
-
)
|
|
64
|
-
))
|
|
65
|
-
));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return state;
|
|
69
|
-
},
|
|
70
|
-
performChangeView = (state : PagesState, action : any, view : number) => {
|
|
71
|
-
const { payload : { token } } = action;
|
|
72
|
-
|
|
73
|
-
const item = state.get(token);
|
|
74
|
-
|
|
75
|
-
if (typeof item === "undefined") {
|
|
76
|
-
return state;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return state.update(token, (current) => (
|
|
80
|
-
current.set("view", view)
|
|
81
|
-
));
|
|
82
|
-
},
|
|
83
|
-
changeView = (state : PagesState, action : ChangeViewAction) => (
|
|
84
|
-
performChangeView(state, action, action.payload.view)
|
|
85
|
-
),
|
|
86
|
-
resetView = (state : PagesState, action : Action) => (
|
|
87
|
-
performChangeView(state, action, 1)
|
|
88
|
-
),
|
|
89
|
-
receivePageItems = (state : ItemsState, action : ReceivePageAction) => {
|
|
90
|
-
const { payload, meta } = action;
|
|
91
|
-
const { items : payloadItems } = payload;
|
|
92
|
-
const { idKey, manageEntity } = meta;
|
|
93
|
-
|
|
94
|
-
const manage : any = (
|
|
95
|
-
typeof manageEntity === "function"
|
|
96
|
-
) ? manageEntity : (item) => Immutable.Map(item);
|
|
97
|
-
|
|
98
|
-
const newItems = payloadItems.reduce((previous, item) => (
|
|
99
|
-
previous.set(String(item[idKey]), manage(item, state, idKey))
|
|
100
|
-
), Immutable.Map());
|
|
101
|
-
|
|
102
|
-
return state.merge(newItems);
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export {
|
|
106
|
-
requestPage,
|
|
107
|
-
receivePage,
|
|
108
|
-
performChangeView,
|
|
109
|
-
changeView,
|
|
110
|
-
resetView,
|
|
111
|
-
receivePageItems,
|
|
112
|
-
};
|
package/src/selectors.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable no-undefined */
|
|
3
|
-
|
|
4
|
-
import type { State, PagesState } from "./types";
|
|
5
|
-
|
|
6
|
-
import * as Immutable from "immutable";
|
|
7
|
-
import { createSelector } from "reselect";
|
|
8
|
-
|
|
9
|
-
const pagesSelector = (state : State) => state;
|
|
10
|
-
|
|
11
|
-
const rowsPerPage = 50;
|
|
12
|
-
|
|
13
|
-
const pageSelector = createSelector(
|
|
14
|
-
pagesSelector,
|
|
15
|
-
(state : State, token : string) => token,
|
|
16
|
-
(pages : PagesState, token : string) => pages.get(token)
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
const getAllResults = createSelector(
|
|
20
|
-
pageSelector,
|
|
21
|
-
(state, token, items) => items,
|
|
22
|
-
(page, items) => {
|
|
23
|
-
if (typeof page === "undefined") {
|
|
24
|
-
return Immutable.List();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return page.get("ids").map((id) => items.get(String(id)));
|
|
28
|
-
}
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const getResultsUpToPage = createSelector(
|
|
32
|
-
pageSelector,
|
|
33
|
-
(state, token, items) => items,
|
|
34
|
-
(state, token, items, target) => target,
|
|
35
|
-
(state, token, items, target, perPage) => perPage || rowsPerPage,
|
|
36
|
-
(page, items, target, perPage) => {
|
|
37
|
-
if (typeof page === "undefined") {
|
|
38
|
-
return Immutable.List();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const ids = page.get("ids").slice(0, (perPage * target));
|
|
42
|
-
|
|
43
|
-
return ids.map((id) => items.get(String(id)));
|
|
44
|
-
}
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const getCurrentTotalResultsCount = createSelector(
|
|
48
|
-
pageSelector,
|
|
49
|
-
(page) => {
|
|
50
|
-
if (typeof page === "undefined") {
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return page.get("total");
|
|
55
|
-
}
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const isPageFetching = createSelector(
|
|
59
|
-
pageSelector,
|
|
60
|
-
(state, token, target) => target,
|
|
61
|
-
(page, target) => {
|
|
62
|
-
if (typeof page === "undefined") {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return (
|
|
67
|
-
page.get("fetching") &&
|
|
68
|
-
target === page.get("page")
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const isPageFetched = createSelector(
|
|
74
|
-
pageSelector,
|
|
75
|
-
(state, token, target) => target,
|
|
76
|
-
(page, target) => {
|
|
77
|
-
if (typeof page === "undefined") {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return (
|
|
82
|
-
page.get("fetched") &&
|
|
83
|
-
target <= page.get("page")
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const hasPageProblems = createSelector(
|
|
89
|
-
pageSelector,
|
|
90
|
-
(page) => {
|
|
91
|
-
if (typeof page === "undefined") {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return page.get("error");
|
|
96
|
-
}
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
const getCurrentView = createSelector(
|
|
100
|
-
pageSelector,
|
|
101
|
-
(page) => {
|
|
102
|
-
if (typeof page === "undefined") {
|
|
103
|
-
return 1;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return page.get("view");
|
|
107
|
-
}
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
// these selectors are used for the entire app
|
|
112
|
-
const
|
|
113
|
-
getPaginatorPaginations = (state : any, name : string) => state.getIn([
|
|
114
|
-
"paginations",
|
|
115
|
-
name,
|
|
116
|
-
]),
|
|
117
|
-
getPaginatorEntities = (state : any, name : string) => state.getIn([
|
|
118
|
-
"entities",
|
|
119
|
-
name,
|
|
120
|
-
]),
|
|
121
|
-
getPaginators = createSelector(
|
|
122
|
-
getPaginatorPaginations,
|
|
123
|
-
getPaginatorEntities,
|
|
124
|
-
(list, entities) => ({
|
|
125
|
-
list,
|
|
126
|
-
entities,
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
export default {
|
|
131
|
-
getAllResults,
|
|
132
|
-
getCurrentTotalResultsCount,
|
|
133
|
-
getResultsUpToPage,
|
|
134
|
-
isPageFetching,
|
|
135
|
-
isPageFetched,
|
|
136
|
-
hasPageProblems,
|
|
137
|
-
getCurrentView,
|
|
138
|
-
|
|
139
|
-
// for state
|
|
140
|
-
getPaginatorPaginations,
|
|
141
|
-
getPaginatorEntities,
|
|
142
|
-
getPaginators,
|
|
143
|
-
};
|
package/src/util.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable no-duplicate-imports */
|
|
3
|
-
|
|
4
|
-
type NormalizrEntity = (data : any) => any;
|
|
5
|
-
|
|
6
|
-
import type { Map as ImmutableMap } from "immutable";
|
|
7
|
-
|
|
8
|
-
import * as Immutable from "immutable";
|
|
9
|
-
|
|
10
|
-
import { createSelector } from "reselect";
|
|
11
|
-
|
|
12
|
-
type ManageDataEntity = ImmutableMap<string, *>;
|
|
13
|
-
|
|
14
|
-
// simple item
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
manageEntity = (
|
|
18
|
-
(normalizrEntity : NormalizrEntity) => (item : any, state : any, keyID : string)
|
|
19
|
-
: ManageDataEntity => {
|
|
20
|
-
// prevent rewrite
|
|
21
|
-
const old = state.get(String(item[keyID]));
|
|
22
|
-
|
|
23
|
-
if (typeof old === "undefined") {
|
|
24
|
-
return Immutable.Map(normalizrEntity(item));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return old;
|
|
28
|
-
}
|
|
29
|
-
),
|
|
30
|
-
manipulateItems = (sortKey? : string = "ID") => createSelector(
|
|
31
|
-
(data) => data,
|
|
32
|
-
(data) => (
|
|
33
|
-
data.sortBy((current) => -current.get(sortKey))
|
|
34
|
-
)
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
// data item
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
manageDataEntity = (
|
|
41
|
-
(normalizrEntity : NormalizrEntity) => (item : any, state : any, keyID : string)
|
|
42
|
-
: ManageDataEntity => {
|
|
43
|
-
// prevent rewrite
|
|
44
|
-
const old = state.get(String(item[keyID]));
|
|
45
|
-
|
|
46
|
-
if (typeof old === "undefined") {
|
|
47
|
-
return Immutable.Map({
|
|
48
|
-
Data: normalizrEntity(item),
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return old;
|
|
53
|
-
}
|
|
54
|
-
),
|
|
55
|
-
manipulateDataItems = (sortKey? : string = "ID") => createSelector(
|
|
56
|
-
(data) => data,
|
|
57
|
-
(data) => (
|
|
58
|
-
data.sortBy((current) => -current.getIn([
|
|
59
|
-
"Data",
|
|
60
|
-
sortKey,
|
|
61
|
-
]))
|
|
62
|
-
)
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
export {
|
|
66
|
-
manageDataEntity,
|
|
67
|
-
manipulateDataItems,
|
|
68
|
-
|
|
69
|
-
manipulateItems,
|
|
70
|
-
manageEntity,
|
|
71
|
-
};
|