@selfcommunity/react-core 0.6.7-alpha.8 → 0.6.7-payments.143
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/cjs/components/provider/SCUserProvider/index.js +6 -0
- package/lib/cjs/constants/Cache.d.ts +21 -2
- package/lib/cjs/constants/Cache.js +29 -4
- package/lib/cjs/constants/Preferences.d.ts +13 -0
- package/lib/cjs/constants/Preferences.js +24 -1
- package/lib/cjs/constants/Routes.d.ts +8 -0
- package/lib/cjs/constants/Routes.js +17 -1
- package/lib/cjs/hooks/useSCFetchCategories.d.ts +2 -0
- package/lib/cjs/hooks/useSCFetchCategories.js +7 -4
- package/lib/cjs/hooks/useSCFetchCourse.d.ts +23 -0
- package/lib/cjs/hooks/useSCFetchCourse.js +81 -0
- package/lib/cjs/hooks/useSCFetchCourses.d.ts +22 -0
- package/lib/cjs/hooks/useSCFetchCourses.js +83 -0
- package/lib/cjs/hooks/useSCFetchLesson.d.ts +22 -0
- package/lib/cjs/hooks/useSCFetchLesson.js +72 -0
- package/lib/cjs/hooks/useSCFetchLessonCommentObject.d.ts +23 -0
- package/lib/cjs/hooks/useSCFetchLessonCommentObject.js +72 -0
- package/lib/cjs/hooks/useSCFetchLessonCommentObjects.d.ts +48 -0
- package/lib/cjs/hooks/useSCFetchLessonCommentObjects.js +302 -0
- package/lib/cjs/hooks/useSCFetchUserBlockedBy.js +1 -1
- package/lib/cjs/hooks/useSCJoinedCoursesManager.d.ts +38 -0
- package/lib/cjs/hooks/useSCJoinedCoursesManager.js +277 -0
- package/lib/cjs/index.d.ts +7 -2
- package/lib/cjs/index.js +12 -2
- package/lib/cjs/themes/theme.js +12 -0
- package/lib/cjs/types/context.d.ts +36 -1
- package/lib/cjs/types/index.d.ts +2 -2
- package/lib/cjs/types/theme.d.ts +52 -0
- package/lib/esm/components/provider/SCUserProvider/index.js +6 -0
- package/lib/esm/constants/Cache.d.ts +21 -2
- package/lib/esm/constants/Cache.js +21 -2
- package/lib/esm/constants/Preferences.d.ts +13 -0
- package/lib/esm/constants/Preferences.js +23 -0
- package/lib/esm/constants/Routes.d.ts +8 -0
- package/lib/esm/constants/Routes.js +16 -0
- package/lib/esm/hooks/useSCFetchCategories.d.ts +2 -0
- package/lib/esm/hooks/useSCFetchCategories.js +7 -4
- package/lib/esm/hooks/useSCFetchCourse.d.ts +23 -0
- package/lib/esm/hooks/useSCFetchCourse.js +78 -0
- package/lib/esm/hooks/useSCFetchCourses.d.ts +22 -0
- package/lib/esm/hooks/useSCFetchCourses.js +81 -0
- package/lib/esm/hooks/useSCFetchLesson.d.ts +22 -0
- package/lib/esm/hooks/useSCFetchLesson.js +69 -0
- package/lib/esm/hooks/useSCFetchLessonCommentObject.d.ts +23 -0
- package/lib/esm/hooks/useSCFetchLessonCommentObject.js +69 -0
- package/lib/esm/hooks/useSCFetchLessonCommentObjects.d.ts +48 -0
- package/lib/esm/hooks/useSCFetchLessonCommentObjects.js +297 -0
- package/lib/esm/hooks/useSCFetchUserBlockedBy.js +1 -1
- package/lib/esm/hooks/useSCJoinedCoursesManager.d.ts +38 -0
- package/lib/esm/hooks/useSCJoinedCoursesManager.js +273 -0
- package/lib/esm/index.d.ts +7 -2
- package/lib/esm/index.js +6 -1
- package/lib/esm/themes/theme.js +12 -0
- package/lib/esm/types/context.d.ts +36 -1
- package/lib/esm/types/index.d.ts +2 -2
- package/lib/esm/types/theme.d.ts +52 -0
- package/lib/umd/react-core.js +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const api_services_1 = require("@selfcommunity/api-services");
|
|
4
|
+
const utils_1 = require("@selfcommunity/utils");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const use_deep_compare_effect_1 = require("use-deep-compare-effect");
|
|
7
|
+
const SCUserProvider_1 = require("../components/provider/SCUserProvider");
|
|
8
|
+
const Cache_1 = require("../constants/Cache");
|
|
9
|
+
const Errors_1 = require("../constants/Errors");
|
|
10
|
+
/**
|
|
11
|
+
:::info
|
|
12
|
+
This custom hook is used to fetch a lesson object.
|
|
13
|
+
:::
|
|
14
|
+
* @param object
|
|
15
|
+
* @param object.id
|
|
16
|
+
* @param object.lesson
|
|
17
|
+
* @param object.cacheStrategy
|
|
18
|
+
*/
|
|
19
|
+
function useSCFetchLesson({ id = null, lesson = null, courseId = null, sectionId = null, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST, }) {
|
|
20
|
+
const __lessonId = (0, react_1.useMemo)(() => (lesson === null || lesson === void 0 ? void 0 : lesson.id) || id, [lesson, id]);
|
|
21
|
+
// CONTEXT
|
|
22
|
+
const scUserContext = (0, SCUserProvider_1.useSCUser)();
|
|
23
|
+
const authUserId = (0, react_1.useMemo)(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]);
|
|
24
|
+
// CACHE
|
|
25
|
+
const __lessonCacheKey = (0, react_1.useMemo)(() => (0, Cache_1.getLessonObjectCacheKey)(__lessonId), [__lessonId]);
|
|
26
|
+
const [scLesson, setScLesson] = (0, react_1.useState)(cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? utils_1.LRUCache.get(__lessonCacheKey, lesson) : null);
|
|
27
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
28
|
+
const setSCLesson = (lesson) => {
|
|
29
|
+
setScLesson(lesson);
|
|
30
|
+
utils_1.LRUCache.set(__lessonCacheKey, lesson);
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Memoized fetchTag
|
|
34
|
+
*/
|
|
35
|
+
const fetchLesson = (0, react_1.useMemo)(() => (id) => {
|
|
36
|
+
return api_services_1.http
|
|
37
|
+
.request({
|
|
38
|
+
url: api_services_1.Endpoints.GetCourseLesson.url({ id: courseId, section_id: sectionId, lesson_id: id }),
|
|
39
|
+
method: api_services_1.Endpoints.GetCourseLesson.method,
|
|
40
|
+
})
|
|
41
|
+
.then((res) => {
|
|
42
|
+
if (res.status >= 300) {
|
|
43
|
+
return Promise.reject(res);
|
|
44
|
+
}
|
|
45
|
+
return Promise.resolve(res.data);
|
|
46
|
+
});
|
|
47
|
+
}, []);
|
|
48
|
+
/**
|
|
49
|
+
* If id attempt to get the lesson by id
|
|
50
|
+
*/
|
|
51
|
+
(0, react_1.useEffect)(() => {
|
|
52
|
+
if (id !== null && id !== undefined && courseId !== null && courseId !== undefined && sectionId) {
|
|
53
|
+
fetchLesson(id)
|
|
54
|
+
.then((e) => {
|
|
55
|
+
setSCLesson(e);
|
|
56
|
+
})
|
|
57
|
+
.catch((err) => {
|
|
58
|
+
utils_1.LRUCache.delete(__lessonCacheKey);
|
|
59
|
+
setError(`Lesson with id ${id} not found`);
|
|
60
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, `Lesson with id ${id} not found`);
|
|
61
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}, [id, courseId, authUserId, sectionId]);
|
|
65
|
+
(0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => {
|
|
66
|
+
if (lesson) {
|
|
67
|
+
setSCLesson(lesson);
|
|
68
|
+
}
|
|
69
|
+
}, [lesson, authUserId]);
|
|
70
|
+
return { scLesson, setSCLesson, error };
|
|
71
|
+
}
|
|
72
|
+
exports.default = useSCFetchLesson;
|
|
@@ -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,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
const Errors_1 = require("../constants/Errors");
|
|
5
|
+
const api_services_1 = require("@selfcommunity/api-services");
|
|
6
|
+
const utils_1 = require("@selfcommunity/utils");
|
|
7
|
+
const Cache_1 = require("../constants/Cache");
|
|
8
|
+
const use_deep_compare_effect_1 = require("use-deep-compare-effect");
|
|
9
|
+
/**
|
|
10
|
+
:::info
|
|
11
|
+
This custom hooks is used to fetch a course lesson comment.
|
|
12
|
+
:::
|
|
13
|
+
* @param object
|
|
14
|
+
* @param object.id
|
|
15
|
+
* @param object.commentObject
|
|
16
|
+
* @param object.lesson
|
|
17
|
+
* @param object.cacheStrategy
|
|
18
|
+
*/
|
|
19
|
+
function useSCFetchLessonCommentObject({ id = null, commentObject = null, lesson = null, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST, }) {
|
|
20
|
+
const __commentObjectId = commentObject ? commentObject.id : id;
|
|
21
|
+
// CACHE
|
|
22
|
+
const __commentObjectCacheKey = (0, Cache_1.getLessonCommentCacheKey)(__commentObjectId);
|
|
23
|
+
const [obj, setObj] = (0, react_1.useState)(cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? utils_1.LRUCache.get(__commentObjectCacheKey, commentObject) : commentObject);
|
|
24
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
25
|
+
/**
|
|
26
|
+
* Memoized fetchLessonCommentObject
|
|
27
|
+
*/
|
|
28
|
+
const fetchLessonCommentObject = (0, react_1.useMemo)(() => () => {
|
|
29
|
+
return api_services_1.http
|
|
30
|
+
.request({
|
|
31
|
+
url: api_services_1.Endpoints.GetCourseLessonComment.url({
|
|
32
|
+
id: lesson.course_id,
|
|
33
|
+
section_id: lesson.section_id,
|
|
34
|
+
lesson_id: lesson.id,
|
|
35
|
+
comment_id: __commentObjectId,
|
|
36
|
+
}),
|
|
37
|
+
method: api_services_1.Endpoints.GetCourseLessonComment.method,
|
|
38
|
+
})
|
|
39
|
+
.then((res) => {
|
|
40
|
+
if (res.status >= 300) {
|
|
41
|
+
return Promise.reject(res);
|
|
42
|
+
}
|
|
43
|
+
return Promise.resolve(res.data);
|
|
44
|
+
});
|
|
45
|
+
}, [__commentObjectId, lesson]);
|
|
46
|
+
/**
|
|
47
|
+
* If course commentObject resolve it
|
|
48
|
+
*/
|
|
49
|
+
(0, react_1.useEffect)(() => {
|
|
50
|
+
if (__commentObjectId && lesson && (!obj || cacheStrategy === utils_1.CacheStrategies.STALE_WHILE_REVALIDATE)) {
|
|
51
|
+
fetchLessonCommentObject()
|
|
52
|
+
.then((obj) => {
|
|
53
|
+
setObj(obj);
|
|
54
|
+
utils_1.LRUCache.set(__commentObjectCacheKey, obj);
|
|
55
|
+
})
|
|
56
|
+
.catch((err) => {
|
|
57
|
+
utils_1.LRUCache.delete(__commentObjectCacheKey);
|
|
58
|
+
setError(`Course Lesson comment Object with id ${id} not found`);
|
|
59
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, `Course Lesson comment Object with id ${id} not found`);
|
|
60
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}, [__commentObjectId, lesson]);
|
|
64
|
+
(0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => {
|
|
65
|
+
if (commentObject) {
|
|
66
|
+
setObj(commentObject);
|
|
67
|
+
utils_1.LRUCache.set(__commentObjectCacheKey, obj);
|
|
68
|
+
}
|
|
69
|
+
}, [commentObject]);
|
|
70
|
+
return { obj, setObj, error };
|
|
71
|
+
}
|
|
72
|
+
exports.default = useSCFetchLessonCommentObject;
|
|
@@ -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;
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.commentsObjectActionTypes = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const Errors_1 = require("../constants/Errors");
|
|
7
|
+
const types_1 = require("@selfcommunity/types");
|
|
8
|
+
const api_services_1 = require("@selfcommunity/api-services");
|
|
9
|
+
const utils_1 = require("@selfcommunity/utils");
|
|
10
|
+
const Cache_1 = require("../constants/Cache");
|
|
11
|
+
const hooks_1 = require("../utils/hooks");
|
|
12
|
+
const pagination_1 = require("../utils/pagination");
|
|
13
|
+
const useSCFetchLesson_1 = tslib_1.__importDefault(require("../hooks/useSCFetchLesson"));
|
|
14
|
+
/**
|
|
15
|
+
* @hidden
|
|
16
|
+
* We have complex state logic that involves multiple sub-values,
|
|
17
|
+
* so useReducer is preferable to useState.
|
|
18
|
+
* Define all possible auth action types label
|
|
19
|
+
* Use this to export actions and dispatch an action
|
|
20
|
+
*/
|
|
21
|
+
exports.commentsObjectActionTypes = {
|
|
22
|
+
LOADING_NEXT: '_loading_next',
|
|
23
|
+
LOADING_PREVIOUS: '_loading_previous',
|
|
24
|
+
DATA_NEXT_LOADED: '_data_next_loaded',
|
|
25
|
+
DATA_PREVIOUS_LOADED: '_data_previous_loaded',
|
|
26
|
+
DATA_RELOAD: '_data_reload',
|
|
27
|
+
DATA_RELOADED: '_data_reloaded',
|
|
28
|
+
DATA_REVALIDATE: '_data_revalidate',
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* commentsReducer:
|
|
32
|
+
* - manage the state of comments object
|
|
33
|
+
* - update the state base on action type
|
|
34
|
+
* @param state
|
|
35
|
+
* @param action
|
|
36
|
+
*/
|
|
37
|
+
function commentsReducer(state, action) {
|
|
38
|
+
switch (action.type) {
|
|
39
|
+
case exports.commentsObjectActionTypes.LOADING_NEXT:
|
|
40
|
+
return Object.assign(Object.assign({}, state), { isLoadingNext: true, isLoadingPrevious: false });
|
|
41
|
+
case exports.commentsObjectActionTypes.LOADING_PREVIOUS:
|
|
42
|
+
return Object.assign(Object.assign({}, state), { isLoadingNext: false, isLoadingPrevious: true });
|
|
43
|
+
case exports.commentsObjectActionTypes.DATA_NEXT_LOADED:
|
|
44
|
+
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, state), { comments: action.payload.comments, isLoadingNext: false, componentLoaded: true, revalidate: false, revalidateNext: null, revalidatePrevious: null, next: action.payload.next }), (action.payload.page ? { page: action.payload.page } : {})), (action.payload.nextPage ? { nextPage: action.payload.nextPage } : {})), (action.payload.previous ? { previous: action.payload.previous } : {})), (action.payload.previousPage ? { previousPage: action.payload.previousPage } : {})), (action.payload.total ? { total: action.payload.total } : {}));
|
|
45
|
+
case exports.commentsObjectActionTypes.DATA_PREVIOUS_LOADED:
|
|
46
|
+
return Object.assign(Object.assign(Object.assign(Object.assign({}, state), { comments: action.payload.comments, isLoadingPrevious: false, revalidate: false, revalidateNext: null, revalidatePrevious: null, previous: action.payload.previous }), (action.payload.page ? { page: action.payload.page } : {})), (action.payload.previousPage ? { previousPage: action.payload.previousPage } : {}));
|
|
47
|
+
case exports.commentsObjectActionTypes.DATA_RELOAD:
|
|
48
|
+
return Object.assign(Object.assign({}, state), { next: action.payload.next, previousPage: null, nextPage: null, comments: [], total: 0, previous: null, isLoadingNext: true, reload: true });
|
|
49
|
+
case exports.commentsObjectActionTypes.DATA_RELOADED:
|
|
50
|
+
return Object.assign(Object.assign({}, state), { componentLoaded: true, reload: false });
|
|
51
|
+
case exports.commentsObjectActionTypes.DATA_REVALIDATE:
|
|
52
|
+
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, state), { componentLoaded: true, revalidate: true }), (action.payload.revalidateNext ? { revalidateNext: action.payload.revalidateNext } : {})), (action.payload.revalidatePrevious ? { revalidatePrevious: action.payload.revalidatePrevious } : {})), { reload: false });
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unhandled type: ${action.type}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Define initial state
|
|
59
|
+
* @param data
|
|
60
|
+
*/
|
|
61
|
+
function stateInitializer(data) {
|
|
62
|
+
const __commentsObjectCacheKey = data.scLesson ? (0, Cache_1.getLessonCommentsCacheKey)(data.scLesson.id, data.next) : null;
|
|
63
|
+
let _initState = {
|
|
64
|
+
componentLoaded: false,
|
|
65
|
+
comments: [],
|
|
66
|
+
total: 0,
|
|
67
|
+
next: data.next,
|
|
68
|
+
previous: null,
|
|
69
|
+
isLoadingNext: false,
|
|
70
|
+
isLoadingPrevious: false,
|
|
71
|
+
page: Math.ceil(data.offset / data.pageSize + 1),
|
|
72
|
+
reload: false,
|
|
73
|
+
revalidate: false,
|
|
74
|
+
revalidateNext: null,
|
|
75
|
+
revalidatePrevious: null,
|
|
76
|
+
};
|
|
77
|
+
_initState['nextPage'] = _initState.next ? _initState.page + 1 : null;
|
|
78
|
+
_initState['previousPage'] = _initState.previous ? _initState.page - 1 : null;
|
|
79
|
+
if (__commentsObjectCacheKey && utils_1.LRUCache.hasKey(__commentsObjectCacheKey) && data.cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY) {
|
|
80
|
+
const _cachedData = utils_1.LRUCache.get(__commentsObjectCacheKey);
|
|
81
|
+
let page = Math.max((0, pagination_1.getCurrentPage)(_cachedData.next, data.pageSize), 1);
|
|
82
|
+
const nextPage = _cachedData.next ? (0, pagination_1.getCurrentPage)(_cachedData.next, data.pageSize) : null;
|
|
83
|
+
const previousPage = _cachedData.previous ? Math.max((0, pagination_1.getCurrentPage)(_cachedData.previous, data.pageSize) - 1, 1) : null;
|
|
84
|
+
return Object.assign(Object.assign({}, _initState), {
|
|
85
|
+
total: _cachedData.count,
|
|
86
|
+
next: _cachedData.next,
|
|
87
|
+
previous: _cachedData.previous,
|
|
88
|
+
comments: _cachedData.results,
|
|
89
|
+
page,
|
|
90
|
+
nextPage,
|
|
91
|
+
previousPage,
|
|
92
|
+
componentLoaded: true,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return _initState;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
:::info
|
|
99
|
+
This custom hooks is used to fetch paginated comments for a specific course lesson.
|
|
100
|
+
:::
|
|
101
|
+
* @param props
|
|
102
|
+
*/
|
|
103
|
+
function useSCFetchLessonCommentObjects(props) {
|
|
104
|
+
// PROPS
|
|
105
|
+
const { id, lessonObject, offset = 0, pageSize = 5, orderBy = types_1.SCCommentsOrderBy.ADDED_AT_DESC, parent, onChangePage, cacheStrategy = utils_1.CacheStrategies.NETWORK_ONLY, } = props;
|
|
106
|
+
// FeedObject
|
|
107
|
+
const { scLesson, setSCLesson } = (0, useSCFetchLesson_1.default)({
|
|
108
|
+
id: id,
|
|
109
|
+
lesson: lessonObject,
|
|
110
|
+
courseId: lessonObject.course_id,
|
|
111
|
+
sectionId: lessonObject.section_id,
|
|
112
|
+
cacheStrategy,
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* Get next url
|
|
116
|
+
*/
|
|
117
|
+
const getNextUrl = () => {
|
|
118
|
+
const _offset = offset ? `&offset=${offset}` : '';
|
|
119
|
+
const _parent = parent ? `&parent=${parent}` : '';
|
|
120
|
+
const _lessonId = scLesson ? scLesson.id : id;
|
|
121
|
+
const _courseId = scLesson ? scLesson.course_id : lessonObject.course_id;
|
|
122
|
+
const _sectionId = scLesson ? scLesson.section_id : lessonObject.section_id;
|
|
123
|
+
return `${api_services_1.Endpoints.GetCourseLessonComments.url({
|
|
124
|
+
id: _courseId,
|
|
125
|
+
section_id: _sectionId,
|
|
126
|
+
lesson_id: _lessonId,
|
|
127
|
+
})}?limit=${pageSize}&ordering=${orderBy}${_offset}${_parent}`;
|
|
128
|
+
};
|
|
129
|
+
// STATE
|
|
130
|
+
const [state, dispatch] = (0, react_1.useReducer)(commentsReducer, { scLesson, offset, pageSize, next: getNextUrl(), cacheStrategy }, stateInitializer);
|
|
131
|
+
// REFS
|
|
132
|
+
const isMountedRef = (0, hooks_1.useIsComponentMountedRef)();
|
|
133
|
+
/**
|
|
134
|
+
* Get Comments (with cache)
|
|
135
|
+
*/
|
|
136
|
+
const revalidate = (url, forward) => {
|
|
137
|
+
return performFetchComments(url, false).then((res) => {
|
|
138
|
+
let _comments;
|
|
139
|
+
let page = (0, pagination_1.getCurrentPage)(forward ? res.next : res.previous, pageSize);
|
|
140
|
+
if (forward) {
|
|
141
|
+
let start = state.comments.slice(0, state.comments.length - res.results.length);
|
|
142
|
+
_comments = start.concat(res.results);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
let start = state.comments.slice(res.results.length, state.comments.length);
|
|
146
|
+
_comments = res.results.concat(start);
|
|
147
|
+
}
|
|
148
|
+
if (isMountedRef.current) {
|
|
149
|
+
dispatch({
|
|
150
|
+
type: forward ? exports.commentsObjectActionTypes.DATA_NEXT_LOADED : exports.commentsObjectActionTypes.DATA_PREVIOUS_LOADED,
|
|
151
|
+
payload: Object.assign(Object.assign({ page, comments: _comments }, (forward ? { next: res.next } : { previous: res.previous })), { total: res.count }),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Get Comments
|
|
158
|
+
*/
|
|
159
|
+
const performFetchComments = (url, seekCache = true) => {
|
|
160
|
+
const __commentObjectsCacheKey = (0, Cache_1.getLessonCommentsCacheKey)(scLesson.id, url);
|
|
161
|
+
if (seekCache && utils_1.LRUCache.hasKey(__commentObjectsCacheKey) && cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY) {
|
|
162
|
+
return Promise.resolve(utils_1.LRUCache.get(__commentObjectsCacheKey));
|
|
163
|
+
}
|
|
164
|
+
return api_services_1.http
|
|
165
|
+
.request({
|
|
166
|
+
url,
|
|
167
|
+
method: api_services_1.Endpoints.GetCourseLessonComments.method,
|
|
168
|
+
})
|
|
169
|
+
.then((res) => {
|
|
170
|
+
if (res.status >= 300) {
|
|
171
|
+
return Promise.reject(res);
|
|
172
|
+
}
|
|
173
|
+
utils_1.LRUCache.set(__commentObjectsCacheKey, res.data);
|
|
174
|
+
res.data.results.forEach((e) => utils_1.LRUCache.set((0, Cache_1.getLessonCommentCacheKey)(e.id), e));
|
|
175
|
+
return Promise.resolve(res.data);
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Fetch previous comments
|
|
180
|
+
*/
|
|
181
|
+
function getPreviousPage() {
|
|
182
|
+
if (scLesson && state.previous && !state.isLoadingPrevious) {
|
|
183
|
+
const _previous = state.previous;
|
|
184
|
+
dispatch({ type: exports.commentsObjectActionTypes.LOADING_PREVIOUS });
|
|
185
|
+
performFetchComments(_previous)
|
|
186
|
+
.then((res) => {
|
|
187
|
+
if (isMountedRef.current) {
|
|
188
|
+
let currentPage = (0, pagination_1.getCurrentPage)(_previous, pageSize);
|
|
189
|
+
let previousPage = res.previous ? currentPage - 1 : null;
|
|
190
|
+
dispatch({
|
|
191
|
+
type: exports.commentsObjectActionTypes.DATA_PREVIOUS_LOADED,
|
|
192
|
+
payload: {
|
|
193
|
+
page: currentPage,
|
|
194
|
+
previousPage,
|
|
195
|
+
comments: [...res.results, ...state.comments],
|
|
196
|
+
previous: res.previous,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
onChangePage && onChangePage(currentPage);
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
.catch((error) => {
|
|
203
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, error);
|
|
204
|
+
})
|
|
205
|
+
.then(() => {
|
|
206
|
+
if (isMountedRef.current && cacheStrategy === utils_1.CacheStrategies.STALE_WHILE_REVALIDATE) {
|
|
207
|
+
dispatch({ type: exports.commentsObjectActionTypes.DATA_REVALIDATE, payload: { revalidatePrevious: _previous } });
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Fetch next comments
|
|
214
|
+
*/
|
|
215
|
+
function getNextPage() {
|
|
216
|
+
if (scLesson && state.next && !state.isLoadingNext) {
|
|
217
|
+
const _next = state.next;
|
|
218
|
+
dispatch({ type: exports.commentsObjectActionTypes.LOADING_NEXT });
|
|
219
|
+
performFetchComments(_next)
|
|
220
|
+
.then((res) => {
|
|
221
|
+
if (isMountedRef.current) {
|
|
222
|
+
let currentPage = (0, pagination_1.getCurrentPage)(_next, pageSize);
|
|
223
|
+
let nextPage = res.next ? currentPage + 1 : null;
|
|
224
|
+
dispatch({
|
|
225
|
+
type: exports.commentsObjectActionTypes.DATA_NEXT_LOADED,
|
|
226
|
+
payload: Object.assign({ page: currentPage, nextPage, comments: [...state.comments, ...res.results], next: res.next, total: res.count, componentLoaded: true }, (offset && state.comments.length === 0 ? { previous: res.previous, previousPage: res.previous ? currentPage - 1 : null } : {})),
|
|
227
|
+
});
|
|
228
|
+
onChangePage && onChangePage(currentPage);
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
.catch((error) => {
|
|
232
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, error);
|
|
233
|
+
})
|
|
234
|
+
.then(() => {
|
|
235
|
+
if (isMountedRef.current && cacheStrategy === utils_1.CacheStrategies.STALE_WHILE_REVALIDATE) {
|
|
236
|
+
dispatch({ type: exports.commentsObjectActionTypes.DATA_REVALIDATE, payload: { revalidateNext: _next } });
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Reset component status on change orderBy, pageSize, offset
|
|
243
|
+
*/
|
|
244
|
+
const reload = () => {
|
|
245
|
+
if (isMountedRef.current && state.componentLoaded && Boolean(scLesson) && !state.isLoadingNext && !state.reload) {
|
|
246
|
+
dispatch({
|
|
247
|
+
type: exports.commentsObjectActionTypes.DATA_RELOAD,
|
|
248
|
+
payload: {
|
|
249
|
+
next: getNextUrl(),
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* Reload fetch comments
|
|
256
|
+
*/
|
|
257
|
+
(0, react_1.useEffect)(() => {
|
|
258
|
+
if (isMountedRef.current && state.componentLoaded && state.reload && !state.isLoadingNext && !state.isLoadingPrevious) {
|
|
259
|
+
dispatch({
|
|
260
|
+
type: exports.commentsObjectActionTypes.DATA_RELOADED,
|
|
261
|
+
});
|
|
262
|
+
getNextPage();
|
|
263
|
+
}
|
|
264
|
+
}, [state.reload, isMountedRef]);
|
|
265
|
+
/**
|
|
266
|
+
* Revalidate last fetched comments
|
|
267
|
+
*/
|
|
268
|
+
(0, react_1.useEffect)(() => {
|
|
269
|
+
if (isMountedRef.current && state.componentLoaded && Boolean(scLesson) && !state.reload && state.revalidate) {
|
|
270
|
+
revalidate(state.revalidateNext, Boolean(state.revalidateNext));
|
|
271
|
+
}
|
|
272
|
+
}, [state.revalidate, isMountedRef]);
|
|
273
|
+
/**
|
|
274
|
+
* Update lesson comments list
|
|
275
|
+
* @param updatedData
|
|
276
|
+
*/
|
|
277
|
+
const updateLessonComments = (updatedData) => {
|
|
278
|
+
if (updatedData) {
|
|
279
|
+
dispatch({
|
|
280
|
+
type: exports.commentsObjectActionTypes.DATA_NEXT_LOADED,
|
|
281
|
+
payload: {
|
|
282
|
+
comments: updatedData,
|
|
283
|
+
next: state.next,
|
|
284
|
+
total: updatedData.length,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
const __commentsCacheKey = (0, Cache_1.getLessonCommentsCacheKey)(scLesson.id, state.next);
|
|
288
|
+
utils_1.LRUCache.set(__commentsCacheKey, updatedData.map((comment) => {
|
|
289
|
+
const __commentCacheKey = (0, Cache_1.getLessonCommentCacheKey)(comment.id);
|
|
290
|
+
utils_1.LRUCache.set(__commentCacheKey, comment);
|
|
291
|
+
return comment.id;
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
return Object.assign(Object.assign({ lessonObject: scLesson, setLessonObject: setSCLesson }, state), { pageSize,
|
|
296
|
+
getNextPage,
|
|
297
|
+
getPreviousPage,
|
|
298
|
+
orderBy,
|
|
299
|
+
reload,
|
|
300
|
+
updateLessonComments });
|
|
301
|
+
}
|
|
302
|
+
exports.default = useSCFetchLessonCommentObjects;
|
|
@@ -71,7 +71,7 @@ function useSCFetchUserBlockedBy({ user = null, blockedByUser = null, sync = tru
|
|
|
71
71
|
*/
|
|
72
72
|
(0, react_1.useEffect)(() => {
|
|
73
73
|
let interval;
|
|
74
|
-
if (scUserContext.user && blockedBy !== null && sync) {
|
|
74
|
+
if (scUserContext.user && scUserContext.user.id !== (user === null || user === void 0 ? void 0 : user.id) && blockedBy !== null && sync) {
|
|
75
75
|
interval = setInterval(() => {
|
|
76
76
|
fetchUserBlockedBy(user, false);
|
|
77
77
|
}, 5000);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SCCourseType, SCUserType } from '@selfcommunity/types';
|
|
2
|
+
/**
|
|
3
|
+
:::info
|
|
4
|
+
This custom hook is used to manage the courses followed.
|
|
5
|
+
:::
|
|
6
|
+
|
|
7
|
+
:::tip How to use it:
|
|
8
|
+
Follow these steps:
|
|
9
|
+
```jsx
|
|
10
|
+
1. const scUserContext: SCUserContextType = useSCUser();
|
|
11
|
+
2. const scJoinedCoursesManager: SCJoinedCoursesManagerType = scUserContext.manager.courses;
|
|
12
|
+
3. scJoinedCoursesManager.isJoined(course)
|
|
13
|
+
```
|
|
14
|
+
:::
|
|
15
|
+
*/
|
|
16
|
+
export default function useSCJoinedCoursesManager(user?: SCUserType): {
|
|
17
|
+
courses: any[];
|
|
18
|
+
loading: any[];
|
|
19
|
+
isLoading: (v: number | {
|
|
20
|
+
id: number;
|
|
21
|
+
}) => boolean;
|
|
22
|
+
join?: undefined;
|
|
23
|
+
leave?: undefined;
|
|
24
|
+
joinStatus?: undefined;
|
|
25
|
+
refresh?: undefined;
|
|
26
|
+
emptyCache?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
courses: any[];
|
|
29
|
+
loading: any[];
|
|
30
|
+
isLoading: (v: number | {
|
|
31
|
+
id: number;
|
|
32
|
+
}) => boolean;
|
|
33
|
+
join: (course: SCCourseType, userId?: number) => Promise<any>;
|
|
34
|
+
leave: (course: SCCourseType) => Promise<any>;
|
|
35
|
+
joinStatus: (course: SCCourseType) => string;
|
|
36
|
+
refresh: () => void;
|
|
37
|
+
emptyCache: () => void;
|
|
38
|
+
};
|