dce-reactkit 3.0.0-beta.4 → 3.0.0-beta.40

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 (60) hide show
  1. package/.eslintrc.js +95 -0
  2. package/README.md +1 -546
  3. package/dist/cjs/index.js +784 -85
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/types/components/AppWrapper.d.ts +25 -2
  6. package/dist/cjs/types/components/ButtonInputGroup.d.ts +12 -0
  7. package/dist/cjs/types/components/CheckboxButton.d.ts +20 -0
  8. package/dist/cjs/types/components/RadioButton.d.ts +20 -0
  9. package/dist/cjs/types/components/SimpleDateChooser.d.ts +21 -0
  10. package/dist/cjs/types/components/TabBox.d.ts +1 -0
  11. package/dist/cjs/types/helpers/genRouteHandler.d.ts +30 -0
  12. package/dist/cjs/types/helpers/getOrdinal.d.ts +8 -0
  13. package/dist/cjs/types/helpers/getTimeInfoInET.d.ts +17 -0
  14. package/dist/cjs/types/helpers/handleError.d.ts +18 -0
  15. package/dist/cjs/types/helpers/handleSuccess.d.ts +8 -0
  16. package/dist/cjs/types/helpers/visitServerEndpoint.d.ts +23 -0
  17. package/dist/cjs/types/index.d.ts +10 -1
  18. package/dist/cjs/types/server/initServer.d.ts +21 -0
  19. package/dist/cjs/types/types/ParamType.d.ts +17 -0
  20. package/dist/cjs/types/types/ReactKitErrorCode.d.ts +3 -1
  21. package/dist/esm/index.js +776 -86
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/types/components/AppWrapper.d.ts +25 -2
  24. package/dist/esm/types/components/ButtonInputGroup.d.ts +12 -0
  25. package/dist/esm/types/components/CheckboxButton.d.ts +20 -0
  26. package/dist/esm/types/components/RadioButton.d.ts +20 -0
  27. package/dist/esm/types/components/SimpleDateChooser.d.ts +21 -0
  28. package/dist/esm/types/components/TabBox.d.ts +1 -0
  29. package/dist/esm/types/helpers/genRouteHandler.d.ts +30 -0
  30. package/dist/esm/types/helpers/getOrdinal.d.ts +8 -0
  31. package/dist/esm/types/helpers/getTimeInfoInET.d.ts +17 -0
  32. package/dist/esm/types/helpers/handleError.d.ts +18 -0
  33. package/dist/esm/types/helpers/handleSuccess.d.ts +8 -0
  34. package/dist/esm/types/helpers/visitServerEndpoint.d.ts +23 -0
  35. package/dist/esm/types/index.d.ts +10 -1
  36. package/dist/esm/types/server/initServer.d.ts +21 -0
  37. package/dist/esm/types/types/ParamType.d.ts +17 -0
  38. package/dist/esm/types/types/ReactKitErrorCode.d.ts +3 -1
  39. package/dist/index.d.ts +182 -10
  40. package/package.json +13 -1
  41. package/rollup.config.js +0 -1
  42. package/src/components/AppWrapper.tsx +73 -15
  43. package/src/components/ButtonInputGroup.tsx +89 -0
  44. package/src/components/CheckboxButton.tsx +100 -0
  45. package/src/components/ErrorBox.tsx +4 -4
  46. package/src/components/LoadingSpinner.tsx +3 -3
  47. package/src/components/Modal.tsx +185 -71
  48. package/src/components/RadioButton.tsx +102 -0
  49. package/src/components/SimpleDateChooser.tsx +217 -0
  50. package/src/components/TabBox.tsx +65 -58
  51. package/src/helpers/genRouteHandler.ts +326 -0
  52. package/src/helpers/getOrdinal.tsx +13 -0
  53. package/src/helpers/getTimeInfoInET.tsx +56 -0
  54. package/src/helpers/handleError.ts +65 -0
  55. package/src/helpers/handleSuccess.ts +18 -0
  56. package/src/helpers/visitServerEndpoint.tsx +110 -0
  57. package/src/index.ts +21 -0
  58. package/src/server/initServer.ts +53 -0
  59. package/src/types/ParamType.ts +18 -0
  60. package/src/types/ReactKitErrorCode.tsx +3 -1
