@capillarytech/cap-ui-utils 1.4.3 → 1.4.5

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/index.js CHANGED
@@ -5,4 +5,5 @@ export { default as utilsSessionStorageApi } from "./utils/utilsSessionStorageAp
5
5
  export { default as dateHelper } from "./utils/date/dateHelper";
6
6
  export { default as formatter } from "./utils/formatter";
7
7
  export { default as validationHelper } from "./utils/validationHelper";
8
+ export { default as loadable, FORCE_REFRESH_NOTIFIER, FORCE_REFRESH_SECONDS } from './utils/loadable';
8
9
  export { default as GTMTracker } from './utils/gtmTracker';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,7 +23,7 @@ export default class GTMTracker {
23
23
  });
24
24
  }
25
25
 
26
- push = (eventType, eventObject) => window.dataLayer.push({ ...eventObject, event: eventType })
26
+ push = (eventType, eventObject = {}) => window.dataLayer.push({ ...eventObject, event: eventType })
27
27
 
28
28
  getUserGtmData = (userDetails = {}) => {
29
29
  try {
@@ -0,0 +1,49 @@
1
+ import { lazy } from 'react';
2
+
3
+ export const FORCE_REFRESH_NOTIFIER = 'showForceRefreshNotifier';
4
+ export const FORCE_REFRESH_SECONDS = 5;
5
+
6
+ const reloadWithDelay = (delay = FORCE_REFRESH_SECONDS) =>
7
+ new Promise((resolve) => {
8
+ setTimeout(() => {
9
+ // reload page
10
+ resolve(window.location.reload());
11
+ }, delay * 1000);
12
+ });
13
+
14
+ export const lazyWithRetry = (importFunction) =>
15
+ lazy(async () => {
16
+ // get refresh flag from session storage
17
+ const pageHasAlreadyBeenForceRefreshed = JSON.parse(
18
+ window.sessionStorage.getItem(
19
+ 'page-has-been-force-refreshed'
20
+ ) || 'false'
21
+ );
22
+ try {
23
+ const component = await importFunction();
24
+ // set refresh flag to false once component is imported
25
+ window.sessionStorage.setItem(
26
+ 'page-has-been-force-refreshed',
27
+ 'false'
28
+ );
29
+ return component;
30
+ } catch (error) {
31
+ if (!pageHasAlreadyBeenForceRefreshed) {
32
+ // Assuming that the user is NOT on the latest version of the application.
33
+ window.sessionStorage.setItem(
34
+ 'page-has-been-force-refreshed',
35
+ 'true'
36
+ );
37
+ // Notify the main app to show notification if needed
38
+ window.postMessage(FORCE_REFRESH_NOTIFIER, window.location.origin);
39
+ // Let's refresh the page immediately.
40
+ return reloadWithDelay();
41
+ }
42
+ // The page has already been reloaded
43
+ // Assuming that user is already using the latest version of the application.
44
+ // Let's let the application crash and raise the error.
45
+ throw error;
46
+ }
47
+ });
48
+
49
+ export default lazyWithRetry;