dce-reactkit 3.6.1 → 3.6.2

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.
@@ -50,7 +50,7 @@ export declare const confirm: (title: string, text: string, opts?: {
50
50
  * @param error the error to show
51
51
  * @param [errorTitle] title of the error box
52
52
  */
53
- export declare const showFatalError: (error: any, errorTitle?: string) => undefined;
53
+ export declare const showFatalError: (error: any, errorTitle?: string) => Promise<void>;
54
54
  /**
55
55
  * Add a handler for when a fatal error occurs
56
56
  * @author Gabe Abrams
@@ -60,6 +60,6 @@ export declare const addFatalErrorHandler: (handler: () => void) => void;
60
60
  * Show the "session expired" message
61
61
  * @author Gabe Abrams
62
62
  */
63
- export declare const showSessionExpiredMessage: () => undefined;
63
+ export declare const showSessionExpiredMessage: () => void;
64
64
  declare const AppWrapper: React.FC<Props>;
65
65
  export default AppWrapper;
package/dist/index.d.ts CHANGED
@@ -69,7 +69,7 @@ declare const confirm: (title: string, text: string, opts?: {
69
69
  * @param error the error to show
70
70
  * @param [errorTitle] title of the error box
71
71
  */
72
- declare const showFatalError: (error: any, errorTitle?: string) => undefined;
72
+ declare const showFatalError: (error: any, errorTitle?: string) => Promise<void>;
73
73
  /**
74
74
  * Add a handler for when a fatal error occurs
75
75
  * @author Gabe Abrams
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "3.6.1",
3
+ "version": "3.6.2",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -33,6 +33,7 @@ import {
33
33
  getSessionExpiredMessage,
34
34
  isDarkModeOn,
35
35
  } from '../client/initClient';
36
+ import waitMs from '../helpers/waitMs';
36
37
 
37
38
  /*------------------------------------------------------------------------*/
38
39
  /* -------------------------------- Props ------------------------------- */
@@ -47,6 +48,32 @@ type Props = {
47
48
  /* --------------------------- Static Helpers --------------------------- */
48
49
  /*------------------------------------------------------------------------*/
49
50
 
51
+ // Timestamp after initialization when helpers should be available
52
+ const timestampWhenHelpersShouldBeAvailable = Date.now() + 2000;
53
+
54
+ /**
55
+ * Wait for a little while for a helper to exist
56
+ * @author Gabe Abrams
57
+ * @param checkForHelper a function that returns true if the helper exists
58
+ * @returns true if the helper exists, false if the process timed out
59
+ */
60
+ const waitForHelper = async (checkForHelper: () => boolean): Promise<boolean> => {
61
+ // Wait for helper to exist
62
+ while (!checkForHelper()) {
63
+ // Check if we should stop waiting
64
+ if (Date.now() > timestampWhenHelpersShouldBeAvailable) {
65
+ // Stop waiting
66
+ return false;
67
+ }
68
+
69
+ // Wait a little while
70
+ await waitMs(10);
71
+ }
72
+
73
+ // Helper exists
74
+ return true;
75
+ };
76
+
50
77
  /*----------------------------------------*/
51
78
  /* ----------- Redirect/Leave ----------- */
52
79
  /*----------------------------------------*/
@@ -103,6 +130,11 @@ let onAlertClosed: () => void;
103
130
  * @param text the text to display in the alert
104
131
  */
105
132
  export const alert = async (title: string, text: string): Promise<undefined> => {
133
+ // Wait for helper to exist
134
+ await waitForHelper(() => {
135
+ return !!setAlertInfo;
136
+ });
137
+
106
138
  // Fallback if alert not available
107
139
  if (!setAlertInfo) {
108
140
  // eslint-disable-next-line no-alert
@@ -172,6 +204,11 @@ export const confirm = async (
172
204
  cancelButtonVariant?: Variant,
173
205
  },
174
206
  ): Promise<boolean> => {
207
+ // Wait for helper to exist
208
+ await waitForHelper(() => {
209
+ return !!setConfirmInfo;
210
+ });
211
+
175
212
  // Fallback if confirm is not available
176
213
  if (!setConfirmInfo) {
177
214
  // eslint-disable-next-line no-alert
@@ -212,10 +249,10 @@ const fatalErrorHandlers: (() => void)[] = [];
212
249
  * @param error the error to show
213
250
  * @param [errorTitle] title of the error box
214
251
  */
215
- export const showFatalError = (
252
+ export const showFatalError = async (
216
253
  error: any,
217
254
  errorTitle: string = 'An Error Occurred',
218
- ): undefined => {
255
+ ) => {
219
256
  // Determine message and code
220
257
  const message: string = (
221
258
  typeof error === 'string'
@@ -250,21 +287,27 @@ export const showFatalError = (
250
287
  },
251
288
  });
252
289
 
290
+ // Wait for helper to exist
291
+ await waitForHelper(() => {
292
+ return (
293
+ !!setFatalErrorMessage
294
+ && !!setFatalErrorCode
295
+ );
296
+ });
297
+
253
298
  // Handle case where app hasn't loaded
254
299
  if (!setFatalErrorMessage || !setFatalErrorCode) {
255
300
  alert(
256
301
  errorTitle,
257
302
  `${message} (code: ${code}). Please contact support.`,
258
303
  );
259
- return undefined;
304
+ return;
260
305
  }
261
306
 
262
307
  // Use setters
263
308
  setFatalErrorMessage(message);
264
309
  setFatalErrorCode(code);
265
310
  setFatalErrorTitle(errorTitle);
266
-
267
- return undefined;
268
311
  };
269
312
 
270
313
  /**
@@ -286,9 +329,8 @@ let setSessionHasExpired: (isExpired: boolean) => void;
286
329
  * Show the "session expired" message
287
330
  * @author Gabe Abrams
288
331
  */
289
- export const showSessionExpiredMessage = (): undefined => {
332
+ export const showSessionExpiredMessage = () => {
290
333
  setSessionHasExpired(true);
291
- return undefined;
292
334
  };
293
335
 
294
336
  /*------------------------------------------------------------------------*/