dce-reactkit 2.0.9 → 2.0.12
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/lib/components/AppWrapper.d.ts +16 -1
- package/lib/components/AppWrapper.js +157 -13
- package/lib/components/AppWrapper.js.map +1 -1
- package/lib/components/Modal.d.ts +2 -0
- package/lib/components/Modal.js +9 -3
- package/lib/components/Modal.js.map +1 -1
- package/lib/components/TabBox.d.ts +11 -0
- package/lib/components/TabBox.js +42 -0
- package/lib/components/TabBox.js.map +1 -0
- package/lib/helpers/alert.d.ts +2 -0
- package/lib/helpers/alert.js +6 -0
- package/lib/helpers/alert.js.map +1 -0
- package/lib/helpers/confirm.d.ts +2 -0
- package/lib/helpers/confirm.js +6 -0
- package/lib/helpers/confirm.js.map +1 -0
- package/lib/helpers/showFatalError.d.ts +1 -1
- package/lib/helpers/showFatalError.js +2 -1
- package/lib/helpers/showFatalError.js.map +1 -1
- package/package.json +1 -1
- package/src/components/AppWrapper.tsx +191 -11
- package/src/components/Modal.tsx +16 -2
- package/src/components/TabBox.tsx +136 -0
- package/src/helpers/alert.tsx +5 -0
- package/src/helpers/confirm.tsx +5 -0
- package/src/helpers/showFatalError.tsx +3 -1
- package/lib/components/AppWrapper.tsx +0 -181
- package/lib/components/ErrorBox.tsx +0 -124
- package/lib/components/LoadingSpinner.tsx +0 -105
- package/lib/components/Modal.tsx +0 -447
- package/lib/errors/ErrorWithCode.tsx +0 -15
- package/lib/helpers/abbreviate.tsx +0 -23
- package/lib/helpers/avg.tsx +0 -22
- package/lib/helpers/ceilToNumDecimals.tsx +0 -13
- package/lib/helpers/floorToNumDecimals.tsx +0 -13
- package/lib/helpers/forceNumIntoBounds.tsx +0 -19
- package/lib/helpers/handleError.ts +0 -65
- package/lib/helpers/handleSuccess.ts +0 -18
- package/lib/helpers/padDecimalZeros.tsx +0 -32
- package/lib/helpers/padZerosLeft.tsx +0 -21
- package/lib/helpers/parseRequest.ts +0 -299
- package/lib/helpers/roundToNumDecimals.tsx +0 -13
- package/lib/helpers/showFatalError.tsx +0 -3
- package/lib/helpers/sum.tsx +0 -16
- package/lib/helpers/visitServerEndpoint.tsx +0 -110
- package/lib/helpers/waitMs.tsx +0 -12
- package/lib/types/ParamType.ts +0 -18
- package/lib/types/ReactKitErrorCode.tsx +0 -16
- package/lib/types/Variant.tsx +0 -16
package/lib/helpers/avg.tsx
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import sum from './sum';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get the average of a set of numbers
|
|
5
|
-
* @author Gabe Abrams
|
|
6
|
-
* @param nums the numbers to average
|
|
7
|
-
* @returns average value or 0 if no numbers
|
|
8
|
-
*/
|
|
9
|
-
const avg = (nums: number[]) => {
|
|
10
|
-
// Handle empty array case
|
|
11
|
-
if (nums.length === 0) {
|
|
12
|
-
return 0;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Get the total value
|
|
16
|
-
const total = sum(nums);
|
|
17
|
-
|
|
18
|
-
// Get average
|
|
19
|
-
return (total / nums.length);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export default avg;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round a number (ceiling) to a certain number of decimals
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param num the number to round
|
|
5
|
-
* @param numDecimals the number of decimals to round to
|
|
6
|
-
* @returns rounded number
|
|
7
|
-
*/
|
|
8
|
-
const ceilToNumDecimals = (num: number, numDecimals: number): number => {
|
|
9
|
-
const rounder = 10 ** numDecimals;
|
|
10
|
-
return (Math.ceil(num * rounder) / rounder);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default ceilToNumDecimals;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round a number (floor) to a certain number of decimals
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param num the number to round
|
|
5
|
-
* @param numDecimals the number of decimals to round to
|
|
6
|
-
* @returns rounded number
|
|
7
|
-
*/
|
|
8
|
-
const floorToNumDecimals = (num: number, numDecimals: number): number => {
|
|
9
|
-
const rounder = 10 ** numDecimals;
|
|
10
|
-
return (Math.floor(num * rounder) / rounder);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default floorToNumDecimals;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Force a number to stay within specific bounds
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param num the number to move into the bounds
|
|
5
|
-
* @param min the minimum number in the bound
|
|
6
|
-
* @param max the maximum number in the bound
|
|
7
|
-
* @returns bounded number
|
|
8
|
-
*/
|
|
9
|
-
const forceNumIntoBounds = (num: number, min: number, max: number): number => {
|
|
10
|
-
return Math.max(
|
|
11
|
-
min,
|
|
12
|
-
Math.min(
|
|
13
|
-
max,
|
|
14
|
-
num,
|
|
15
|
-
),
|
|
16
|
-
);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export default forceNumIntoBounds;
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// Import shared types
|
|
2
|
-
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Handle an error and respond to the client
|
|
6
|
-
* @author Gabe Abrams
|
|
7
|
-
* @param res express response
|
|
8
|
-
* @param error error info
|
|
9
|
-
* @param opts.err the error to send to the client
|
|
10
|
-
* or the error message
|
|
11
|
-
* @param [opts.code] an error code (only used if err.code is not
|
|
12
|
-
* included)
|
|
13
|
-
* @param [opts.status=500] the https status code to use
|
|
14
|
-
* defined)
|
|
15
|
-
*/
|
|
16
|
-
const handleError = (
|
|
17
|
-
res: any,
|
|
18
|
-
error: (
|
|
19
|
-
| {
|
|
20
|
-
message: any,
|
|
21
|
-
code?: string,
|
|
22
|
-
status?: number,
|
|
23
|
-
}
|
|
24
|
-
| Error
|
|
25
|
-
| string
|
|
26
|
-
| any
|
|
27
|
-
),
|
|
28
|
-
): undefined => {
|
|
29
|
-
// Get the error message
|
|
30
|
-
let message;
|
|
31
|
-
if (error && (error as any).message) {
|
|
32
|
-
message = (error.message || 'An unknown error occurred.');
|
|
33
|
-
} else if (typeof error === 'string') {
|
|
34
|
-
message = (
|
|
35
|
-
error.trim().length > 0
|
|
36
|
-
? error
|
|
37
|
-
: 'An unknown error occurred.'
|
|
38
|
-
);
|
|
39
|
-
} else {
|
|
40
|
-
message = 'An unknown error occurred.';
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Get the error code
|
|
44
|
-
const code = (error.code || ReactKitErrorCode.NoCode);
|
|
45
|
-
|
|
46
|
-
// Get the status code
|
|
47
|
-
const status = (error.status || 500);
|
|
48
|
-
|
|
49
|
-
// Respond to user
|
|
50
|
-
res
|
|
51
|
-
// Set the http status code
|
|
52
|
-
.status(status)
|
|
53
|
-
// Send a JSON response
|
|
54
|
-
.json({
|
|
55
|
-
// Error message
|
|
56
|
-
message,
|
|
57
|
-
// Error code
|
|
58
|
-
code,
|
|
59
|
-
// Success = false flag so client can detect server-side errors
|
|
60
|
-
success: false,
|
|
61
|
-
});
|
|
62
|
-
return undefined;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export default handleError;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Send successful API response
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param res express response
|
|
5
|
-
* @param body the body of the response to send to the client
|
|
6
|
-
*/
|
|
7
|
-
const handleSuccess = (res: any, body: any): undefined => {
|
|
8
|
-
// Send a http 200 json response
|
|
9
|
-
res.json({
|
|
10
|
-
// Include the body as a parameter
|
|
11
|
-
body,
|
|
12
|
-
// Success = true flag so client can detect successful responses
|
|
13
|
-
success: true,
|
|
14
|
-
});
|
|
15
|
-
return undefined;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export default handleSuccess;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pad a number's decimal with zeros on the right
|
|
3
|
-
* (e.g. 5.2 becomes 5.20 with 2 digit padding)
|
|
4
|
-
* @author Gabe Abrams
|
|
5
|
-
* @param num the number to pad
|
|
6
|
-
* @param numDigits the minimum number of digits after the decimal
|
|
7
|
-
* @returns padded number
|
|
8
|
-
*/
|
|
9
|
-
const padDecimalZeros = (num: number, numDigits: number): string => {
|
|
10
|
-
// Skip if nothing to do
|
|
11
|
-
if (numDigits < 1) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Convert to string
|
|
16
|
-
let out = String(num);
|
|
17
|
-
|
|
18
|
-
// Add a decimal point if there isn't one
|
|
19
|
-
if (!out.includes('.')) {
|
|
20
|
-
out += '.';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Add zeros
|
|
24
|
-
while (out.split('.')[1].length < numDigits) {
|
|
25
|
-
out = `${out}0`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Return
|
|
29
|
-
return out;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export default padDecimalZeros;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pad a number with zeros on the left (e.g. 5 becomes 05 with 2 digit padding)
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param num the number to pad
|
|
5
|
-
* @param numDigits the minimum number of digits before the decimal
|
|
6
|
-
* @returns padded number
|
|
7
|
-
*/
|
|
8
|
-
const padZerosLeft = (num: number, numDigits: number): string => {
|
|
9
|
-
// Convert to string
|
|
10
|
-
let out = String(num);
|
|
11
|
-
|
|
12
|
-
// Add zeros
|
|
13
|
-
while (out.split('.')[0].length < numDigits) {
|
|
14
|
-
out = `0${out}`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Return
|
|
18
|
-
return out;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default padZerosLeft;
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
// Import caccl libs
|
|
2
|
-
import { getLaunchInfo } from 'caccl-lti';
|
|
3
|
-
|
|
4
|
-
// Import shared types
|
|
5
|
-
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
6
|
-
import ParamType from '../types/ParamType';
|
|
7
|
-
|
|
8
|
-
// Import helpers
|
|
9
|
-
import handleError from './handleError';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Parse express request params and body
|
|
13
|
-
* @author Gabe Abrams
|
|
14
|
-
* @param opts object containing all arguments
|
|
15
|
-
* @param opts.req express request instance
|
|
16
|
-
* @param opts.res express response instance
|
|
17
|
-
* @param opts.params map of parameters that should be parsed out of the request
|
|
18
|
-
* @returns parsed params + user info from session (if it exists) or undefined
|
|
19
|
-
* if an error occurred
|
|
20
|
-
*/
|
|
21
|
-
const parseRequest = (
|
|
22
|
-
opts: {
|
|
23
|
-
req: any,
|
|
24
|
-
res: any,
|
|
25
|
-
params: {
|
|
26
|
-
[k in string]: ParamType
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
): ({ [k in string]: any } | undefined) => {
|
|
30
|
-
// Output
|
|
31
|
-
|
|
32
|
-
const output: { [k in string]: any } = {};
|
|
33
|
-
|
|
34
|
-
/*----------------------------------------*/
|
|
35
|
-
/* Parse Params */
|
|
36
|
-
/*----------------------------------------*/
|
|
37
|
-
|
|
38
|
-
// Process items one by one
|
|
39
|
-
const paramList = Object.entries(opts.params);
|
|
40
|
-
for (let i = 0; i < paramList.length; i++) {
|
|
41
|
-
const [name, type] = paramList[i];
|
|
42
|
-
|
|
43
|
-
// Find the value as a string
|
|
44
|
-
const value = (
|
|
45
|
-
opts.req.params[name]
|
|
46
|
-
|| opts.req.query[name]
|
|
47
|
-
|| opts.req.body[name]
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
// Parse
|
|
51
|
-
if (type === ParamType.Boolean || type === ParamType.BooleanOptional) {
|
|
52
|
-
// Boolean
|
|
53
|
-
|
|
54
|
-
// Handle case where value doesn't exist
|
|
55
|
-
if (value === undefined) {
|
|
56
|
-
if (type === ParamType.BooleanOptional) {
|
|
57
|
-
output[name] = undefined;
|
|
58
|
-
} else {
|
|
59
|
-
return handleError(
|
|
60
|
-
opts.res,
|
|
61
|
-
{
|
|
62
|
-
message: `Parameter ${name} is required, but it was not included.`,
|
|
63
|
-
code: ReactKitErrorCode.MissingParameter,
|
|
64
|
-
status: 422,
|
|
65
|
-
},
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
} else {
|
|
69
|
-
// Value exists
|
|
70
|
-
|
|
71
|
-
// Simplify value
|
|
72
|
-
const simpleVal = (
|
|
73
|
-
String(value)
|
|
74
|
-
.trim()
|
|
75
|
-
.toLowerCase()
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
// Parse
|
|
79
|
-
output[name] = (
|
|
80
|
-
[
|
|
81
|
-
'true',
|
|
82
|
-
'yes',
|
|
83
|
-
'y',
|
|
84
|
-
'1',
|
|
85
|
-
't',
|
|
86
|
-
].indexOf(simpleVal) >= 0
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
} else if (type === ParamType.Float || type === ParamType.FloatOptional) {
|
|
90
|
-
// Float
|
|
91
|
-
|
|
92
|
-
// Handle case where value doesn't exist
|
|
93
|
-
if (value === undefined) {
|
|
94
|
-
if (type === ParamType.FloatOptional) {
|
|
95
|
-
output[name] = undefined;
|
|
96
|
-
} else {
|
|
97
|
-
return handleError(
|
|
98
|
-
opts.res,
|
|
99
|
-
{
|
|
100
|
-
message: `Parameter ${name} is required, but it was not included.`,
|
|
101
|
-
code: ReactKitErrorCode.MissingParameter,
|
|
102
|
-
status: 422,
|
|
103
|
-
},
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
} else if (!Number.isNaN(Number.parseFloat(String(value)))) {
|
|
107
|
-
// Value is a number
|
|
108
|
-
output[name] = Number.parseFloat(String(value));
|
|
109
|
-
} else {
|
|
110
|
-
// Issue!
|
|
111
|
-
return handleError(
|
|
112
|
-
opts.res,
|
|
113
|
-
{
|
|
114
|
-
message: `Request data was malformed: ${name} was not a valid float.`,
|
|
115
|
-
code: ReactKitErrorCode.InvalidParameter,
|
|
116
|
-
status: 422,
|
|
117
|
-
},
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
} else if (type === ParamType.Int || type === ParamType.IntOptional) {
|
|
121
|
-
// Int
|
|
122
|
-
|
|
123
|
-
// Handle case where value doesn't exist
|
|
124
|
-
if (value === undefined) {
|
|
125
|
-
if (type === ParamType.IntOptional) {
|
|
126
|
-
output[name] = undefined;
|
|
127
|
-
} else {
|
|
128
|
-
return handleError(
|
|
129
|
-
opts.res,
|
|
130
|
-
{
|
|
131
|
-
message: `Parameter ${name} is required, but it was not included.`,
|
|
132
|
-
code: ReactKitErrorCode.MissingParameter,
|
|
133
|
-
status: 422,
|
|
134
|
-
},
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
} else if (!Number.isNaN(Number.parseInt(String(value), 10))) {
|
|
138
|
-
// Value is a number
|
|
139
|
-
output[name] = Number.parseInt(String(value), 10);
|
|
140
|
-
} else {
|
|
141
|
-
// Issue!
|
|
142
|
-
return handleError(
|
|
143
|
-
opts.res,
|
|
144
|
-
{
|
|
145
|
-
message: `Request data was malformed: ${name} was not a valid int.`,
|
|
146
|
-
code: ReactKitErrorCode.InvalidParameter,
|
|
147
|
-
status: 422,
|
|
148
|
-
},
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
} else if (type === ParamType.JSON || type === ParamType.JSONOptional) {
|
|
152
|
-
// Stringified JSON
|
|
153
|
-
|
|
154
|
-
// Handle case where value doesn't exist
|
|
155
|
-
if (value === undefined) {
|
|
156
|
-
if (type === ParamType.JSONOptional) {
|
|
157
|
-
output[name] = undefined;
|
|
158
|
-
} else {
|
|
159
|
-
return handleError(
|
|
160
|
-
opts.res,
|
|
161
|
-
{
|
|
162
|
-
message: `Parameter ${name} is required, but it was not included.`,
|
|
163
|
-
code: ReactKitErrorCode.MissingParameter,
|
|
164
|
-
status: 422,
|
|
165
|
-
},
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
// Value exists
|
|
170
|
-
|
|
171
|
-
// Parse
|
|
172
|
-
try {
|
|
173
|
-
output[name] = JSON.parse(String(value));
|
|
174
|
-
} catch (err) {
|
|
175
|
-
return handleError(
|
|
176
|
-
opts.res,
|
|
177
|
-
{
|
|
178
|
-
message: `Request data was malformed: ${name} was not a valid JSON payload.`,
|
|
179
|
-
code: ReactKitErrorCode.InvalidParameter,
|
|
180
|
-
status: 422,
|
|
181
|
-
},
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
} else if (type === ParamType.String || type === ParamType.StringOptional) {
|
|
186
|
-
// String
|
|
187
|
-
|
|
188
|
-
// Handle case where value doesn't exist
|
|
189
|
-
if (value === undefined) {
|
|
190
|
-
if (type === ParamType.StringOptional) {
|
|
191
|
-
output[name] = undefined;
|
|
192
|
-
} else {
|
|
193
|
-
return handleError(
|
|
194
|
-
opts.res,
|
|
195
|
-
{
|
|
196
|
-
message: `Parameter ${name} is required, but it was not included.`,
|
|
197
|
-
code: ReactKitErrorCode.MissingParameter,
|
|
198
|
-
status: 422,
|
|
199
|
-
},
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
} else {
|
|
203
|
-
// Value exists
|
|
204
|
-
|
|
205
|
-
// Leave as is
|
|
206
|
-
output[name] = value;
|
|
207
|
-
}
|
|
208
|
-
} else {
|
|
209
|
-
// No valid data type
|
|
210
|
-
return handleError(
|
|
211
|
-
opts.res,
|
|
212
|
-
{
|
|
213
|
-
message: `An internal error occurred: we could not determine the type of ${name}.`,
|
|
214
|
-
code: ReactKitErrorCode.InvalidParameter,
|
|
215
|
-
status: 422,
|
|
216
|
-
},
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/*----------------------------------------*/
|
|
222
|
-
/* Launch Info */
|
|
223
|
-
/*----------------------------------------*/
|
|
224
|
-
|
|
225
|
-
// Get launch info
|
|
226
|
-
const { launched, launchInfo } = getLaunchInfo(opts.req);
|
|
227
|
-
if (!launched || !launchInfo) {
|
|
228
|
-
return handleError(
|
|
229
|
-
opts.res,
|
|
230
|
-
{
|
|
231
|
-
message: 'Your session has expired. Please refresh the page and try again.',
|
|
232
|
-
code: ReactKitErrorCode.SessionExpired,
|
|
233
|
-
status: 440,
|
|
234
|
-
},
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Error if user info cannot be found
|
|
239
|
-
if (
|
|
240
|
-
!launchInfo.userId
|
|
241
|
-
|| !launchInfo.userFirstName
|
|
242
|
-
|| !launchInfo.userLastName
|
|
243
|
-
|| (
|
|
244
|
-
launchInfo.notInCourse
|
|
245
|
-
&& !launchInfo.isAdmin
|
|
246
|
-
)
|
|
247
|
-
|| (
|
|
248
|
-
!launchInfo.isTTM
|
|
249
|
-
&& !launchInfo.isLearner
|
|
250
|
-
&& !launchInfo.isAdmin
|
|
251
|
-
)
|
|
252
|
-
) {
|
|
253
|
-
return handleError(
|
|
254
|
-
opts.res,
|
|
255
|
-
{
|
|
256
|
-
message: 'Your session was invalid. Please refresh the page and try again.',
|
|
257
|
-
code: ReactKitErrorCode.SessionExpired,
|
|
258
|
-
status: 440,
|
|
259
|
-
},
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Add launch info to output
|
|
264
|
-
output.userId = launchInfo.userId;
|
|
265
|
-
output.userFirstName = launchInfo.userFirstName;
|
|
266
|
-
output.userLastName = launchInfo.userLastName;
|
|
267
|
-
output.isLearner = !!launchInfo.isLearner;
|
|
268
|
-
output.isTTM = !!launchInfo.isTTM;
|
|
269
|
-
output.isAdmin = !!launchInfo.isAdmin;
|
|
270
|
-
output.isWatchingInPrivate = !!(opts.req.session.isWatchingInPrivate);
|
|
271
|
-
|
|
272
|
-
/*----------------------------------------*/
|
|
273
|
-
/* Require Course Consistency */
|
|
274
|
-
/*----------------------------------------*/
|
|
275
|
-
|
|
276
|
-
// Make sure the user actually launched from the appropriate course
|
|
277
|
-
if (
|
|
278
|
-
output.courseId
|
|
279
|
-
&& launchInfo.courseId
|
|
280
|
-
&& output.courseId !== launchInfo.courseId
|
|
281
|
-
&& !output.isTTM
|
|
282
|
-
&& !output.isAdmin
|
|
283
|
-
) {
|
|
284
|
-
// Course of interest is not the launch course
|
|
285
|
-
return handleError(
|
|
286
|
-
opts.res,
|
|
287
|
-
{
|
|
288
|
-
message: 'You switched sessions by opening Immersive Classroom in another tab. Please refresh the page and try again.',
|
|
289
|
-
code: ReactKitErrorCode.WrongCourse,
|
|
290
|
-
status: 401,
|
|
291
|
-
},
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Return processed params
|
|
296
|
-
return output;
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
export default parseRequest;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round a number to a certain number of decimals
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param num the number to round
|
|
5
|
-
* @param numDecimals the number of decimals to round to
|
|
6
|
-
* @returns rounded number
|
|
7
|
-
*/
|
|
8
|
-
const roundToNumDecimals = (num: number, numDecimals: number): number => {
|
|
9
|
-
const rounder = 10 ** numDecimals;
|
|
10
|
-
return (Math.round(num * rounder) / rounder);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default roundToNumDecimals;
|
package/lib/helpers/sum.tsx
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sum the numbers in an array
|
|
3
|
-
* @author Gabe Abrams
|
|
4
|
-
* @param nums the numbers to sum
|
|
5
|
-
* @returns the sum of the numbers
|
|
6
|
-
*/
|
|
7
|
-
const sum = (nums: number[]): number => {
|
|
8
|
-
return nums.reduce(
|
|
9
|
-
(a: number, b: number) => {
|
|
10
|
-
return (a + b);
|
|
11
|
-
},
|
|
12
|
-
0,
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export default sum;
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
// Initialize caccl
|
|
2
|
-
import { sendRequest } from 'caccl/client';
|
|
3
|
-
|
|
4
|
-
// Import custom error
|
|
5
|
-
import ErrorWithCode from '../errors/ErrorWithCode';
|
|
6
|
-
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
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 sendRequest({
|
|
49
|
-
path: opts.path,
|
|
50
|
-
method: opts.method,
|
|
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/lib/helpers/waitMs.tsx
DELETED
package/lib/types/ParamType.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 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;
|