@selfcommunity/react-core 0.6.4-alpha.9 → 0.6.4-courses.101

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 (59) hide show
  1. package/lib/cjs/components/provider/SCRoutingProvider/index.js +1 -0
  2. package/lib/cjs/components/provider/SCUserProvider/index.js +6 -0
  3. package/lib/cjs/constants/Cache.d.ts +25 -2
  4. package/lib/cjs/constants/Cache.js +34 -4
  5. package/lib/cjs/constants/Notification.d.ts +1 -0
  6. package/lib/cjs/constants/Notification.js +2 -1
  7. package/lib/cjs/constants/Preferences.d.ts +8 -0
  8. package/lib/cjs/constants/Preferences.js +18 -2
  9. package/lib/cjs/constants/Routes.d.ts +8 -0
  10. package/lib/cjs/constants/Routes.js +17 -1
  11. package/lib/cjs/hooks/useSCFetchCourse.d.ts +23 -0
  12. package/lib/cjs/hooks/useSCFetchCourse.js +81 -0
  13. package/lib/cjs/hooks/useSCFetchCourses.d.ts +22 -0
  14. package/lib/cjs/hooks/useSCFetchCourses.js +83 -0
  15. package/lib/cjs/hooks/useSCFetchLesson.d.ts +22 -0
  16. package/lib/cjs/hooks/useSCFetchLesson.js +72 -0
  17. package/lib/cjs/hooks/useSCFetchLessonCommentObject.d.ts +23 -0
  18. package/lib/cjs/hooks/useSCFetchLessonCommentObject.js +72 -0
  19. package/lib/cjs/hooks/useSCFetchLessonCommentObjects.d.ts +48 -0
  20. package/lib/cjs/hooks/useSCFetchLessonCommentObjects.js +302 -0
  21. package/lib/cjs/hooks/useSCFetchLiveStream.d.ts +20 -0
  22. package/lib/cjs/hooks/useSCFetchLiveStream.js +76 -0
  23. package/lib/cjs/hooks/useSCJoinedCoursesManager.d.ts +38 -0
  24. package/lib/cjs/hooks/useSCJoinedCoursesManager.js +277 -0
  25. package/lib/cjs/index.d.ts +8 -2
  26. package/lib/cjs/index.js +14 -2
  27. package/lib/cjs/types/context.d.ts +36 -1
  28. package/lib/cjs/types/index.d.ts +2 -2
  29. package/lib/esm/components/provider/SCRoutingProvider/index.js +2 -1
  30. package/lib/esm/components/provider/SCUserProvider/index.js +6 -0
  31. package/lib/esm/constants/Cache.d.ts +25 -2
  32. package/lib/esm/constants/Cache.js +25 -2
  33. package/lib/esm/constants/Notification.d.ts +1 -0
  34. package/lib/esm/constants/Notification.js +2 -1
  35. package/lib/esm/constants/Preferences.d.ts +8 -0
  36. package/lib/esm/constants/Preferences.js +16 -0
  37. package/lib/esm/constants/Routes.d.ts +8 -0
  38. package/lib/esm/constants/Routes.js +16 -0
  39. package/lib/esm/hooks/useSCFetchCourse.d.ts +23 -0
  40. package/lib/esm/hooks/useSCFetchCourse.js +78 -0
  41. package/lib/esm/hooks/useSCFetchCourses.d.ts +22 -0
  42. package/lib/esm/hooks/useSCFetchCourses.js +81 -0
  43. package/lib/esm/hooks/useSCFetchLesson.d.ts +22 -0
  44. package/lib/esm/hooks/useSCFetchLesson.js +69 -0
  45. package/lib/esm/hooks/useSCFetchLessonCommentObject.d.ts +23 -0
  46. package/lib/esm/hooks/useSCFetchLessonCommentObject.js +69 -0
  47. package/lib/esm/hooks/useSCFetchLessonCommentObjects.d.ts +48 -0
  48. package/lib/esm/hooks/useSCFetchLessonCommentObjects.js +297 -0
  49. package/lib/esm/hooks/useSCFetchLiveStream.d.ts +20 -0
  50. package/lib/esm/hooks/useSCFetchLiveStream.js +73 -0
  51. package/lib/esm/hooks/useSCJoinedCoursesManager.d.ts +38 -0
  52. package/lib/esm/hooks/useSCJoinedCoursesManager.js +273 -0
  53. package/lib/esm/index.d.ts +8 -2
  54. package/lib/esm/index.js +7 -1
  55. package/lib/esm/types/context.d.ts +36 -1
  56. package/lib/esm/types/index.d.ts +2 -2
  57. package/lib/umd/react-core.js +1 -1
  58. package/lib/umd/react-core.js.LICENSE.txt +2 -0
  59. package/package.json +132 -127