@@ -0,0 +1,110 @@
1
+ // Import custom error
2
+ import ErrorWithCode from '../errors/ErrorWithCode';
3
+ import ReactKitErrorCode from '../types/ReactKitErrorCode';
4
+
5
+ // Import helpers from app wrapper
6
+ import { cacclSendRequest } from '../components/AppWrapper';
7
+
8
+ /*------------------------------------------------------------------------*/
9
+ /* Listener */
10
+ /*------------------------------------------------------------------------*/
11
+
12
+ // Handler for session expiry
13
+ let sessionExpiryHandler: () => void;
14
+
15
+ // Keep track of whether or not session expiry has already been handled
16
+ let sessionAlreadyExpired = false;
17
+
18
+ /**
19
+ * Set the session expiry handler
20
+ * @author Gabe Abrams
21
+ * @param handler new handler to use when session expires
22
+ */
23
+ export const setSessionExpiryHandler = (handler: () => void) => {
24
+ sessionExpiryHandler = handler;
25
+ };
26
+
27
+ /*------------------------------------------------------------------------*/
28
+ /* Main */
29
+ /*------------------------------------------------------------------------*/
30
+
31
+ /**
32
+ * Visit an endpoint on the server [for client only]
33
+ * @author Gabe Abrams
34
+ * @param opts object containing all arguments
35
+ * @param opts.path - the path of the server endpoint
36
+ * @param [opts.method=GET] - the method of the endpoint
37
+ * @param [opts.params] - query/body parameters to include
38
+ * @returns response from server
39
+ */
40
+ const visitServerEndpoint = async (
41
+ opts: {
42
+ path: string,
43
+ method?: ('GET' | 'POST' | 'DELETE' | 'PUT'),
44
+ params?: { [key in string]: any },
45
+ },
46
+ ): Promise<any> => {
47
+ // Send the request
48
+ const response = await cacclSendRequest({
49
+ path: opts.path,
50
+ method: opts.method ?? 'GET',
51
+ params: opts.params,
52
+ });
53
+
54
+ // Check for failure
55
+ if (!response || !response.body) {
56
+ throw new ErrorWithCode(
57
+ 'We didn\'t get a response from the server. Please check your internet connection.',
58
+ ReactKitErrorCode.NoResponse,
59
+ );
60
+ }
61
+ if (!response.body.success) {
62
+ // Session expired
63
+ if (response.body.code === ReactKitErrorCode.SessionExpired) {
64
+ // Skip notice if session was already expired
65
+ if (sessionAlreadyExpired) {
66
+ // Never return (browser is already reloading)
67
+ await new Promise<{ [key in string]: any }>(() => {
68
+ // Promise that never returns
69
+ });
70
+ }
71
+ sessionAlreadyExpired = true;
72
+
73
+ // Show session expiration message
74
+ if (sessionExpiryHandler) {
75
+ // Use handler
76
+ sessionExpiryHandler();
77
+ } else {
78
+ // Fallback to alert
79
+
80
+ // eslint-disable-next-line no-alert
81
+ alert('Your session has expired. Please start over.');
82
+ }
83
+
84
+ // Never return (don't continue execution)
85
+ await new Promise<{ [key in string]: any }>(() => {
86
+ // Promise that never returns
87
+ });
88
+ }
89
+
90
+ // Other errors
91
+ throw new ErrorWithCode(
92
+ (
93
+ response.body.message
94
+ || 'An unknown error occurred. Please contact an admin.'
95
+ ),
96
+ (
97
+ response.body.code
98
+ || ReactKitErrorCode.NoCode
99
+ ),
100
+ );
101
+ }
102
+
103
+ // Success! Extract the body
104
+ const { body } = response.body;
105
+
106
+ // Return
107
+ return body;
108
+ };
109
+
110
+ export default visitServerEndpoint;
package/src/index.ts CHANGED
@@ -4,6 +4,9 @@ import LoadingSpinner from './components/LoadingSpinner';
4
4
  import ErrorBox from './components/ErrorBox';
5
5
  import Modal from './components/Modal';
6
6
  import TabBox from './components/TabBox';
7
+ import RadioButton from './components/RadioButton';
8
+ import CheckboxButton from './components/CheckboxButton';
9
+ import ButtonInputGroup from './components/ButtonInputGroup';
7
10
 
8
11
  // Import errors
9
12
  import ErrorWithCode from './errors/ErrorWithCode';
@@ -19,6 +22,11 @@ import padZerosLeft from './helpers/padZerosLeft';
19
22
  import roundToNumDecimals from './helpers/roundToNumDecimals';
20
23
  import sum from './helpers/sum';
21
24
  import waitMs from './helpers/waitMs';
25
+ import visitServerEndpoint from './helpers/visitServerEndpoint';
26
+ import genRouteHandler from './helpers/genRouteHandler';
27
+ import handleError from './helpers/handleError';
28
+ import handleSuccess from './helpers/handleSuccess';
29
+ import initServer from './server/initServer';
22
30
 
23
31
  // Import types
24
32
  import ModalButtonType from './types/ModalButtonType';
