@dhis2-ui/organisation-unit-tree 7.16.1 → 8.0.0

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 (37) hide show
  1. package/build/cjs/__e2e__/common.js +51 -24
  2. package/build/cjs/__e2e__/tree_api.stories.e2e.js +13 -5
  3. package/build/cjs/__stories__/development-stories.js +1 -3
  4. package/build/cjs/__stories__/shared.js +50 -17
  5. package/build/cjs/features/controlled_expanded/index.js +9 -3
  6. package/build/cjs/features/tree_api/index.js +4 -3
  7. package/build/cjs/helpers/sort-node-children-alphabetically.js +2 -17
  8. package/build/cjs/locales/en/translations.json +3 -2
  9. package/build/cjs/organisation-unit-node/label/label.js +2 -5
  10. package/build/cjs/organisation-unit-node/loading-spinner.js +32 -0
  11. package/build/cjs/organisation-unit-node/organisation-unit-node-children.js +120 -0
  12. package/build/cjs/organisation-unit-node/organisation-unit-node.js +43 -68
  13. package/build/cjs/organisation-unit-node/use-org-children.js +81 -0
  14. package/build/cjs/organisation-unit-node/use-org-children.test.js +319 -0
  15. package/build/cjs/organisation-unit-node/use-org-data/use-org-data.js +10 -33
  16. package/build/cjs/organisation-unit-node/use-org-data/use-org-data.test.js +3 -169
  17. package/build/cjs/organisation-unit-tree/organisation-unit-tree.js +4 -2
  18. package/build/cjs/organisation-unit-tree/use-root-org-data/use-root-org-data.js +7 -5
  19. package/build/es/__e2e__/common.js +52 -24
  20. package/build/es/__e2e__/tree_api.stories.e2e.js +13 -5
  21. package/build/es/__stories__/development-stories.js +1 -3
  22. package/build/es/__stories__/shared.js +51 -17
  23. package/build/es/features/controlled_expanded/index.js +9 -3
  24. package/build/es/features/tree_api/index.js +4 -3
  25. package/build/es/helpers/sort-node-children-alphabetically.js +2 -17
  26. package/build/es/locales/en/translations.json +3 -2
  27. package/build/es/organisation-unit-node/label/label.js +2 -5
  28. package/build/es/organisation-unit-node/loading-spinner.js +17 -0
  29. package/build/es/organisation-unit-node/organisation-unit-node-children.js +103 -0
  30. package/build/es/organisation-unit-node/organisation-unit-node.js +41 -65
  31. package/build/es/organisation-unit-node/use-org-children.js +69 -0
  32. package/build/es/organisation-unit-node/use-org-children.test.js +311 -0
  33. package/build/es/organisation-unit-node/use-org-data/use-org-data.js +10 -31
  34. package/build/es/organisation-unit-node/use-org-data/use-org-data.test.js +3 -169
  35. package/build/es/organisation-unit-tree/organisation-unit-tree.js +4 -2
  36. package/build/es/organisation-unit-tree/use-root-org-data/use-root-org-data.js +7 -5
  37. package/package.json +5 -5
@@ -5,10 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.OrganisationUnitNode = void 0;
7
7
 
8
- var _style = _interopRequireDefault(require("styled-jsx/style"));
9
-
10
- var _loader = require("@dhis2-ui/loader");
11
-
12
8
  var _node = require("@dhis2-ui/node");
13
9
 
14
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
@@ -21,36 +17,22 @@ var _index2 = _interopRequireDefault(require("../locales/index.js"));
21
17
 
22
18
  var _propTypes2 = require("../prop-types.js");
23
19
 
24
- var _computeChildNodes = require("./compute-child-nodes.js");
25
-
26
20
  var _errorMessage = require("./error-message.js");
27
21
 
28
22
  var _hasDescendantSelectedPaths = require("./has-descendant-selected-paths.js");
29
23
 
30
24
  var _index3 = require("./label/index.js");
31
25
 
26
+ var _loadingSpinner = require("./loading-spinner.js");
27
+
28
+ var _organisationUnitNodeChildren = require("./organisation-unit-node-children.js");
29
+
32
30
  var _useOpenState = require("./use-open-state.js");
33
31
 
34
32
  var _index4 = require("./use-org-data/index.js");
35
33
 
36
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
35
 