@@ -117,6 +117,7 @@ export declare const CONFIGURATIONS_URL_TEMPLATE_USER_PROFILE_SETTINGS: string;
117
117
  export declare const CONFIGURATIONS_URL_TEMPLATE_NOTIFICATIONS: string;
118
118
  export declare const CONFIGURATIONS_URL_TEMPLATE_USER_PRIVATE_MESSAGES: string;
119
119
  export declare const CONFIGURATIONS_URL_TEMPLATE_INCUBATOR: string;
120
+ export declare const CONFIGURATIONS_URL_TEMPLATE_LIVE_STREAM: string;
120
121
  export declare const CONFIGURATIONS_APP_URL: string;
121
122
  export declare const CONFIGURATIONS_USER_METADATA_DEFINITIONS: string;
122
123
  export declare const CONFIGURATIONS_TAG_MANAGER_CONTAINER_ID: string;
@@ -134,8 +135,15 @@ export declare const CONFIGURATIONS_EVENTS_ENABLED: string;
134
135
  export declare const CONFIGURATIONS_EVENTS_ONLY_STAFF_ENABLED: string;
135
136
  export declare const CONFIGURATIONS_EVENTS_VISIBILITY_ENABLED: string;
136
137
  export declare const CONFIGURATIONS_EVENTS_PRIVATE_ENABLED: string;
138
+ export declare const CONFIGURATIONS_COURSES_ENABLED: string;
139
+ export declare const CONFIGURATIONS_COURSES_ONLY_STAFF_ENABLED: string;
140
+ export declare const CONFIGURATIONS_COURSES_VISIBILITY_ENABLED: string;
141
+ export declare const CONFIGURATIONS_COURSES_PRIVATE_ENABLED: string;
137
142
  export declare const CONFIGURATIONS_ONBOARDING_ENABLED: string;
138
143
  export declare const CONFIGURATIONS_ONBOARDING_HIDDEN: string;
144
+ export declare const CONFIGURATIONS_LIVE_STREAM_ENABLED: string;
145
+ export declare const CONFIGURATIONS_LIVE_STREAM_ONLY_STAFF_ENABLED: string;
146
+ export declare const CONFIGURATIONS_SUBSCRIPTION_TIER: string;
139
147
  /**
140
148
  * PROVIDERS
141
149
  */
@@ -118,6 +118,7 @@ export const CONFIGURATIONS_URL_TEMPLATE_USER_PROFILE_SETTINGS = `${SCPreference
118
118
  export const CONFIGURATIONS_URL_TEMPLATE_NOTIFICATIONS = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.URL_TEMPLATE_NOTIFICATIONS}`;
119
119
  export const CONFIGURATIONS_URL_TEMPLATE_USER_PRIVATE_MESSAGES = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.URL_TEMPLATE_USER_PRIVATE_MESSAGES}`;
120
120
  export const CONFIGURATIONS_URL_TEMPLATE_INCUBATOR = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.URL_TEMPLATE_INCUBATOR}`;
121
+ export const CONFIGURATIONS_URL_TEMPLATE_LIVE_STREAM = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.URL_TEMPLATE_LIVE_STREAM}`;
121
122
  export const CONFIGURATIONS_APP_URL = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.APP_URL}`;
122
123
  export const CONFIGURATIONS_USER_METADATA_DEFINITIONS = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.USER_METADATA_DEFINITIONS}`;
123
124
  export const CONFIGURATIONS_TAG_MANAGER_CONTAINER_ID = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.TAG_MANAGER_CONTAINER_ID}`;
@@ -135,8 +136,15 @@ export const CONFIGURATIONS_EVENTS_ENABLED = `${SCPreferenceSection.CONFIGURATIO
135
136
  export const CONFIGURATIONS_EVENTS_ONLY_STAFF_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.EVENTS_ONLY_STAFF_ENABLED}`;
136
137
  export const CONFIGURATIONS_EVENTS_VISIBILITY_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.EVENTS_VISIBILITY_ENABLED}`;
137
138
  export const CONFIGURATIONS_EVENTS_PRIVATE_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.EVENTS_PRIVATE_ENABLED}`;