@@ -26,6 +34,7 @@ import ModalSize from './types/ModalSize';
26
34
  import ModalType from './types/ModalType';
27
35
  import ReactKitErrorCode from './types/ReactKitErrorCode';
28
36
  import Variant from './types/Variant';
37
+ import ParamType from './types/ParamType';
29
38
 
30
39
  // Export each item
31
40
  export {
@@ -35,6 +44,9 @@ export {
35
44
  ErrorBox,
36
45
  Modal,
37
46
  TabBox,
47
+ RadioButton,
48
+ CheckboxButton,
49
+ ButtonInputGroup,
38
50
  // Global functions
39
51
  alert,
40
52
  confirm,
@@ -52,10 +64,19 @@ export {
52
64
  roundToNumDecimals,
53
65
  sum,
54
66
  waitMs,
67
+ // Client helpers
68
+ visitServerEndpoint,
69
+ // Server helpers
70
+ initServer,
71
+ genRouteHandler,
72
+ handleError,
73
+ handleSuccess,
55
74
  // Types
56
75
  ModalButtonType,
57
76
  ModalSize,
58
77
  ModalType,
59
78
  ReactKitErrorCode,
60
79
  Variant,
80
+ // Server types
81
+ ParamType,
61
82
  };
@@ -0,0 +1,53 @@
1
+ // Import custom error
2
+ import ErrorWithCode from '../errors/ErrorWithCode';
3
+ import ReactKitErrorCode from '../types/ReactKitErrorCode';
4
+
5
+ // Types
6
+ type GetLaunchInfoFunction = (req: any) => {
7
+ launched: boolean,
8
+ launchInfo?: any,
9
+ };
10
+
11
+ // Stored copy of caccl functions
12
+ let _cacclGetLaunchInfo: GetLaunchInfoFunction;
13
+
14
+ /*------------------------------------------------------------------------*/
15
+ /* Helpers */
16
+ /*------------------------------------------------------------------------*/
17
+
18
+ /**
19
+ * Get launch info via CACCL
20
+ * @author Gabe Abrams
21
+ * @param req express request object
22
+ * @returns object { launched, launchInfo }
23
+ */
24
+ export const cacclGetLaunchInfo: GetLaunchInfoFunction = (req: any) => {
25
+ if (!_cacclGetLaunchInfo) {
26
+ throw new ErrorWithCode(
27
+ 'Could not get launch info because server was not initialized with dce-reactkit\'s initServer function',
28
+ ReactKitErrorCode.NoCACCLGetLaunchInfoFunction,
29
+ );
30
+ }
31
+
32
+ return _cacclGetLaunchInfo(req);
33
+ };
34
+
35
+ /*------------------------------------------------------------------------*/
36
+ /* Main */
37
+ /*------------------------------------------------------------------------*/
38
+
39
+ /**
40
+ * Prepare dce-reactkit to run on the server
41
+ * @author Gabe Abrams
42
+ * @param opts object containing all arguments
43
+ * @param opts.getLaunchInfo CACCL LTI's get launch info function
44
+ */
45
+ const initServer = (
46
+ opts: {
47
+ getLaunchInfo: GetLaunchInfoFunction,
48
+ },
49
+ ) => {
50
+ _cacclGetLaunchInfo = opts.getLaunchInfo;
51
+ };
52
+
53
+ export default initServer;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Server-side API param types
3
+ * @author Gabe Abrams
4
+ */
5
+ enum ParamType {
6
+ Boolean = 'boolean', // Boolean
7
+ BooleanOptional = 'boolean-optional', // Optional boolean
8
+ Float = 'float', // Float Number
9
+ FloatOptional = 'float-optional', // Optional Float Number
10
+ Int = 'int', // Integer Number
11
+ IntOptional = 'int-optional', // Optional Integer Number
12
+ JSON = 'json', // JSONified object
13
+ JSONOptional = 'json-optional', // Optional JSONified object
14
+ String = 'string', // String
15
+ StringOptional = 'string-optional', // Optional string
16
+ }
17
+
18
+ export default ParamType;
@@ -1,4 +1,4 @@
1
- // Highest error code = DRK2
1
+ // Highest error code = DRK8
2
2
 
3
3
  /**
4
4
  * List of error codes built into the react kit
@@ -11,6 +11,8 @@ enum ReactKitErrorCode {
11
11
  MissingParameter = 'DRK4',
12
12
  InvalidParameter = 'DRK5',
13
13
  WrongCourse = 'DRK6',
14
+ NoCACCLSendRequestFunction = 'DRK7',
15
+ NoCACCLGetLaunchInfoFunction = 'DRK8',
14
16
  }
15
17
 
16
18
  export default ReactKitErrorCode;