38
- const loadingSpinnerStyles = {
39
- styles: /*#__PURE__*/_react.default.createElement(_style.default, {
40
- id: "358163430"
41
- }, [".extrasmall.jsx-358163430{display:block;margin:3px 0;}"]),
42
- className: "jsx-358163430"
43
- };
44
-
45
- const LoadingSpinner = () => /*#__PURE__*/_react.default.createElement("div", {
46
- className: "jsx-2503342345"
47
- }, /*#__PURE__*/_react.default.createElement(_loader.CircularLoader, {
48
- extrasmall: true,
49
- className: loadingSpinnerStyles.className
50
- }), /*#__PURE__*/_react.default.createElement("style", null, loadingSpinnerStyles.styles), /*#__PURE__*/_react.default.createElement(_style.default, {
51
- id: "2503342345"
52
- }, ["div.jsx-2503342345{width:24px;}"]));
53
-
54
36
  const OrganisationUnitNode = ({
55
37
  autoExpandLoadingError,
56
38
  dataTest,
@@ -72,22 +54,20 @@ const OrganisationUnitNode = ({
72
54
  onCollapse,
73
55
  onExpand
74
56
  }) => {
75
- const {
76
- loading,
77
- error,
78
- data
79
- } = (0, _index4.useOrgData)(id, {
57
+ const orgData = (0, _index4.useOrgData)(id, {
80
58
  isUserDataViewFallback,
81
- suppressAlphabeticalSorting,
82
- displayName,
83
- onComplete: onChildrenLoaded
59
+ displayName
84
60
  });
85
61
  const strippedPath = (0, _index.leftTrimToRootId)(path, rootId);
86
- const node = { ...data,
62
+ const node = {
63
+ // guarantee that displayName and id are avaiable before data loaded
64
+ displayName,
65
+ id,
66
+ ...(orgData.data || {}),
67
+ // do not override strippedPath with path from loaded data
87
68
  path: strippedPath
88
69
  };
89
- const childNodes = !loading && !error ? (0, _computeChildNodes.computeChildNodes)(node, filter) : [];
90
- const hasChildren = !!childNodes.length;
70
+ const hasChildren = !!node.children && node.children > 0;
91
71
  const hasSelectedDescendants = (0, _hasDescendantSelectedPaths.hasDescendantSelectedPaths)(strippedPath, selected, rootId);
92
72
  const isHighlighted = highlighted.includes(path);
93
73
  const {
@@ -95,7 +75,7 @@ const OrganisationUnitNode = ({
95
75
  onToggleOpen
96
76
  } = (0, _useOpenState.useOpenState)({
97
77
  autoExpandLoadingError,
98
- errorMessage: error && error.toString(),
78
+ errorMessage: orgData.error && orgData.error.toString(),
99
79
  path: strippedPath,
100
80
  expanded,
101
81
  onExpand,
@@ -106,14 +86,14 @@ const OrganisationUnitNode = ({
106
86
  disableSelection,
107
87
  hasChildren,
108
88
  hasSelectedDescendants,
109
- loading,
110
- error,
89
+ loading: orgData.loading,
90
+ error: orgData.error,
111
91
  selected,
112
92
  open,
113
93
  path,
114
94
  singleSelection,
115
95
  node,
116
- label: node.displayName,
96
+ label: displayName,
117
97
  checked: isSelected,
118
98
  highlighted: isHighlighted
119
99
  });
@@ -122,7 +102,7 @@ const OrganisationUnitNode = ({
122
102
  node: node,
123
103
  fullPath: path,
124
104
  open: open,
125
- loading: loading,
105
+ loading: orgData.loading,
126
106
  checked: isSelected,
127
107
  rootId: rootId,
128
108
  onChange: onChange,
@@ -147,45 +127,40 @@ const OrganisationUnitNode = ({
147
127
  */
148
128
 
149
129
 
150
- const showPlaceholder = hasChildren && !open && !error;
151
- const showChildNodes = hasChildren && open && !error;
130
+ const showPlaceholder = hasChildren && !open && !orgData.error;
131
+ const showChildNodes = hasChildren && open && !orgData.error;
152
132
  return /*#__PURE__*/_react.default.createElement(_node.Node, {
153
133
  dataTest: `${dataTest}-node`,
154
134
  open: open,
155
135
  onOpen: onToggleOpen,
156
136
  onClose: onToggleOpen,
157
137
  component: label,
158
- icon: loading && /*#__PURE__*/_react.default.createElement(LoadingSpinner, null)
159
- }, error && /*#__PURE__*/_react.default.createElement(_errorMessage.ErrorMessage, {
138
+ icon: orgData.loading && /*#__PURE__*/_react.default.createElement(_loadingSpinner.LoadingSpinner, null)
139
+ }, orgData.error && /*#__PURE__*/_react.default.createElement(_errorMessage.ErrorMessage, {
160
140
  dataTest: dataTest
161
141
  }, _index2.default.t('Could not load children')), showPlaceholder && /*#__PURE__*/_react.default.createElement("span", {
162
142
  "data-test": `${dataTest}-placeholder`
163
- }), showChildNodes && childNodes.map(child => {
164
- const childPath = `${path}/${child.id}`;
165
- const grandChildNodes = (0, _computeChildNodes.computeChildNodes)(child, filter);
166
- return /*#__PURE__*/_react.default.createElement(OrganisationUnitNode, {
167
- autoExpandLoadingError: autoExpandLoadingError,
168
- childNodes: grandChildNodes,
169
- dataTest: dataTest,
170
- disableSelection: disableSelection,
171
- displayName: child.displayName,
172
- expanded: expanded,
173
- filter: filter,
174
- highlighted: highlighted,
175
- id: child.id,
176
- isUserDataViewFallback: isUserDataViewFallback,
177
- key: childPath,
178
- onChange: onChange,
179
- onChildrenLoaded: onChildrenLoaded,
180
- onCollapse: onCollapse,
181
- onExpand: onExpand,
182
- path: childPath,
183
- renderNodeLabel: renderNodeLabel,
184
- rootId: rootId,
185
- selected: selected,
186
- singleSelection: singleSelection,
187
- suppressAlphabeticalSorting: suppressAlphabeticalSorting
188
- });
143
+ }), showChildNodes && /*#__PURE__*/_react.default.createElement(_organisationUnitNodeChildren.OrganisationUnitNodeChildren // Prevent cirular imports
144
+ , {
145
+ OrganisationUnitNode: OrganisationUnitNode,
146
+ node: node,
147
+ autoExpandLoadingError: autoExpandLoadingError,
148
+ dataTest: dataTest,
149
+ disableSelection: disableSelection,
150
+ expanded: expanded,
151
+ filter: filter,
152
+ highlighted: highlighted,
153
+ isUserDataViewFallback: isUserDataViewFallback,
154
+ onChange: onChange,
155
+ onChildrenLoaded: onChildrenLoaded,
156
+ onCollapse: onCollapse,
157
+ onExpand: onExpand,
158
+ parentPath: path,
159
+ renderNodeLabel: renderNodeLabel,
160
+ rootId: rootId,
161
+ selected: selected,
162
+ singleSelection: singleSelection,
163
+ suppressAlphabeticalSorting: suppressAlphabeticalSorting
189
164
  }));
190
165
  };
191
166
 
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useOrgChildren = void 0;
7
+
8
+ var _appRuntime = require("@dhis2/app-runtime");
9
+
10
+ var _react = require("react");
11
+
12
+ var _index = require("../helpers/index.js");
13
+
14
+ const ORG_DATA_QUERY = {
15
+ orgUnit: {
16
+ resource: `organisationUnits`,
17
+ id: ({
18
+ id
19
+ }) => id,
20
+ params: {
21
+ fields: 'children[id,path,displayName]'
22
+ }
23
+ }
24
+ };
25
+ /**
26
+ * @param {string[]} ids
27
+ * @param {Object} options
28
+ * @param {string} options.displayName
29
+ * @param {boolean} [options.withChildren]
30
+ * @returns {Object}
31
+ */
32
+
33
+ const useOrgChildren = ({
34
+ node,
35
+ suppressAlphabeticalSorting,
36
+ onComplete
37
+ }) => {
38
+ const onCompleteCalledRef = (0, _react.useRef)(false);
39
+ const {
40
+ called,
41
+ loading,
42
+ error,
43
+ data
44
+ } = (0, _appRuntime.useDataQuery)(ORG_DATA_QUERY, {
45
+ variables: {
46
+ id: node.id
47
+ }
48
+ });
49
+ const orgChildren = (0, _react.useMemo)(() => {
50
+ if (!data) {
51
+ return undefined;
52
+ } // undefined or zero
53
+
54
+
55
+ if (!node.children) {
56
+ return [];
57
+ }
58
+
59
+ const {
60
+ orgUnit
61
+ } = data;
62
+ return suppressAlphabeticalSorting ? orgUnit.children : (0, _index.sortNodeChildrenAlphabetically)(orgUnit.children);
63
+ }, [data, suppressAlphabeticalSorting]);
64
+ (0, _react.useEffect)(() => {
65
+ if (onComplete && orgChildren && !onCompleteCalledRef.current) {
66
+ // For backwards compatibility: Pass entire node incl. children
67
+ onComplete({ ...node,
68
+ children: orgChildren
69
+ });
70
+ onCompleteCalledRef.current = true;
71
+ }
72
+ }, [onComplete, orgChildren, onCompleteCalledRef]);
73
+ return {
74
+ called,
75
+ loading,
76
+ error: error || null,
77
+ data: orgChildren
78
+ };
79
+ };
80
+
81
+ exports.useOrgChildren = useOrgChildren;
@@ -0,0 +1,319 @@
1
+ "use strict";
2
+
3
+ var _appRuntime = require("@dhis2/app-runtime");
4
+
5
+ var _reactHooks = require("@testing-library/react-hooks");
6
+
7
+ var _react = _interopRequireDefault(require("react"));
8
+
9
+ var _useOrgChildren = require("./use-org-children.js");
10
+
11
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+
13
+ describe('OrganisationUnitTree - useOrgChildren', () => {
14
+ const dataProviderData = {
15
+ organisationUnits: jest.fn((type, {
16
+ id
17
+ }) => {
18
+ if (id === 'A0000000000') {
19
+ return {
20
+ children: [{
21
+ id: 'A0000000001',
22
+ path: '/A0000000000/A0000000001',
23
+ children: [],
24
+ displayName: 'Org Unit 2'
25
+ }]
26
+ };
27
+ }
28
+
29
+ return Promise.reject(`No org unit with id "${id}"`);
30
+ })
31
+ };
32
+
33
+ const wrapper = ({
34
+ children
35
+ }) => /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
36
+ data: dataProviderData
37
+ }, children);
38
+
39
+ const node = {
40
+ id: 'A0000000000',
41
+ path: '/A0000000000',
42
+ displayName: 'Org Unit 1',
43
+ children: 1
44
+ };
45
+ it('should respond with `loading: true`, `error: null` and `data: null` initially', async () => {
46
+ const {
47
+ result,
48
+ waitForNextUpdate
49
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
50
+ node
51
+ }), {
52
+ wrapper
53
+ });
54
+ expect(result.current).toEqual({
55
+ called: true,
56
+ loading: true,
57
+ error: null,
58
+ data: undefined
59
+ }); // Prevent the following error log with
60
+ // "Warning: An update to TestComponent inside a test was not wrapped
61
+ // in act(...)."
62
+
63
+ await waitForNextUpdate();
64
+ });
65
+ it('should provide the org unit data', async () => {
66
+ const {
67
+ result,
68
+ waitForNextUpdate
69
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
70
+ node
71
+ }), {
72
+ wrapper
73
+ });
74
+ await waitForNextUpdate();
75
+ expect(result.current).toEqual({
76
+ called: true,
77
+ loading: false,
78
+ error: null,
79
+ data: [{
80
+ id: 'A0000000001',
81
+ path: '/A0000000000/A0000000001',
82
+ children: [],
83
+ displayName: 'Org Unit 2'
84
+ }]
85
+ });
86
+ });
87
+ it('should provide the error', async () => {
88
+ const errorWrapper = ({
89
+ children
90
+ }) => /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
91
+ data: {
92
+ organisationUnits: async () => {
93
+ throw new Error('Error message');
94
+ }
95
+ }
96
+ }, children);
97
+
98
+ const {
99
+ result,
100
+ waitForNextUpdate
101
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
102
+ node
103
+ }), {
104
+ wrapper: errorWrapper
105
+ });
106
+ await waitForNextUpdate();
107
+ expect(result.current).toEqual({
108
+ called: true,
109
+ loading: false,
110
+ error: new Error('Error message'),
111
+ data: undefined
112
+ });
113
+ });
114
+ it('should call the onComplete callback', async () => {
115
+ const onComplete = jest.fn();
116
+ const options = {
117
+ onComplete,
118
+ node
119
+ };
120
+ const {
121
+ waitForNextUpdate
122
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)(options), {
123
+ wrapper
124
+ });
125
+ await waitForNextUpdate();
126
+ expect(onComplete).toHaveBeenCalledWith({
127
+ id: 'A0000000000',
128
+ path: '/A0000000000',
129
+ displayName: 'Org Unit 1',
130
+ children: [{
131
+ id: 'A0000000001',
132
+ path: '/A0000000000/A0000000001',
133
+ children: [],
134
+ displayName: 'Org Unit 2'
135
+ }]
136
+ });
137
+ });
138
+ it("should sort the node's children alphabetically by default", async () => {
139
+ const dataProviderDataWithUnsortedChildren = {
140
+ organisationUnits: jest.fn((type, {
141
+ id
142
+ }) => {
143
+ if (id === 'A0000000000') {
144
+ return {
145
+ children: [{
146
+ id: 'A0000000002',
147
+ path: '/A0000000000/A0000000002',
148
+ children: [],
149
+ displayName: 'Org Unit 3'
150
+ }, {
151
+ id: 'A0000000001',
152
+ path: '/A0000000000/A0000000001',
153
+ children: [],
154
+ displayName: 'Org Unit 2'
155
+ }]
156
+ };
157
+ }
158
+
159
+ return Promise.reject(`No org unit with id "${id}"`);
160
+ })
161
+ };
162
+
163
+ const wrapperWithUnsortedChildren = ({
164
+ children
165
+ }) => /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
166
+ data: dataProviderDataWithUnsortedChildren
167
+ }, children);
168
+
169
+ const {
170
+ result,
171
+ waitForNextUpdate
172
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
173
+ node: { ...node,
174
+ children: 2
175
+ }
176
+ }), {
177
+ wrapper: wrapperWithUnsortedChildren
178
+ });
179
+ await waitForNextUpdate();
180
+ expect(result.current).toEqual({
181
+ called: true,
182
+ loading: false,
183
+ error: null,
184
+ data: [{
185
+ id: 'A0000000001',
186
+ path: '/A0000000000/A0000000001',
187
+ children: [],
188
+ displayName: 'Org Unit 2'
189
+ }, {
190
+ id: 'A0000000002',
191
+ path: '/A0000000000/A0000000002',
192
+ children: [],
193
+ displayName: 'Org Unit 3'
194
+ }]
195
+ });
196
+ });
197
+ it(`should not sort the node's children alphabetically when "suppressAlphabeticalSorting" is true`, async () => {
198
+ const dataProviderDataWithUnsortedChildren = {
199
+ organisationUnits: jest.fn((type, {
200
+ id
201
+ }) => {
202
+ if (id === 'A0000000000') {
203
+ return {
204
+ children: [{
205
+ id: 'A0000000002',
206
+ path: '/A0000000000/A0000000002',
207
+ children: [],
208
+ displayName: 'Org Unit 3'
209
+ }, {
210
+ id: 'A0000000001',
211
+ path: '/A0000000000/A0000000001',
212
+ children: [],
213
+ displayName: 'Org Unit 2'
214
+ }]
215
+ };
216
+ }
217
+
218
+ return Promise.reject(`No org unit with id "${id}"`);
219
+ })
220
+ };
221
+
222
+ const wrapperWithUnsortedChildren = ({
223
+ children
224
+ }) => /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
225
+ data: dataProviderDataWithUnsortedChildren
226
+ }, children);
227
+
228
+ const options = {
229
+ node: { ...node,
230
+ children: 2
231
+ },
232
+ suppressAlphabeticalSorting: true
233
+ };
234
+ const {
235
+ result,
236
+ waitForNextUpdate
237
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)(options), {
238
+ wrapper: wrapperWithUnsortedChildren
239
+ });
240
+ await waitForNextUpdate();
241
+ expect(result.current).toEqual({
242
+ called: true,
243
+ loading: false,
244
+ error: null,
245
+ data: [{
246
+ id: 'A0000000002',
247
+ path: '/A0000000000/A0000000002',
248
+ children: [],
249
+ displayName: 'Org Unit 3'
250
+ }, {
251
+ id: 'A0000000001',
252
+ path: '/A0000000000/A0000000001',
253
+ children: [],
254
+ displayName: 'Org Unit 2'
255
+ }]
256
+ });
257
+ });
258
+ it(`should not sort the node's children alphabetically when "suppressAlphabeticalSorting" is true`, async () => {
259
+ const dataProviderDataWithUnsortedChildren = {
260
+ organisationUnits: jest.fn((type, {
261
+ id
262
+ }) => {
263
+ if (id === 'A0000000000') {
264
+ return {
265
+ children: [{
266
+ id: 'A0000000002',
267
+ path: '/A0000000000/A0000000002',
268
+ children: [],
269
+ displayName: 'Org Unit 3'
270
+ }, {
271
+ id: 'A0000000001',
272
+ path: '/A0000000000/A0000000001',
273
+ children: [],
274
+ displayName: 'Org Unit 2'
275
+ }]
276
+ };
277
+ }
278
+
279
+ return Promise.reject(`No org unit with id "${id}"`);
280
+ })
281
+ };
282
+
283
+ const wrapperWithUnsortedChildren = ({
284
+ children
285
+ }) => /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
286
+ data: dataProviderDataWithUnsortedChildren
287
+ }, children);
288
+
289
+ const options = {
290
+ node: { ...node,
291
+ children: 2
292
+ },
293
+ suppressAlphabeticalSorting: true
294
+ };
295
+ const {
296
+ result,
297
+ waitForNextUpdate
298
+ } = (0, _reactHooks.renderHook)(() => (0, _useOrgChildren.useOrgChildren)(options), {
299
+ wrapper: wrapperWithUnsortedChildren
300
+ });
301
+ await waitForNextUpdate();
302
+ expect(result.current).toEqual({
303
+ called: true,
304
+ loading: false,
305
+ error: null,
306
+ data: [{
307
+ id: 'A0000000002',
308
+ path: '/A0000000000/A0000000002',
309
+ children: [],
310
+ displayName: 'Org Unit 3'
311
+ }, {
312
+ id: 'A0000000001',
313
+ path: '/A0000000000/A0000000001',
314
+ children: [],
315
+ displayName: 'Org Unit 2'
316
+ }]
317
+ });
318
+ });
319
+ });
@@ -7,10 +7,6 @@ exports.useOrgData = void 0;
7
7
 
