orc-shared 5.10.0-dev.7 → 5.10.0-dev.9

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.
@@ -0,0 +1,203 @@
1
+ import Immutable from "immutable";
2
+ import { isObject } from "lodash";
3
+
4
+ export const ListInfoPropertyName = "listInfo";
5
+
6
+ const standardInfoKeys = ["scope", "page", "filters", "sorting", "totalCount", "nextPageToLoad", "index", "list"];
7
+
8
+ class ListReducerHelper {
9
+ constructor(groupPropertyName) {
10
+ this.groupPropertyName = groupPropertyName;
11
+ }
12
+
13
+ setNextPageToLoad = (state, nextPageToLoad) => {
14
+ return state.setIn([this.groupPropertyName, "nextPageToLoad"], nextPageToLoad);
15
+ };
16
+
17
+ setResults = (state, listInfo, forceReset = false) => {
18
+ return state.withMutations(s => {
19
+ const page = s.getIn([this.groupPropertyName, "nextPageToLoad"]);
20
+
21
+ const entities = Immutable.fromJS(listInfo.indexEntities || {});
22
+ if (page === 1 || forceReset) {
23
+ s.setIn([this.groupPropertyName, "index"], entities);
24
+ s.setIn([this.groupPropertyName, "list"], Immutable.fromJS(listInfo.listEntities));
25
+ } else {
26
+ s.mergeIn([this.groupPropertyName, "index"], entities);
27
+ s.setIn(
28
+ [this.groupPropertyName, "list"],
29
+ s.getIn([this.groupPropertyName, "list"]).concat(Immutable.fromJS(listInfo.listEntities)),
30
+ );
31
+ }
32
+
33
+ s.setIn([this.groupPropertyName, "totalCount"], listInfo.totalCount);
34
+ });
35
+ };
36
+
37
+ setCurrentInfo = (state, { resetList, scope, filters, sorting, ...others } = {}) => {
38
+ return state.withMutations(s => {
39
+ if (resetList) {
40
+ s.setIn([this.groupPropertyName, "nextPageToLoad"], 1);
41
+ s.setIn([this.groupPropertyName, "page"], null);
42
+ s.setIn([this.groupPropertyName, "index"], Immutable.fromJS({}));
43
+ s.setIn([this.groupPropertyName, "list"], Immutable.fromJS([]));
44
+ s.setIn([this.groupPropertyName, "totalCount"], 0);
45
+ } else {
46
+ const nextPageToLoad = state.getIn([this.groupPropertyName, "nextPageToLoad"]);
47
+ s.setIn([this.groupPropertyName, "nextPageToLoad"], nextPageToLoad);
48
+ s.setIn([this.groupPropertyName, "page"], nextPageToLoad);
49
+ }
50
+
51
+ s.setIn([this.groupPropertyName, "scope"], scope ?? null);
52
+ s.setIn([this.groupPropertyName, "filters"], Immutable.fromJS(filters ?? null));
53
+ s.setIn([this.groupPropertyName, "sorting"], Immutable.fromJS(sorting ?? null));
54
+
55
+ const otherKeys = Object.keys(others);
56
+
57
+ s.get(this.groupPropertyName)
58
+ .keySeq()
59
+ .forEach(key => {
60
+ if (!standardInfoKeys.includes(key) && !otherKeys.includes(key)) {
61
+ s.removeIn([this.groupPropertyName, key]);
62
+ }
63
+ });
64
+
65
+ otherKeys.forEach(key => {
66
+ s.setIn([this.groupPropertyName, key], isObject(others[key]) ? Immutable.fromJS(others[key]) : others[key]);
67
+ });
68
+ });
69
+ };
70
+
71
+ addIndexWithMutations = (mutator, id, value) => {
72
+ mutator.setIn([this.groupPropertyName, "index", id], Immutable.fromJS(value));
73
+ };
74
+
75
+ appendIdToListWithMutations = (mutator, id) => {
76
+ mutator.setIn([this.groupPropertyName, "list"], mutator.getIn([this.groupPropertyName, "list"]).push(id));
77
+ };
78
+
79
+ updateIndexWithMutations = (mutator, id, value) => {
80
+ const key = [this.groupPropertyName, "index", id];
81
+ if (mutator.getIn(key)) {
82
+ mutator.setIn(key, Immutable.fromJS(value));
83
+ }
84
+ };
85
+
86
+ removeFromIndexWithMutations = (mutator, id) => {
87
+ mutator.removeIn([this.groupPropertyName, "index", id]);
88
+ };
89
+
90
+ resetListInfo = (state, propertiesToKeep = {}) => {
91
+ let updatedState = state
92
+ .setIn([this.groupPropertyName, "nextPageToLoad"], 1)
93
+ .setIn([this.groupPropertyName, "index"], Immutable.fromJS({}))
94
+ .setIn([this.groupPropertyName, "list"], Immutable.fromJS([]))
95
+ .setIn([this.groupPropertyName, "totalCount"], 0);
96
+
97
+ if (!propertiesToKeep.filters) {
98
+ updatedState = updatedState.setIn([this.groupPropertyName, "filters"], null);
99
+ }
100
+
101
+ if (!propertiesToKeep.sorting) {
102
+ updatedState = updatedState.setIn([this.groupPropertyName, "sorting"], null);
103
+ }
104
+
105
+ if (!propertiesToKeep.scope) {
106
+ updatedState = updatedState.setIn([this.groupPropertyName, "scope"], null);
107
+ }
108
+
109
+ if (!propertiesToKeep.page) {
110
+ updatedState = updatedState.setIn([this.groupPropertyName, "page"], null);
111
+ }
112
+
113
+ return updatedState;
114
+ };
115
+ }
116
+
117
+ class ListSelectorHelper {
118
+ constructor(groupPropertyName) {
119
+ this.groupPropertyName = groupPropertyName;
120
+ }
121
+
122
+ getCurrentInfo = state => {
123
+ const listInfo = state.get(this.groupPropertyName) || Immutable.Map();
124
+
125
+ const info = {
126
+ currentScope: listInfo.get("scope"),
127
+ currentPage: listInfo.get("page"),
128
+ currentFilters: listInfo.get("filters")?.toJS(),
129
+ currentSorting: listInfo.get("sorting")?.toJS(),
130
+ totalCount: listInfo.get("totalCount"),
131
+ };
132
+
133
+ listInfo.mapKeys((key, value) => {
134
+ if (standardInfoKeys.includes(key)) {
135
+ // standard key, nothing to do
136
+ } else {
137
+ info[key] = value?.toJS ? value.toJS() : value;
138
+ }
139
+ });
140
+
141
+ return info;
142
+ };
143
+
144
+ getIndex = state => {
145
+ return state.getIn([this.groupPropertyName, "index"]);
146
+ };
147
+
148
+ getList = state => {
149
+ return state.getIn([this.groupPropertyName, "list"]);
150
+ };
151
+
152
+ getNextPageToLoad = state => {
153
+ return state.getIn([this.groupPropertyName, "nextPageToLoad"]);
154
+ };
155
+ }
156
+
157
+ class ListHelper {
158
+ constructor(groupPropertyName = ListInfoPropertyName) {
159
+ this.groupPropertyName = groupPropertyName;
160
+ this.reducer = new ListReducerHelper(this.groupPropertyName);
161
+ this.selector = new ListSelectorHelper(this.groupPropertyName);
162
+ }
163
+
164
+ static defaultInstance = new ListHelper();
165
+ static reducer = ListHelper.defaultInstance.reducer;
166
+ static selector = ListHelper.defaultInstance.selector;
167
+ static createInitialListInfo = ListHelper.defaultInstance.createInitialListInfo;
168
+ static createListInfoFrom = ListHelper.defaultInstance.createListInfoFrom;
169
+
170
+ createInitialListInfo = (additionalValues = null) => {
171
+ return this.createListInfoFrom({
172
+ ...additionalValues,
173
+ });
174
+ };
175
+
176
+ createListInfoFrom = ({
177
+ sorting,
178
+ filters,
179
+ scope,
180
+ page,
181
+ nextPageToLoad,
182
+ index,
183
+ list,
184
+ totalCount,
185
+ ...additionalValues
186
+ } = {}) => {
187
+ return {
188
+ [this.groupPropertyName]: {
189
+ sorting: sorting ?? null,
190
+ filters: filters ?? null,
191
+ scope: scope ?? null,
192
+ page: page ?? null,
193
+ nextPageToLoad: nextPageToLoad ?? 1,
194
+ index: index ?? {},
195
+ list: list ?? [],
196
+ totalCount: totalCount ?? 0,
197
+ ...additionalValues,
198
+ },
199
+ };
200
+ };
201
+ }
202
+
203
+ export default ListHelper;