@availity/mui-spaces 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,456 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __objRest = (source, exclude) => {
24
+ var target = {};
25
+ for (var prop in source)
26
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
27
+ target[prop] = source[prop];
28
+ if (source != null && __getOwnPropSymbols)
29
+ for (var prop of __getOwnPropSymbols(source)) {
30
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
31
+ target[prop] = source[prop];
32
+ }
33
+ return target;
34
+ };
35
+ var __export = (target, all) => {
36
+ for (var name in all)
37
+ __defProp(target, name, { get: all[name], enumerable: true });
38
+ };
39
+ var __copyProps = (to, from, except, desc) => {
40
+ if (from && typeof from === "object" || typeof from === "function") {
41
+ for (let key of __getOwnPropNames(from))
42
+ if (!__hasOwnProp.call(to, key) && key !== except)
43
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
44
+ }
45
+ return to;
46
+ };
47
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
48
+ var __async = (__this, __arguments, generator) => {
49
+ return new Promise((resolve, reject) => {
50
+ var fulfilled = (value) => {
51
+ try {
52
+ step(generator.next(value));
53
+ } catch (e) {
54
+ reject(e);
55
+ }
56
+ };
57
+ var rejected = (value) => {
58
+ try {
59
+ step(generator.throw(value));
60
+ } catch (e) {
61
+ reject(e);
62
+ }
63
+ };
64
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
65
+ step((generator = generator.apply(__this, __arguments)).next());
66
+ });
67
+ };
68
+
69
+ // src/index.ts
70
+ var src_exports = {};
71
+ __export(src_exports, {
72
+ INITIAL_STATE: () => INITIAL_STATE,
73
+ Spaces: () => Spaces,
74
+ SpacesContext: () => SpacesContext,
75
+ useSpaces: () => useSpaces,
76
+ useSpacesContext: () => useSpacesContext
77
+ });
78
+ module.exports = __toCommonJS(src_exports);
79
+
80
+ // src/lib/Spaces.tsx
81
+ var import_react = require("react");
82
+ var import_react_query = require("@tanstack/react-query");
83
+
84
+ // src/lib/spaces-data.tsx
85
+ var import_api_axios = require("@availity/api-axios");
86
+ var actions = {
87
+ SPACES: (currentState, { spaces, spacesByConfig, spacesByPayer }) => ({
88
+ previousSpacesMap: spaces,
89
+ previousSpacesByConfigMap: spacesByConfig,
90
+ previousSpacesByPayerMap: spacesByPayer,
91
+ error: void 0,
92
+ loading: false
93
+ }),
94
+ ERROR: (state, { error }) => __spreadProps(__spreadValues({}, state), {
95
+ loading: false,
96
+ error
97
+ }),
98
+ LOADING: (state, { loading }) => __spreadProps(__spreadValues({}, state), {
99
+ loading: loading !== void 0 ? loading : !state.loading
100
+ })
101
+ };
102
+ var spacesReducer = (state, action) => {
103
+ const { type } = action;
104
+ return actions[type](state, action);
105
+ };
106
+ var normalizeSpaces = (spaces) => {
107
+ let spacesToReduce = spaces;
108
+ if (Array.isArray(spaces[0])) {
109
+ spacesToReduce = spaces[0];
110
+ }
111
+ const pairFields = ["images", "metadata", "colors", "icons", "mapping"];
112
+ return spacesToReduce.reduce((accum, spc) => {
113
+ var _b;
114
+ if (!spc)
115
+ return accum;
116
+ const _a = spc, { images, metadata, colors, icons, mapping } = _a, rest = __objRest(_a, ["images", "metadata", "colors", "icons", "mapping"]);
117
+ const normalizedSpace = __spreadValues({}, rest);
118
+ for (const field of pairFields) {
119
+ if (spc[field] && Array.isArray(spc[field])) {
120
+ normalizedSpace[field] = (_b = spc[field]) == null ? void 0 : _b.reduce((_accum, { name, value }) => {
121
+ _accum[name] = value;
122
+ return _accum;
123
+ }, {});
124
+ }
125
+ }
126
+ accum.push(normalizedSpace);
127
+ return accum;
128
+ }, []);
129
+ };
130
+ var fetchSpaces = (_0) => __async(void 0, [_0], function* ({ query, clientId, variables }) {
131
+ const {
132
+ data: {
133
+ data: { configurationPagination }
134
+ }
135
+ } = yield import_api_axios.avWebQLApi.create(
136
+ {
137
+ query,
138
+ variables: __spreadValues({}, variables)
139
+ },
140
+ { headers: { "X-Client-ID": clientId } }
141
+ );
142
+ const {
143
+ pageInfo: { currentPage, hasNextPage },
144
+ items
145
+ } = configurationPagination;
146
+ return {
147
+ items,
148
+ currentPage,
149
+ hasNextPage
150
+ };
151
+ });
152
+ var fetchAllSpaces = (_0) => __async(void 0, [_0], function* ({
153
+ query,
154
+ clientId,
155
+ variables,
156
+ _spaces = []
157
+ }) {
158
+ const { items, currentPage, hasNextPage } = yield fetchSpaces({ query, clientId, variables });
159
+ _spaces.push(...items);
160
+ if (hasNextPage) {
161
+ const vars = __spreadProps(__spreadValues({}, variables), {
162
+ page: currentPage + 1
163
+ });
164
+ return fetchAllSpaces({ query, clientId, variables: vars, _spaces });
165
+ }
166
+ return _spaces;
167
+ });
168
+
169
+ // src/lib/configurationFindMany.tsx
170
+ var configurationFindMany_default = `query configurationFindMany($ids: [String!], $payerIDs: [ID!], $types: [TypeEnum!]) {
171
+ configurationPagination(filter: { ids: $ids, payerIds: $payerIDs, types: $types }) {
172
+ pageInfo {
173
+ hasNextPage
174
+ currentPage
175
+ }
176
+ items {
177
+ ... on Configuration {
178
+ configurationId
179
+ name
180
+ shortName
181
+ type
182
+ activeDate
183
+ isNew
184
+ description
185
+ payerIDs
186
+ parentIDs
187
+ metadataPairs {
188
+ name
189
+ value
190
+ }
191
+ }
192
+
193
+ ... on Node {
194
+ id
195
+ }
196
+
197
+ ... on Alert {
198
+ link {
199
+ text
200
+ target
201
+ url
202
+ }
203
+ }
204
+
205
+ ... on Container {
206
+ link {
207
+ text
208
+ target
209
+ url
210
+ }
211
+ images {
212
+ tile
213
+ promotional
214
+ logo
215
+ billboard
216
+ }
217
+ }
218
+
219
+ ... on PayerSpace {
220
+ link {
221
+ text
222
+ target
223
+ url
224
+ }
225
+ images {
226
+ tile
227
+ logo
228
+ billboard
229
+ }
230
+ url
231
+ }
232
+
233
+ ... on Application {
234
+ link {
235
+ text
236
+ target
237
+ url
238
+ }
239
+ }
240
+
241
+ ... on Resource {
242
+ link {
243
+ text
244
+ target
245
+ url
246
+ }
247
+ }
248
+
249
+ ... on Navigation {
250
+ icons {
251
+ dashboard
252
+ navigation
253
+ }
254
+ images {
255
+ promotional
256
+ }
257
+ }
258
+
259
+ ... on Learning {
260
+ images {
261
+ promotional
262
+ }
263
+ }
264
+
265
+ ... on Proxy {
266
+ url
267
+ }
268
+
269
+ ... on File {
270
+ url
271
+ }
272
+ }
273
+ }
274
+ }`;
275
+
276
+ // src/lib/Spaces.tsx
277
+ var import_jsx_runtime = require("react/jsx-runtime");
278
+ var INITIAL_STATE = {
279
+ loading: true
280
+ };
281
+ var SpacesContext = (0, import_react.createContext)(INITIAL_STATE);
282
+ var useSpacesContext = () => (0, import_react.useContext)(SpacesContext);
283
+ var Spaces = ({
284
+ query = configurationFindMany_default,
285
+ variables = { types: ["PAYERSPACE"] },
286
+ clientId,
287
+ children,
288
+ payerIds,
289
+ spaceIds,
290
+ spaces: spacesFromProps
291
+ }) => {
292
+ const [{ previousSpacesMap, previousSpacesByConfigMap, previousSpacesByPayerMap, loading, error }, dispatch] = (0, import_react.useReducer)(spacesReducer, INITIAL_STATE);
293
+ const spacesMap = new Map(previousSpacesMap);
294
+ const configIdsMap = new Map(previousSpacesByConfigMap);
295
+ const payerIdsMap = new Map(previousSpacesByPayerMap);
296
+ const spaceIdsToQuery = /* @__PURE__ */ new Set();
297
+ const payerIdsToQuery = /* @__PURE__ */ new Set();
298
+ if (spacesFromProps && spacesFromProps.length > 0) {
299
+ for (const space of spacesFromProps) {
300
+ if (space.id && !spacesMap.has(space.id)) {
301
+ spacesMap.set(space.id, space);
302
+ }
303
+ if (space.configurationId && !configIdsMap.has(space.configurationId)) {
304
+ configIdsMap.set(space.configurationId, space);
305
+ }
306
+ if (space.payerIDs) {
307
+ for (const pId of space.payerIDs) {
308
+ const currentSpacesForPayerId = payerIdsMap.get(pId);
309
+ if (currentSpacesForPayerId) {
310
+ payerIdsMap.set(pId, [...currentSpacesForPayerId, space]);
311
+ } else {
312
+ payerIdsMap.set(pId, [space]);
313
+ }
314
+ }
315
+ }
316
+ }
317
+ }
318
+ if (spaceIds && spaceIds.length > 0) {
319
+ for (const id of spaceIds) {
320
+ if (!(spacesMap.has(id) || configIdsMap.has(id))) {
321
+ spaceIdsToQuery.add(id);
322
+ }
323
+ }
324
+ }
325
+ if (payerIds && payerIds.length > 0) {
326
+ for (const pid of payerIds) {
327
+ if (!payerIdsMap.has(pid)) {
328
+ payerIdsToQuery.add(pid);
329
+ }
330
+ }
331
+ }
332
+ const spaceIdVars = __spreadProps(__spreadValues({}, variables), { ids: [...spaceIdsToQuery.keys()] });
333
+ const payerIdVars = __spreadProps(__spreadValues({}, variables), { payerIds: [...payerIdsToQuery.keys()] });
334
+ const [
335
+ { data: spacesBySpaceIds, isFetching: isLoadingBySpaceIds, error: errorBySpaceIds },
336
+ { data: spacesByPayerIds, isFetching: isLoadingByPayerIds, error: errorByPayerIds }
337
+ ] = (0, import_react_query.useQueries)({
338
+ queries: [
339
+ {
340
+ queryKey: ["id", spaceIdVars],
341
+ queryFn: () => fetchAllSpaces({ query, clientId, variables: spaceIdVars }),
342
+ enabled: spaceIdsToQuery.size > 0
343
+ },
344
+ {
345
+ queryKey: ["id", payerIdVars],
346
+ queryFn: () => fetchAllSpaces({ query, clientId, variables: payerIdVars }),
347
+ enabled: payerIdsToQuery.size > 0
348
+ }
349
+ ]
350
+ });
351
+ (0, import_react.useEffect)(() => {
352
+ if (errorByPayerIds || errorBySpaceIds) {
353
+ dispatch({
354
+ type: "ERROR",
355
+ error: (errorByPayerIds == null ? void 0 : errorByPayerIds.message) || (errorBySpaceIds == null ? void 0 : errorBySpaceIds.message),
356
+ loading: false
357
+ });
358
+ }
359
+ }, [errorByPayerIds, errorBySpaceIds]);
360
+ (0, import_react.useEffect)(() => {
361
+ dispatch({
362
+ type: "LOADING",
363
+ loading: true
364
+ });
365
+ if (spaceIdsToQuery.size === 0 && payerIdsToQuery.size === 0) {
366
+ dispatch({
367
+ type: "LOADING",
368
+ loading: false
369
+ });
370
+ return;
371
+ }
372
+ if (spacesBySpaceIds) {
373
+ for (const space of spacesBySpaceIds) {
374
+ if (!spacesMap.has(space.id)) {
375
+ spacesMap.set(space.id, space);
376
+ }
377
+ if (!configIdsMap.has(space.configurationId)) {
378
+ configIdsMap.set(space.configurationId, space);
379
+ }
380
+ if (space.payerIDs) {
381
+ for (const pId of space.payerIDs) {
382
+ const currentSpacesForPayerId = payerIdsMap.get(pId);
383
+ if (currentSpacesForPayerId) {
384
+ payerIdsMap.set(pId, [...currentSpacesForPayerId, space]);
385
+ } else {
386
+ payerIdsMap.set(pId, [space]);
387
+ }
388
+ }
389
+ }
390
+ }
391
+ }
392
+ if (payerIdsToQuery.size > 0) {
393
+ if (spacesByPayerIds) {
394
+ for (const space of spacesByPayerIds) {
395
+ if (!spacesMap.has(space.id)) {
396
+ spacesMap.set(space.id, space);
397
+ }
398
+ if (!configIdsMap.has(space.configurationId)) {
399
+ configIdsMap.set(space.configurationId, space);
400
+ }
401
+ if (space.payerIDs) {
402
+ for (const pId of space.payerIDs) {
403
+ const currentSpacesForPayerId = payerIdsMap.get(pId);
404
+ if (currentSpacesForPayerId) {
405
+ payerIdsMap.set(pId, [...currentSpacesForPayerId, space]);
406
+ } else {
407
+ payerIdsMap.set(pId, [space]);
408
+ }
409
+ }
410
+ }
411
+ }
412
+ }
413
+ }
414
+ dispatch({
415
+ type: "SPACES",
416
+ spaces: spacesMap,
417
+ spacesByConfig: configIdsMap,
418
+ spacesByPayer: payerIdsMap,
419
+ loading: false
420
+ });
421
+ }, [spacesBySpaceIds, spacesByPayerIds, payerIds, spaceIds]);
422
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
423
+ SpacesContext.Provider,
424
+ {
425
+ children,
426
+ value: {
427
+ spaces: spacesMap,
428
+ spacesByConfig: configIdsMap,
429
+ spacesByPayer: payerIdsMap,
430
+ loading: loading || isLoadingByPayerIds || isLoadingBySpaceIds,
431
+ error
432
+ }
433
+ }
434
+ );
435
+ };
436
+ var useSpaces = (...ids) => {
437
+ const { spaces, spacesByConfig, spacesByPayer } = useSpacesContext();
438
+ const idsIsEmpty = !ids || ids.length === 0;
439
+ const callerIsExpectingFirstSpace = (ids == null ? void 0 : ids.length) === 1 && ids[0] === void 0;
440
+ const shouldReturnAllSpaces = idsIsEmpty || callerIsExpectingFirstSpace;
441
+ if (shouldReturnAllSpaces) {
442
+ console.warn(`You did not pass in an ID to find a space, returning all spaces.`);
443
+ return spaces && normalizeSpaces([...spaces.values()]);
444
+ }
445
+ const matchedSpaces = ids.map((id) => (spaces == null ? void 0 : spaces.get(id)) || (spacesByConfig == null ? void 0 : spacesByConfig.get(id)) || (spacesByPayer == null ? void 0 : spacesByPayer.get(id)));
446
+ const normalized = normalizeSpaces(matchedSpaces);
447
+ return normalized;
448
+ };
449
+ // Annotate the CommonJS export names for ESM import in node:
450
+ 0 && (module.exports = {
451
+ INITIAL_STATE,
452
+ Spaces,
453
+ SpacesContext,
454
+ useSpaces,
455
+ useSpacesContext
456
+ });