8
8
  var _appRuntime = require("@dhis2/app-runtime");
9
9
 
10
- var _react = require("react");
11
-
12
- var _index = require("../../helpers/index.js");
13
-
14
10
  const ORG_DATA_QUERY = {
15
11
  orgUnit: {
16
12
  resource: `organisationUnits`,
@@ -21,7 +17,7 @@ const ORG_DATA_QUERY = {
21
17
  isUserDataViewFallback
22
18
  }) => ({
23
19
  isUserDataViewFallback,
24
- fields: ['id', 'path', 'children[id,path,displayName]']
20
+ fields: ['path', 'children::size']
25
21
  })
26
22
  }
27
23
  };
@@ -29,24 +25,18 @@ const ORG_DATA_QUERY = {
29
25
  * @param {string[]} ids
30
26
  * @param {Object} options
31
27
  * @param {string} options.displayName
32
- * @param {boolean} [options.withChildren]
28
+ * @param {bool} options.isUserDataViewFallback
33
29
  * @returns {Object}
34
30
  */
35
31
 
36
32
  const useOrgData = (id, {
37
- isUserDataViewFallback,
38
- suppressAlphabeticalSorting,
39
- onComplete,
40
- displayName
33
+ displayName,
34
+ isUserDataViewFallback
41
35
  }) => {
42
36
  if (!displayName) {
43
37
  throw new Error('"displayName" is required');
44
38
  }
45
39
 
46
- const defaultData = {
47
- id,
48
- displayName
49
- };
50
40
  const variables = {
51
41
  id,
52
42
  isUserDataViewFallback
@@ -54,31 +44,18 @@ const useOrgData = (id, {
54
44
  const {
55
45
  loading,
56
46
  error,
57
- data
47
+ data = {}
58
48
  } = (0, _appRuntime.useDataQuery)(ORG_DATA_QUERY, {
59
49
  variables
60
50
  });
61
- const transformedData = (0, _react.useMemo)(() => {
62
- if (!data) {
63
- return defaultData;
64
- }
65
-
66
- const {
67
- orgUnit: node
68
- } = data;
69
- const transformed = suppressAlphabeticalSorting ? node : (0, _index.sortNodeChildrenAlphabetically)(node);
70
- const merged = { ...defaultData,
71
- ...transformed
72
- };
73
- return merged;
74
- }, [data, suppressAlphabeticalSorting, id, displayName]);
75
- (0, _react.useEffect)(() => {
76
- onComplete && onComplete(transformedData);
77
- }, [onComplete, transformedData]);
78
51
  return {
79
52
  loading,
80
53
  error: error || null,
81
- data: transformedData
54
+ data: {
55
+ id,
56
+ displayName,
57
+ ...data.orgUnit
58
+ }
82
59
  };
83
60
  };
84
61