139
+ export const CONFIGURATIONS_COURSES_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.COURSES_ENABLED}`;
140
+ export const CONFIGURATIONS_COURSES_ONLY_STAFF_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.COURSES_ONLY_STAFF_ENABLED}`;
141
+ export const CONFIGURATIONS_COURSES_VISIBILITY_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.COURSES_VISIBILITY_ENABLED}`;
142
+ export const CONFIGURATIONS_COURSES_PRIVATE_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.COURSES_PRIVATE_ENABLED}`;
138
143
  export const CONFIGURATIONS_ONBOARDING_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.ONBOARDING_ENABLED}`;
139
144
  export const CONFIGURATIONS_ONBOARDING_HIDDEN = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.ONBOARDING_HIDDEN}`;
145
+ export const CONFIGURATIONS_LIVE_STREAM_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.LIVE_STREAM_ENABLED}`;
146
+ export const CONFIGURATIONS_LIVE_STREAM_ONLY_STAFF_ENABLED = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.LIVE_STREAM_ONLY_STAFF_ENABLED}`;
147
+ export const CONFIGURATIONS_SUBSCRIPTION_TIER = `${SCPreferenceSection.CONFIGURATIONS}.${SCPreferenceName.SUBSCRIPTION_TIER}`;
140
148
  /**
141
149
  * PROVIDERS
142
150
  */
