-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathutils.tsx
More file actions
355 lines (302 loc) · 10.9 KB
/
utils.tsx
File metadata and controls
355 lines (302 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React, { useState, useContext, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useMediaQuery } from 'react-responsive';
import * as Yup from 'yup';
import { snakeCase } from 'lodash/string';
import moment from 'moment';
import { getConfig, getPath } from '@edx/frontend-platform';
import type { Dispatch, AnyAction } from 'redux';
import type { TypeOfShape } from 'yup/lib/object';
import { RequestStatus } from './data/constants';
import { getCourseAppSettingValue, getLoadingStatus } from './pages-and-resources/data/selectors';
import { fetchCourseAppSettings, updateCourseAppSetting } from './pages-and-resources/data/thunks';
import { PagesAndResourcesContext } from './pages-and-resources/PagesAndResourcesProvider';
import {
hasValidDateFormat, hasValidTimeFormat, decodeDateTime, endOfDayTime, startOfDayTime,
} from './pages-and-resources/discussions/app-config-form/utils';
import { DATE_TIME_FORMAT } from './constants';
export const executeThunk = async (
thunk: (dispatch: any, state?: any) => Promise<any>,
dispatch: Dispatch<AnyAction>,
getState?: any,
) => {
await thunk(dispatch, getState);
await new Promise(setImmediate);
};
export function useIsMobile() {
return useMediaQuery({ query: '(max-width: 767.98px)' });
}
export function useIsDesktop() {
return useMediaQuery({ query: '(min-width: 992px)' });
}
export function convertObjectToSnakeCase(obj: Object, unpacked = false) {
return Object.keys(obj).reduce((snakeCaseObj, key) => {
const snakeCaseKey = snakeCase(key);
const value = unpacked ? obj[key] : { value: obj[key] };
return {
...snakeCaseObj,
[snakeCaseKey]: value,
};
}, {});
}
export function deepConvertingKeysToCamelCase(obj: any[] | Object | null) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => deepConvertingKeysToCamelCase(item));
}
const camelCaseObj = {};
Object.keys(obj).forEach((key) => {
const camelCaseKey = key.replace(/_([a-z])/g, (_, p1) => p1.toUpperCase());
camelCaseObj[camelCaseKey] = deepConvertingKeysToCamelCase(obj[key]);
});
return camelCaseObj;
}
export function deepConvertingKeysToSnakeCase(obj: any[] | Object | null) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => deepConvertingKeysToSnakeCase(item));
}
const snakeCaseObj = {};
Object.entries(obj).forEach(([key, value]) => {
const snakeCaseKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
snakeCaseObj[snakeCaseKey] = key === 'gradeCutoffs' ? value : deepConvertingKeysToSnakeCase(value);
});
return snakeCaseObj;
}
export function transformKeysToCamelCase(obj: { key: any; }) {
return obj.key.replace(/_([a-z])/g, (_: any, letter: string) => letter.toUpperCase());
}
export function parseArrayOrObjectValues(obj: { [s: string]: string; } | ArrayLike<string>) {
const result = {};
Object.entries(obj).forEach(([key, value]) => {
try {
if (!Number.isNaN(Number(value))) {
result[key] = value;
} else {
result[key] = JSON.parse(value);
}
} catch {
result[key] = value;
}
});
return result;
}
/**
* Create a correct inner path depend on config PUBLIC_PATH.
*/
export const createCorrectInternalRoute = (checkPath: string): string => {
let basePath = getPath(getConfig().PUBLIC_PATH);
if (basePath.endsWith('/')) {
basePath = basePath.slice(0, -1);
}
if (!checkPath?.startsWith(basePath)) {
return `${basePath}${checkPath}`;
}
return checkPath;
};
export function getPagePath(courseId: string | undefined, isMfePageEnabled: string, urlParameter: string) {
if (isMfePageEnabled === 'true') {
if (urlParameter === 'tabs') {
return `/course/${courseId}/pages-and-resources`;
}
return `/course/${courseId}/${urlParameter}`;
}
return `${getConfig().STUDIO_BASE_URL}/${urlParameter}/${courseId}`;
}
export function useAppSetting(settingName: string) {
const dispatch = useDispatch();
const { courseId } = useContext(PagesAndResourcesContext);
const settingValue = useSelector(getCourseAppSettingValue(settingName));
const loadingStatus = useSelector(getLoadingStatus);
useEffect(() => {
if ([RequestStatus.DENIED, RequestStatus.FAILED].includes(loadingStatus)) {
return;
}
if (settingValue === undefined || settingValue === null) {
dispatch(fetchCourseAppSettings(courseId, [settingName]));
}
}, [courseId]);
const saveSetting = async (value: any) => dispatch(updateCourseAppSetting(courseId, settingName, value));
return [settingValue, saveSetting];
}
export const getLabelById = (options: any[], id: any) => {
const foundOption = options.find((option) => option.id === id);
return foundOption ? foundOption.label : '';
};
interface YupTestContextExtended {
originalValue?: unknown;
}
/**
* Adds additional validation methods to Yup.
*/
export function setupYupExtensions() {
// Add a uniqueProperty method to arrays that allows validating that the specified property path is unique
// across all objects in the array.
// Credit: https://github.com/jquense/yup/issues/345#issuecomment-717400071
Yup.addMethod(Yup.array, 'uniqueProperty', function uniqueProperty(property, message) {
return this.test('unique', '', function testUniqueness(list) {
const errors: Yup.ValidationError[] = [];
list?.forEach((item, index) => {
const propertyValue = item[property];
if (propertyValue && list.filter(entry => entry[property] === propertyValue).length > 1) {
errors.push(
this.createError({
path: `${this.path}[${index}].${property}`,
message,
}),
);
}
});
if (errors.length > 0) {
throw new Yup.ValidationError(errors);
}
return true;
});
});
Yup.addMethod(Yup.object, 'uniqueObjectProperty', function uniqueObjectProperty(propertyName, message) {
return this.test('unique', message, function testUniqueness(discussionTopic) {
if (!discussionTopic || !discussionTopic[propertyName]) {
return true;
}
const isDuplicate = this.parent.filter((topic: TypeOfShape<any>) => topic !== discussionTopic)
.some((
topic: { [x: string]: string; },
) => topic[propertyName]?.toLowerCase() === discussionTopic[propertyName].toLowerCase());
if (isDuplicate) {
throw this.createError({
path: `${this.path}.${propertyName}`,
message,
});
}
return true;
});
});
Yup.addMethod(Yup.string, 'compare', function compare(message, type) {
return this.test('isGreater', message, function isGreater() {
// This function compare 2 dates or 2 times. It return no error if dateInstance/timeInstance is empty
// of if startTime or endTime is not present for time comparison
// or startDate or endDate is not present for date comparison
if (!this.parent
|| (!(this.parent.startTime && this.parent.endTime) && type === 'time')
|| (!(this.parent.startDate && this.parent.endDate) && type === 'date')
) {
return true;
}
const startDateTime = decodeDateTime(this.parent.startDate, startOfDayTime(this.parent.startTime));
const endDateTime = decodeDateTime(this.parent.endDate, endOfDayTime(this.parent.endTime));
let isInvalidStartDateTime: boolean = false;
if (type === 'date') {
isInvalidStartDateTime = startDateTime.isAfter(endDateTime);
} else if (type === 'time') {
isInvalidStartDateTime = startDateTime.isSameOrAfter(endDateTime);
}
if (isInvalidStartDateTime) {
throw this.createError({
path: `${this.path}`,
message,
});
}
return true;
});
});
Yup.addMethod(Yup.string, 'checkFormat', function checkFormat(message, type) {
return this.test('isValidFormat', message, function isValidFormat() {
const { originalValue } = this as Yup.TestContext & YupTestContextExtended;
if (!originalValue) {
return true;
}
let isValid: boolean = false;
if (type === 'date') {
isValid = hasValidDateFormat(originalValue);
} else if (type === 'time') {
isValid = hasValidTimeFormat(originalValue);
}
if (!isValid) {
throw this.createError({
path: `${this.path}`,
message,
});
}
return true;
});
});
}
export const convertToDateFromString = (dateStr: string): Date | undefined => {
/**
* Convert UTC to local time for react-datepicker
* Note: react-datepicker v4 had a bug where it only interacts with local time
* but this bug may no longer be affecting v8+ ?
* @param {string} dateStr - YYYY-MM-DDTHH:MM:SSZ
* @return date in local time
*/
if (!dateStr) {
return undefined;
}
const stripTimeZone = (stringValue: string) => stringValue.substring(0, 19);
return moment(stripTimeZone(String(dateStr))).toDate();
};
export const convertToStringFromDate = (date: moment.MomentInput): string => {
/**
* Convert local time to UTC from react-datepicker
* Note: react-datepicker v4 had a bug where it only interacts with local time
* but this bug may no longer be affecting v8+ ?
* @param {Date} date - date in local time
* @return YYYY-MM-DDTHH:MM:SSZ
*/
if (!date) {
return '';
}
return moment(date).format(DATE_TIME_FORMAT);
};
export const isValidDate = (date: moment.MomentInput) => {
const formattedValue = convertToStringFromDate(date).split('T')[0];
return Boolean(formattedValue.length <= 10);
};
export const getFileSizeToClosestByte = (fileSize: any) => {
let divides = 0;
let size = fileSize;
while (size > 1000 && divides < 4) {
size /= 1000;
divides += 1;
}
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
const fileSizeFixedDecimal = Number.parseFloat(size).toFixed(2);
return `${fileSizeFixedDecimal} ${units[divides]}`;
};
/**
* A generic hook to run callback on next render cycle.
* @param {} callback - Callback function that needs to be run later
*/
export const useRunOnNextRender = (callback: () => void) => {
const [scheduled, setScheduled] = useState(false);
useEffect(() => {
if (!scheduled) {
return;
}
setScheduled(false);
callback();
}, [scheduled]);
return () => setScheduled(true);
};
/**
* Checks if the click event originated from an element with the stop-event-propagation class.
* If so, return without further processing.
*/
export const skipIfUnwantedTarget = (
e: React.MouseEvent,
onClick: (e: React.MouseEvent) => void,
selector?: string,
) => {
const target = e.target as HTMLElement;
if (target && target.closest(selector || '.stop-event-propagation')) {
return;
}
onClick(e);
};
export const BoldText = (chunk: string[]) => <b>{chunk}</b>;
export const Div = (chunk: string[]) => <div>{chunk}</div>;
export const Paragraph = (chunk: string[]) => <p>{chunk}</p>;