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.
Files changed (106) hide show
  1. package/lib/Info.d.ts +9 -0
  2. package/lib/Info.js +19 -41
  3. package/lib/Info.js.map +1 -0
  4. package/lib/LoadDataItem.d.ts +23 -0
  5. package/lib/LoadDataItem.js +59 -110
  6. package/lib/LoadDataItem.js.map +1 -0
  7. package/lib/LoadPaginator.d.ts +41 -0
  8. package/lib/LoadPaginator.js +109 -216
  9. package/lib/LoadPaginator.js.map +1 -0
  10. package/lib/LoadingButton.d.ts +11 -0
  11. package/lib/LoadingButton.js +22 -75
  12. package/lib/LoadingButton.js.map +1 -0
  13. package/lib/actionTypes.d.ts +9 -0
  14. package/lib/actionTypes.js +13 -16
  15. package/lib/actionTypes.js.map +1 -0
  16. package/lib/actions.d.ts +42 -0
  17. package/lib/actions.js +82 -136
  18. package/lib/actions.js.map +1 -0
  19. package/lib/agent.d.ts +8 -0
  20. package/lib/agent.js +37 -86
  21. package/lib/agent.js.map +1 -0
  22. package/lib/createDataReducer.d.ts +14 -0
  23. package/lib/createDataReducer.js +60 -79
  24. package/lib/createDataReducer.js.map +1 -0
  25. package/lib/createItemReducer.d.ts +9 -0
  26. package/lib/createItemReducer.js +14 -32
  27. package/lib/createItemReducer.js.map +1 -0
  28. package/lib/createPaginator.d.ts +14 -0
  29. package/lib/createPaginator.js +83 -140
  30. package/lib/createPaginator.js.map +1 -0
  31. package/lib/index.d.ts +11 -0
  32. package/lib/index.js +30 -58
  33. package/lib/index.js.map +1 -0
  34. package/lib/paginatorMiddleware.d.ts +5 -0
  35. package/lib/paginatorMiddleware.js +68 -102
  36. package/lib/paginatorMiddleware.js.map +1 -0
  37. package/lib/reducerItemData.d.ts +18 -0
  38. package/lib/reducerItemData.js +56 -63
  39. package/lib/reducerItemData.js.map +1 -0
  40. package/lib/reducerOperations.d.ts +3 -0
  41. package/lib/reducerOperations.js +69 -114
  42. package/lib/reducerOperations.js.map +1 -0
  43. package/lib/reducers.d.ts +4 -0
  44. package/lib/reducers.js +75 -87
  45. package/lib/reducers.js.map +1 -0
  46. package/lib/selectors.d.ts +58 -0
  47. package/lib/selectors.js +79 -129
  48. package/lib/selectors.js.map +1 -0
  49. package/lib/types/actions.d.ts +85 -0
  50. package/lib/types/actions.js +3 -1
  51. package/lib/types/actions.js.map +1 -0
  52. package/lib/types/index.d.ts +21 -0
  53. package/lib/types/index.js +16 -29
  54. package/lib/types/index.js.map +1 -0
  55. package/lib/types/state.d.ts +6 -0
  56. package/lib/types/state.js +3 -1
  57. package/lib/types/state.js.map +1 -0
  58. package/lib/util.d.ts +9 -0
  59. package/lib/util.js +49 -73
  60. package/lib/util.js.map +1 -0
  61. package/lib/words.d.ts +9 -0
  62. package/lib/words.js +12 -15
  63. package/lib/words.js.map +1 -0
  64. package/package.json +35 -21
  65. package/src/Info.tsx +29 -0
  66. package/src/LoadDataItem.tsx +84 -0
  67. package/src/{LoadPaginator.jsx → LoadPaginator.tsx} +77 -94
  68. package/src/LoadingButton.tsx +40 -0
  69. package/src/__tests__/createPaginator.spec.js +6 -6
  70. package/src/__tests__/reducers.spec.js +28 -28
  71. package/src/__tests__/selectors.spec.js +80 -81
  72. package/src/{actionTypes.js → actionTypes.ts} +0 -4
  73. package/src/actions.ts +157 -0
  74. package/src/agent.ts +62 -0
  75. package/src/createDataReducer.ts +91 -0
  76. package/src/{createItemReducer.js → createItemReducer.ts} +7 -13
  77. package/src/createPaginator.ts +107 -0
  78. package/src/{index.js → index.ts} +0 -13
  79. package/src/paginatorMiddleware.ts +93 -0
  80. package/src/reducerItemData.ts +74 -0
  81. package/src/reducerOperations.ts +101 -0
  82. package/src/{reducers.js → reducers.ts} +9 -18
  83. package/src/selectors.ts +113 -0
  84. package/src/types/{actions.js → actions.ts} +27 -39
  85. package/src/types/{index.js → index.ts} +6 -13
  86. package/src/types/{state.js → state.ts} +3 -6
  87. package/src/util.ts +51 -0
  88. package/src/{words.js → words.ts} +0 -2
  89. package/lib/__tests__/actions.spec.js +0 -117
  90. package/lib/__tests__/agent.spec.js +0 -32
  91. package/lib/__tests__/createPaginator.spec.js +0 -39
  92. package/lib/__tests__/reducers.spec.js +0 -126
  93. package/lib/__tests__/selectors.spec.js +0 -162
  94. package/lib/__tests__/setup.js +0 -3
  95. package/src/Info.jsx +0 -34
  96. package/src/LoadDataItem.jsx +0 -116
  97. package/src/LoadingButton.jsx +0 -55
  98. package/src/actions.js +0 -153
  99. package/src/agent.js +0 -55
  100. package/src/createDataReducer.js +0 -94
  101. package/src/createPaginator.js +0 -131
  102. package/src/paginatorMiddleware.js +0 -94
  103. package/src/reducerItemData.jsx +0 -64
  104. package/src/reducerOperations.jsx +0 -112
  105. package/src/selectors.js +0 -143
  106. package/src/util.js +0 -71
@@ -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;
@@ -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
- };