@@ -264,6 +272,7 @@ export const DATA_TYPES = {
264
272
  [CONFIGURATIONS_URL_TEMPLATE_NOTIFICATIONS]: stringType,
265
273
  [CONFIGURATIONS_URL_TEMPLATE_USER_PRIVATE_MESSAGES]: stringType,
266
274
  [CONFIGURATIONS_URL_TEMPLATE_INCUBATOR]: stringType,
275
+ [CONFIGURATIONS_URL_TEMPLATE_LIVE_STREAM]: stringType,
267
276
  [CONFIGURATIONS_APP_URL]: stringType,
268
277
  [CONFIGURATIONS_USER_METADATA_DEFINITIONS]: stringType,
269
278
  [CONFIGURATIONS_TAG_MANAGER_CONTAINER_ID]: stringType,
@@ -281,8 +290,15 @@ export const DATA_TYPES = {
281
290
  [CONFIGURATIONS_EVENTS_ONLY_STAFF_ENABLED]: booleanType,
282
291
  [CONFIGURATIONS_EVENTS_VISIBILITY_ENABLED]: booleanType,
283
292
  [CONFIGURATIONS_EVENTS_PRIVATE_ENABLED]: booleanType,
293
+ [CONFIGURATIONS_COURSES_ENABLED]: booleanType,
294
+ [CONFIGURATIONS_COURSES_ONLY_STAFF_ENABLED]: booleanType,
295
+ [CONFIGURATIONS_COURSES_VISIBILITY_ENABLED]: booleanType,
296
+ [CONFIGURATIONS_COURSES_PRIVATE_ENABLED]: booleanType,
284
297
  [CONFIGURATIONS_ONBOARDING_ENABLED]: booleanType,
285
298
  [CONFIGURATIONS_ONBOARDING_HIDDEN]: booleanType,
299
+ [CONFIGURATIONS_LIVE_STREAM_ENABLED]: booleanType,
300
+ [CONFIGURATIONS_LIVE_STREAM_ONLY_STAFF_ENABLED]: booleanType,
301
+ [CONFIGURATIONS_SUBSCRIPTION_TIER]: stringType,
286
302
  [PROVIDERS_APP_URL_ON_APP_STORE]: stringType,
287
303
  [PROVIDERS_APP_URL_ON_GOOGLE_PLAY]: stringType,
288
304
  [PROVIDERS_FACEBOOK_SIGNIN_ENABLED]: stringType,
@@ -38,9 +38,17 @@ export declare const GROUPS_ROUTE_NAME = "groups";
38
38
  export declare const GROUPS_SUBSCRIBED_ROUTE_NAME = "groups_subscribed";
39
39
  export declare const EVENT_ROUTE_NAME = "event";
40
40
  export declare const EVENTS_ROUTE_NAME = "events";
41
+ export declare const COURSE_ROUTE_NAME = "course";
42
+ export declare const COURSE_EDIT_ROUTE_NAME = "course_edit";
43
+ export declare const COURSE_DASHBOARD_ROUTE_NAME = "course_dashboard";
44
+ export declare const COURSE_LESSON_ROUTE_NAME = "course_lesson";
45
+ export declare const COURSE_LESSON_COMMENT_ROUTE_NAME = "course_lesson_comment";
46
+ export declare const COURSES_ROUTE_NAME = "courses";
47
+ export declare const COURSES_SUBSCRIBED_ROUTE_NAME = "courses_subscribed";
41
48
  export declare const EVENTS_SUGGESTED_ROUTE_NAME = "events_suggested";
42
49
  export declare const EVENTS_SUBSCRIBED_ROUTE_NAME = "events_subscribed";
43
50
  export declare const EVENTS_HIGHLIGHT_ROUTE_NAME = "events_highlight";
51
+ export declare const LIVESTREAM_ROUTE_NAME = "live";
44
52
  /**
45
53
  * Default Routes
46
54
  * @type {{[p: string]: string, '[POST_ROUTE_NAME]': string, '[INCUBATOR_ROUTE_NAME]': string, '[LOYALTY_ROUTE_NAME]': string, '[USER_NOTIFICATION_ROUTE_NAME]': string, '[USER_PRIVATE_MESSAGES_ROUTE_NAME]': string, '[COMMENT_ROUTE_NAME]': string, '[DISCUSSION_ROUTE_NAME]': string, '[CATEGORIES_ROUTE_NAME]': string, '[USER_PROFILE_ROUTE_NAME]': string, '[CATEGORY_ROUTE_NAME]': string, '[USER_PROFILE_SETTINGS_ROUTE_NAME]': string, '[STATUS_ROUTE_NAME]': string}}
@@ -38,9 +38,17 @@ export const GROUPS_ROUTE_NAME = 'groups';
38
38
  export const GROUPS_SUBSCRIBED_ROUTE_NAME = 'groups_subscribed';
39
39
  export const EVENT_ROUTE_NAME = 'event';
40
40
  export const EVENTS_ROUTE_NAME = 'events';
41
+ export const COURSE_ROUTE_NAME = 'course';
42
+ export const COURSE_EDIT_ROUTE_NAME = 'course_edit';
43
+ export const COURSE_DASHBOARD_ROUTE_NAME = 'course_dashboard';
44
+ export const COURSE_LESSON_ROUTE_NAME = 'course_lesson';
45
+ export const COURSE_LESSON_COMMENT_ROUTE_NAME = 'course_lesson_comment';
46
+ export const COURSES_ROUTE_NAME = 'courses';
47
+ export const COURSES_SUBSCRIBED_ROUTE_NAME = 'courses_subscribed';
41
48
  export const EVENTS_SUGGESTED_ROUTE_NAME = 'events_suggested';
42
49
  export const EVENTS_SUBSCRIBED_ROUTE_NAME = 'events_subscribed';
43
50
  export const EVENTS_HIGHLIGHT_ROUTE_NAME = 'events_highlight';
51
+ export const LIVESTREAM_ROUTE_NAME = 'live';
44
52
  /**
45
53
  * Default Routes
46
54
  * @type {{[p: string]: string, '[POST_ROUTE_NAME]': string, '[INCUBATOR_ROUTE_NAME]': string, '[LOYALTY_ROUTE_NAME]': string, '[USER_NOTIFICATION_ROUTE_NAME]': string, '[USER_PRIVATE_MESSAGES_ROUTE_NAME]': string, '[COMMENT_ROUTE_NAME]': string, '[DISCUSSION_ROUTE_NAME]': string, '[CATEGORIES_ROUTE_NAME]': string, '[USER_PROFILE_ROUTE_NAME]': string, '[CATEGORY_ROUTE_NAME]': string, '[USER_PROFILE_SETTINGS_ROUTE_NAME]': string, '[STATUS_ROUTE_NAME]': string}}
@@ -84,4 +92,12 @@ export const defaultRoutes = {
84
92
  [EVENT_ROUTE_NAME]: '/event/:id/:slug/',
85
93
  [EVENTS_SUBSCRIBED_ROUTE_NAME]: '/events/subscribed/',
86
94
  [EVENTS_HIGHLIGHT_ROUTE_NAME]: '/events/highlight/',
95
+ [COURSE_ROUTE_NAME]: '/course/:id/:slug/',
96
+ [COURSES_ROUTE_NAME]: '/courses/',
97
+ [COURSE_EDIT_ROUTE_NAME]: '/course/:id/:slug/edit',
98
+ [COURSE_DASHBOARD_ROUTE_NAME]: '/course/:id/:slug/dashboard',
99
+ [COURSE_LESSON_ROUTE_NAME]: '/course/:id/:slug/section/:section_id/lesson/:lesson_id/',
100
+ [COURSE_LESSON_COMMENT_ROUTE_NAME]: '/course/:id/:slug/section/:section_id/lesson/:lesson_id/comment/:comment_id',
101
+ [COURSES_SUBSCRIBED_ROUTE_NAME]: '/courses/subscribed/',
102
+ [LIVESTREAM_ROUTE_NAME]: '/live/:slug/',
87
103
  };
@@ -0,0 +1,23 @@
1
+ import { CourseInfoParams } from '@selfcommunity/api-services';
2
+ import { SCCourseType } from '@selfcommunity/types';
3
+ import { CacheStrategies } from '@selfcommunity/utils';
4
+ /**
5
+ :::info
6
+ This custom hook is used to fetch an course object.
7
+ :::
8
+ * @param object
9
+ * @param object.id
10
+ * @param object.course
11
+ * @param object.cacheStrategy
12
+ * @param object.params
13
+ */
14
+ export default function useSCFetchCourse({ id, course, cacheStrategy, params, }: {
15
+ id?: number | string;
16
+ course?: SCCourseType;
17
+ cacheStrategy?: CacheStrategies;
18
+ params?: CourseInfoParams;
19
+ }): {
20
+ scCourse: SCCourseType;
21
+ setSCCourse: (c: SCCourseType) => void;
22
+ error: string;
23
+ };
@@ -0,0 +1,78 @@
1
+ import { Endpoints, http } from '@selfcommunity/api-services';
2
+ import { CacheStrategies, Logger, LRUCache, objectWithoutProperties } from '@selfcommunity/utils';
3
+ import { useCallback, useEffect, useMemo, useState } from 'react';
4
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
5
+ import { useSCUser } from '../components/provider/SCUserProvider';
6
+ import { getCourseObjectCacheKey } from '../constants/Cache';
7
+ import { SCOPE_SC_CORE } from '../constants/Errors';
8
+ /**
9
+ :::info
10
+ This custom hook is used to fetch an course object.
11
+ :::
12
+ * @param object
13
+ * @param object.id
14
+ * @param object.course
15
+ * @param object.cacheStrategy
16
+ * @param object.params
17
+ */
18
+ export default function useSCFetchCourse({ id = null, course = null, cacheStrategy = CacheStrategies.CACHE_FIRST, params = null, }) {
19
+ const __courseId = useMemo(() => (course === null || course === void 0 ? void 0 : course.id) || id, [course, id]);
20
+ // CONTEXT
21
+ const scUserContext = useSCUser();
22
+ const authUserId = useMemo(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]);
23
+ // CACHE
24
+ const __courseCacheKey = useMemo(() => getCourseObjectCacheKey(__courseId), [__courseId]);
25
+ const __course = useMemo(() => (authUserId ? course : objectWithoutProperties(course, ['join_status'])), [authUserId, course]);
26
+ const [scCourse, setScCourse] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__courseCacheKey, __course) : null);
27
+ const [error, setError] = useState(null);
28
+ const setSCCourse = useCallback((c) => {
29
+ setScCourse(c);
30
+ LRUCache.set(__courseCacheKey, c);
31
+ }, [setScCourse, __courseCacheKey]);
32
+ /**
33
+ * Memoized fetchTag
34
+ */
35
+ const fetchCourse = useMemo(() => (id) => {
36
+ return http
37
+ .request({
38
+ url: Endpoints.GetCourseInfo.url({ id }),
39
+ method: Endpoints.GetCourseInfo.method,
40
+ params: params !== null && params !== void 0 ? params : {},
41
+ })
42
+ .then((res) => {
43
+ if (res.status >= 300) {
44
+ return Promise.reject(res);
45
+ }
46
+ return Promise.resolve(res.data);
47
+ });
48
+ }, []);
49
+ /**
50
+ * If id attempt to get the course by id
51
+ */
52
+ useEffect(() => {
53
+ if (id !== null && id !== undefined && !course) {
54
+ fetchCourse(id)
55
+ .then((e) => {
56
+ setSCCourse(e);
57
+ })
58
+ .catch((err) => {
59
+ LRUCache.delete(__courseCacheKey);
60
+ if (err.status === 403) {
61
+ setError('You do not have permission to perform this action.');
62
+ Logger.error(SCOPE_SC_CORE, 'You do not have permission to perform this action.');
63
+ }
64
+ else {
65
+ setError(`Course with id ${id} not found`);
66
+ Logger.error(SCOPE_SC_CORE, `Course with id ${id} not found`);
67
+ }
68
+ Logger.error(SCOPE_SC_CORE, err.message);
69
+ });
70
+ }
71
+ }, [id, course, authUserId]);
72
+ useDeepCompareEffectNoCheck(() => {
73
+ if (course) {
74
+ setSCCourse(course);
75
+ }
76
+ }, [course, authUserId]);
77
+ return { scCourse, setSCCourse, error };
78
+ }
@@ -0,0 +1,22 @@
1
+ import { SCCourseType } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ :::info
5
+ This custom hook is used to fetch courses.
6
+ @param object.cacheStrategy
7
+
8
+ :::tip Context can be consumed in this way:
9
+
10
+ ```jsx
11
+ const {courses, isLoading} = useSCFetchCourses();
12
+ ```
13
+ :::
14
+ * @param props
15
+ */
16
+ declare const useSCFetchCourses: (props?: {
17
+ cacheStrategy?: CacheStrategies;
18
+ }) => {
19
+ courses: SCCourseType[];
20
+ isLoading: boolean;
21
+ };
22
+ export default useSCFetchCourses;
@@ -0,0 +1,81 @@
1
+ import { __awaiter } from "tslib";
2
+ import { useEffect, useState } from 'react';
3
+ import { SCOPE_SC_CORE } from '../constants/Errors';
4
+ import { Endpoints, http } from '@selfcommunity/api-services';
5
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
6
+ import { getCourseObjectCacheKey, getCoursesObjectCacheKey } from '../constants/Cache';
7
+ const init = { courses: [], isLoading: true };
8
+ // HYDRATE the cache
9
+ const hydrate = (ids) => {
10
+ if (!ids) {
11
+ return null;
12
+ }
13
+ const courses = ids.map((id) => {
14
+ const __courseCacheKey = getCourseObjectCacheKey(id);
15
+ return LRUCache.get(__courseCacheKey);
16
+ });
17
+ if (courses.filter((c) => !c).length > 0) {
18
+ // REVALIDATE CACHE
19
+ return null;
20
+ }
21
+ return courses;
22
+ };
23
+ /**
24
+ :::info
25
+ This custom hook is used to fetch courses.
26
+ @param object.cacheStrategy
27
+
28
+ :::tip Context can be consumed in this way:
29
+
30
+ ```jsx
31
+ const {courses, isLoading} = useSCFetchCourses();
32
+ ```
33
+ :::
34
+ * @param props
35
+ */
36
+ const useSCFetchCourses = (props) => {
37
+ // PROPS
38
+ const { cacheStrategy = CacheStrategies.CACHE_FIRST } = props || {};
39
+ // CACHE
40
+ const __coursesCacheKey = getCoursesObjectCacheKey();
41
+ // STATE
42
+ const courses = cacheStrategy !== CacheStrategies.NETWORK_ONLY ? hydrate(LRUCache.get(__coursesCacheKey, null)) : null;
43
+ const [data, setData] = useState(courses !== null ? { courses, isLoading: false } : init);
44
+ /**
45
+ * Fetch courses
46
+ */
47
+ const fetchCourses = (next = Endpoints.GetJoinedCourses.url()) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const response = yield http.request({
49
+ url: next,
50
+ method: Endpoints.GetJoinedCourses.method,
51
+ });
52
+ const data = response.data;
53
+ if (data.next) {
54
+ return data.results.concat(yield fetchCourses(data.next));
55
+ }
56
+ return data.results;
57
+ });
58
+ /**
59
+ * Get courses
60
+ */
61
+ useEffect(() => {
62
+ if (cacheStrategy === CacheStrategies.CACHE_FIRST && courses) {
63
+ return;
64
+ }
65
+ fetchCourses()
66
+ .then((data) => {
67
+ setData({ courses: data, isLoading: false });
68
+ LRUCache.set(__coursesCacheKey, data.map((course) => {
69
+ const __courseCacheKey = getCourseObjectCacheKey(course.id);
70
+ LRUCache.set(__courseCacheKey, course);
71
+ return course.id;
72
+ }));
73
+ })
74
+ .catch((error) => {
75
+ console.log(error);
76
+ Logger.error(SCOPE_SC_CORE, 'Unable to retrieve courses');
77
+ });
78
+ }, []);
79
+ return data;
80
+ };
81
+ export default useSCFetchCourses;
@@ -0,0 +1,22 @@
1
+ import { SCCourseLessonType } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ :::info
5
+ This custom hook is used to fetch a lesson object.
6
+ :::
7
+ * @param object
8
+ * @param object.id
9
+ * @param object.lesson
10
+ * @param object.cacheStrategy
11
+ */
12
+ export default function useSCFetchLesson({ id, lesson, courseId, sectionId, cacheStrategy, }: {
13
+ id?: number | string;
14
+ lesson?: SCCourseLessonType;
15
+ courseId: number | string;
16
+ sectionId: number | string;
17
+ cacheStrategy?: CacheStrategies;
18
+ }): {
19
+ scLesson: SCCourseLessonType;
20
+ setSCLesson: (lesson: SCCourseLessonType) => void;
21
+ error: string;
22
+ };
@@ -0,0 +1,69 @@
1
+ import { Endpoints, http } from '@selfcommunity/api-services';
2
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
3
+ import { useEffect, useMemo, useState } from 'react';
4
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
5
+ import { useSCUser } from '../components/provider/SCUserProvider';
6
+ import { getLessonObjectCacheKey } from '../constants/Cache';
7
+ import { SCOPE_SC_CORE } from '../constants/Errors';
8
+ /**
9
+ :::info
10
+ This custom hook is used to fetch a lesson object.
11
+ :::
12
+ * @param object
13
+ * @param object.id
14
+ * @param object.lesson
15
+ * @param object.cacheStrategy
16
+ */
17
+ export default function useSCFetchLesson({ id = null, lesson = null, courseId = null, sectionId = null, cacheStrategy = CacheStrategies.CACHE_FIRST, }) {
18
+ const __lessonId = useMemo(() => (lesson === null || lesson === void 0 ? void 0 : lesson.id) || id, [lesson, id]);
19
+ // CONTEXT
20
+ const scUserContext = useSCUser();
21
+ const authUserId = useMemo(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]);
22
+ // CACHE
23
+ const __lessonCacheKey = useMemo(() => getLessonObjectCacheKey(__lessonId), [__lessonId]);
24
+ const [scLesson, setScLesson] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__lessonCacheKey, lesson) : null);
25
+ const [error, setError] = useState(null);
26
+ const setSCLesson = (lesson) => {
27
+ setScLesson(lesson);
28
+ LRUCache.set(__lessonCacheKey, lesson);
29
+ };
30
+ /**
31
+ * Memoized fetchTag
32
+ */
33
+ const fetchLesson = useMemo(() => (id) => {
34
+ return http
35
+ .request({
36
+ url: Endpoints.GetCourseLesson.url({ id: courseId, section_id: sectionId, lesson_id: id }),
37
+ method: Endpoints.GetCourseLesson.method,
38
+ })
39
+ .then((res) => {
40
+ if (res.status >= 300) {
41
+ return Promise.reject(res);
42
+ }
43
+ return Promise.resolve(res.data);
44
+ });
45
+ }, []);
46
+ /**
47
+ * If id attempt to get the lesson by id
48
+ */
49
+ useEffect(() => {
50
+ if (id !== null && id !== undefined && courseId !== null && courseId !== undefined && sectionId) {
51
+ fetchLesson(id)
52
+ .then((e) => {
53
+ setSCLesson(e);
54
+ })
55
+ .catch((err) => {
56
+ LRUCache.delete(__lessonCacheKey);
57
+ setError(`Lesson with id ${id} not found`);
58
+ Logger.error(SCOPE_SC_CORE, `Lesson with id ${id} not found`);
59
+ Logger.error(SCOPE_SC_CORE, err.message);
60
+ });
61
+ }
62
+ }, [id, courseId, authUserId, sectionId]);
63
+ useDeepCompareEffectNoCheck(() => {
64
+ if (lesson) {
65
+ setSCLesson(lesson);
66
+ }
67
+ }, [lesson, authUserId]);
68
+ return { scLesson, setSCLesson, error };
69
+ }
@@ -0,0 +1,23 @@
1
+ /// <reference types="react" />
2
+ import { SCCourseCommentType, SCCourseLessonType } from '@selfcommunity/types';
3
+ import { CacheStrategies } from '@selfcommunity/utils';
4
+ /**
5
+ :::info
6
+ This custom hooks is used to fetch a course lesson comment.
7
+ :::
8
+ * @param object
9
+ * @param object.id
10
+ * @param object.commentObject
11
+ * @param object.lesson
12
+ * @param object.cacheStrategy
13
+ */
14
+ export default function useSCFetchLessonCommentObject({ id, commentObject, lesson, cacheStrategy, }: {
15
+ id?: number;
16
+ commentObject?: SCCourseCommentType;
17
+ lesson: SCCourseLessonType;
18
+ cacheStrategy?: CacheStrategies;
19
+ }): {
20
+ obj: SCCourseCommentType;
21
+ setObj: import("react").Dispatch<import("react").SetStateAction<SCCourseCommentType>>;
22
+ error: string;
23
+ };
@@ -0,0 +1,69 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import { SCOPE_SC_CORE } from '../constants/Errors';
3
+ import { http, Endpoints } from '@selfcommunity/api-services';
4
+ import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
5
+ import { getLessonCommentCacheKey } from '../constants/Cache';
6
+ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
7
+ /**
8
+ :::info
9
+ This custom hooks is used to fetch a course lesson comment.
10
+ :::
11
+ * @param object
12
+ * @param object.id
13
+ * @param object.commentObject
14
+ * @param object.lesson
15
+ * @param object.cacheStrategy
16
+ */
17
+ export default function useSCFetchLessonCommentObject({ id = null, commentObject = null, lesson = null, cacheStrategy = CacheStrategies.CACHE_FIRST, }) {
18
+ const __commentObjectId = commentObject ? commentObject.id : id;
19
+ // CACHE
20
+ const __commentObjectCacheKey = getLessonCommentCacheKey(__commentObjectId);
21
+ const [obj, setObj] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__commentObjectCacheKey, commentObject) : commentObject);
22
+ const [error, setError] = useState(null);
23
+ /**
24
+ * Memoized fetchLessonCommentObject
25
+ */
26
+ const fetchLessonCommentObject = useMemo(() => () => {
27
+ return http
28
+ .request({
29
+ url: Endpoints.GetCourseLessonComment.url({
30
+ id: lesson.course_id,
31
+ section_id: lesson.section_id,
32
+ lesson_id: lesson.id,
33
+ comment_id: __commentObjectId,
34
+ }),
35
+ method: Endpoints.GetCourseLessonComment.method,
36
+ })
37
+ .then((res) => {
38
+ if (res.status >= 300) {
39
+ return Promise.reject(res);
40
+ }
41
+ return Promise.resolve(res.data);
42
+ });
43
+ }, [__commentObjectId, lesson]);
44
+ /**
45
+ * If course commentObject resolve it
46
+ */
47
+ useEffect(() => {
48
+ if (__commentObjectId && lesson && (!obj || cacheStrategy === CacheStrategies.STALE_WHILE_REVALIDATE)) {
49
+ fetchLessonCommentObject()
50
+ .then((obj) => {
51
+ setObj(obj);
52
+ LRUCache.set(__commentObjectCacheKey, obj);
53
+ })
54
+ .catch((err) => {
55
+ LRUCache.delete(__commentObjectCacheKey);
56
+ setError(`Course Lesson comment Object with id ${id} not found`);
57
+ Logger.error(SCOPE_SC_CORE, `Course Lesson comment Object with id ${id} not found`);
58
+ Logger.error(SCOPE_SC_CORE, err.message);
59
+ });
60
+ }
61
+ }, [__commentObjectId, lesson]);
62
+ useDeepCompareEffectNoCheck(() => {
63
+ if (commentObject) {
64
+ setObj(commentObject);
65
+ LRUCache.set(__commentObjectCacheKey, obj);
66
+ }
67
+ }, [commentObject]);
68
+ return { obj, setObj, error };
69
+ }
@@ -0,0 +1,48 @@
1
+ import { SCCommentsOrderBy, SCCourseCommentType, SCCourseLessonType } from '@selfcommunity/types';
2
+ import { CacheStrategies } from '@selfcommunity/utils';
3
+ /**
4
+ * Interface SCLessonCommentsObjectType
5
+ */
6
+ export interface SCLessonCommentsObjectType {
7
+ componentLoaded: boolean;
8
+ comments: SCCourseCommentType[];
9
+ total: number;
10
+ next: string;
11
+ previous: string;
12
+ isLoadingNext: boolean;
13
+ isLoadingPrevious: boolean;
14
+ page: number;
15
+ reload: boolean;
16
+ }
17
+ /**
18
+ * @hidden
19
+ * We have complex state logic that involves multiple sub-values,
20
+ * so useReducer is preferable to useState.
21
+ * Define all possible auth action types label
22
+ * Use this to export actions and dispatch an action
23
+ */
24
+ export declare const commentsObjectActionTypes: {
25
+ LOADING_NEXT: string;
26
+ LOADING_PREVIOUS: string;
27
+ DATA_NEXT_LOADED: string;
28
+ DATA_PREVIOUS_LOADED: string;
29
+ DATA_RELOAD: string;
30
+ DATA_RELOADED: string;
31
+ DATA_REVALIDATE: string;
32
+ };
33
+ /**
34
+ :::info
35
+ This custom hooks is used to fetch paginated comments for a specific course lesson.
36
+ :::
37
+ * @param props
38
+ */
39
+ export default function useSCFetchLessonCommentObjects(props: {
40
+ id?: number;
41
+ lessonObject?: SCCourseLessonType;
42
+ offset?: number;
43
+ pageSize?: number;
44
+ orderBy?: SCCommentsOrderBy;
45
+ parent?: number;
46
+ onChangePage?: (page: any) => any;
47
+ cacheStrategy?: CacheStrategies;
48
+ }): any;