dce-reactkit 3.3.1 → 3.3.3-beta.1

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.
@@ -24,14 +24,7 @@ declare type SendRequestFunction = (opts: {
24
24
  * @author Gabe Abrams
25
25
  * @returns sendRequest function
26
26
  */
27
- export declare const getSendRequest: () => SendRequestFunction;
28
- declare let dark: boolean | undefined;
29
- /**
30
- * Get the current dark/light theme
31
- * @author Gabe Abrams
32
- * @returns true if the app has a dark theme
33
- */
34
- export declare const darkModeIsOn: () => boolean;
27
+ export declare const getSendRequest: () => Promise<SendRequestFunction>;
35
28
  declare let sessionExpiredMessage: string | undefined;
36
29
  /**
37
30
  * Get the custom session expired message
@@ -44,12 +37,10 @@ export declare const getSessionExpiredMessage: () => string;
44
37
  * @author Gabe Abrams
45
38
  * @param opts object containing all arguments
46
39
  * @param opts.sendRequest caccl send request functions
47
- * @param [opts.dark] if true, the app is a dark-themed app
48
40
  * @param [opts.sessionExpiredMessage] a custom session expired message
49
41
  */
50
42
  declare const initClient: (opts: {
51
43
  sendRequest: SendRequestFunction;
52
- dark?: boolean;
53
44
  sessionExpiredMessage?: string;
54
45
  }) => void;
55
46
  export default initClient;
@@ -7,6 +7,7 @@ import React from 'react';
7
7
  import Variant from '../types/Variant';
8
8
  declare type Props = {
9
9
  children: React.ReactNode;
10
+ dark?: boolean;
10
11
  };
11
12
  /**
12
13
  * Show an alert modal with an "Okay" button
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ declare enum Variant {
24
24
 
25
25
  declare type Props$g = {
26
26
  children: React.ReactNode;
27
+ dark?: boolean;
27
28
  };
28
29
  /**
29
30
  * Show an alert modal with an "Okay" button
@@ -529,12 +530,10 @@ declare type SendRequestFunction = (opts: {
529
530
  * @author Gabe Abrams
530
531
  * @param opts object containing all arguments
531
532
  * @param opts.sendRequest caccl send request functions
532
- * @param [opts.dark] if true, the app is a dark-themed app
533
533
  * @param [opts.sessionExpiredMessage] a custom session expired message
534
534
  */
535
535
  declare const initClient: (opts: {
536
536
  sendRequest: SendRequestFunction;
537
- dark?: boolean;
538
537
  sessionExpiredMessage?: string;
539
538
  }) => void;
540
539
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "3.3.1",
3
+ "version": "3.3.3-beta.1",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -3,6 +3,7 @@ import { showFatalError } from '../components/AppWrapper';
3
3
 
4
4
  // Import shared types
5
5
  import ErrorWithCode from '../errors/ErrorWithCode';
6
+ import waitMs from '../helpers/waitMs';
6
7
  import ReactKitErrorCode from '../types/ReactKitErrorCode';
7
8
 
8
9
 
@@ -40,7 +41,10 @@ type SendRequestFunction = (
40
41
 
41
42
  /* ----------- Initialized ---------- */
42
43
 
43
- let initialized = false;
44
+ let onInitialized: (a: unknown) => void;
45
+ let initialized = new Promise((resolve) => {
46
+ onInitialized = resolve;
47
+ });
44
48
 
45
49
  /* ---------- Send Request ---------- */
46
50
 
@@ -51,9 +55,19 @@ let storedSendRequest: SendRequestFunction;
51
55
  * @author Gabe Abrams
52
56
  * @returns sendRequest function
53
57
  */
54
- export const getSendRequest = () => {
58
+ export const getSendRequest = async () => {
59
+ // Wait for initialization or timeout
60
+ let timedOut = false;
61
+ await Promise.all([
62
+ (async () => {
63
+ await waitMs(5000);
64
+ timedOut = true;
65
+ })(),
66
+ initialized,
67
+ ]);
68
+
55
69
  // Error if no send request function
56
- if (!initialized) {
70
+ if (timedOut) {
57
71
  showFatalError(
58
72
  new ErrorWithCode(
59
73
  'Could not send a request because the request needed to be sent before dce-reactkit was properly initialized. Perhaps dce-reactkit was not initialized with initClient.',
@@ -73,31 +87,6 @@ export const getSendRequest = () => {
73
87
  return storedSendRequest;
74
88
  };
75
89
 
76
- /* ------------ Dark Mode ----------- */
77
-
78
- let dark: boolean | undefined;
79
-
80
- /**
81
- * Get the current dark/light theme
82
- * @author Gabe Abrams
83
- * @returns true if the app has a dark theme
84
- */
85
- export const darkModeIsOn = () => {
86
- // Error if not set up yet
87
- if (!initialized) {
88
- showFatalError(
89
- new ErrorWithCode(
90
- 'Could not check theme color because dce-reactkit was properly initialized. Perhaps dce-reactkit was not initialized with initClient.',
91
- ReactKitErrorCode.ThemeCheckedBeforeReactKitReady,
92
- ),
93
- );
94
- return false;
95
- }
96
-
97
- // Return
98
- return !!dark;
99
- };
100
-
101
90
  /* ----- Session Expired Message ---- */
102
91
 
103
92
  let sessionExpiredMessage: string | undefined;
@@ -108,17 +97,6 @@ let sessionExpiredMessage: string | undefined;
108
97
  * @returns session expired message
109
98
  */
110
99
  export const getSessionExpiredMessage = () => {
111
- // Error if not set up yet
112
- if (!initialized) {
113
- showFatalError(
114
- new ErrorWithCode(
115
- 'Could not get the session expired message because dce-reactkit was properly initialized. Perhaps dce-reactkit was not initialized with initClient.',
116
- ReactKitErrorCode.SessionExpiredMessageGottenBeforeReactKitReady,
117
- ),
118
- );
119
- return '';
120
- }
121
-
122
100
  // Return
123
101
  return (
124
102
  sessionExpiredMessage
@@ -135,26 +113,22 @@ export const getSessionExpiredMessage = () => {
135
113
  * @author Gabe Abrams
136
114
  * @param opts object containing all arguments
137
115
  * @param opts.sendRequest caccl send request functions
138
- * @param [opts.dark] if true, the app is a dark-themed app
139
116
  * @param [opts.sessionExpiredMessage] a custom session expired message
140
117
  */
141
118
  const initClient = (
142
119
  opts: {
143
120
  // Copy of CACCL's send request function
144
121
  sendRequest: SendRequestFunction,
145
- // True if this app is a dark-themed app
146
- dark?: boolean,
147
122
  // Custom session expired message
148
123
  sessionExpiredMessage?: string,
149
124
  },
150
125
  ) => {
151
126
  // Store values
152
127
  storedSendRequest = opts.sendRequest;
153
- dark = !!opts.dark;
154
128
  sessionExpiredMessage = opts.sessionExpiredMessage;
155
129
 
156
130
  // Mark as initialized
157
- initialized = true;
131
+ onInitialized(null);
158
132
  };
159
133
 
160
134
  export default initClient;
@@ -26,7 +26,6 @@ import ErrorWithCode from '../errors/ErrorWithCode';
26
26
  // Import other helpers
27
27
  import logClientEvent from '../helpers/logClientEvent';
28
28
  import {
29
- darkModeIsOn,
30
29
  getSessionExpiredMessage,
31
30
  } from '../client/initClient';
32
31
 
@@ -37,6 +36,8 @@ import {
37
36
  type Props = {
38
37
  // The entire app
39
38
  children: React.ReactNode,
39
+ // True if this app is a dark-themed app
40
+ dark?: boolean,
40
41
  };
41
42
 
42
43
  /*------------------------------------------------------------------------*/
@@ -237,6 +238,7 @@ const AppWrapper: React.FC<Props> = (props: Props): React.ReactElement => {
237
238
 
238
239
  const {
239
240
  children,
241
+ dark,
240
242
  } = props;
241
243
 
242
244
  /* -------------- State ------------- */
@@ -387,7 +389,7 @@ const AppWrapper: React.FC<Props> = (props: Props): React.ReactElement => {
387
389
  minHeight: '100vh',
388
390
  paddingTop: '2rem',
389
391
  backgroundColor: (
390
- darkModeIsOn()
392
+ dark
391
393
  ? '#222'
392
394
  : '#fff'
393
395
  ),
@@ -138,7 +138,7 @@ const visitServerEndpoint = async (
138
138
  }
139
139
 
140
140
  // Send the request
141
- const sendRequest = getSendRequest();
141
+ const sendRequest = await getSendRequest();
142
142
  const response = await sendRequest({
143
143
  path: opts.path,
144
144
  method: opts.method ?? 'GET',