UNPKG

@selfcommunity/react-core

Version:

React Core Components useful for integrating UI Community components (react-ui).

94 lines (93 loc) 3.83 kB
import { Endpoints, http } from '@selfcommunity/api-services'; import { CacheStrategies, Logger, LRUCache, objectWithoutProperties } from '@selfcommunity/utils'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'; import { useSCUser } from '../components/provider/SCUserProvider'; import { getCourseObjectCacheKey } from '../constants/Cache'; import { SCOPE_SC_CORE } from '../constants/Errors'; /** :::info This custom hook is used to fetch a course object. ::: * @param object * @param object.id * @param object.course * @param object.cacheStrategy * @param object.params */ export default function useSCFetchCourse({ id = null, course = null, cacheStrategy = CacheStrategies.CACHE_FIRST, params = null, }) { const __courseId = useMemo(() => (course === null || course === void 0 ? void 0 : course.id) || id, [course, id]); // CONTEXT const scUserContext = useSCUser(); const authUserId = useMemo(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]); // CACHE const __courseCacheKey = useMemo(() => getCourseObjectCacheKey(__courseId), [__courseId]); const __course = useMemo(() => (authUserId ? course : objectWithoutProperties(course, ['join_status'])), [authUserId, course]); const [scCourse, setScCourse] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__courseCacheKey, __course) : null); const [error, setError] = useState(null); const setSCCourse = useCallback((c) => { setScCourse(c); LRUCache.set(__courseCacheKey, c); }, [setScCourse, __courseCacheKey]); /** * Memoized fetchTag */ const fetchCourse = useMemo(() => (id) => { return http .request({ url: Endpoints.GetCourseInfo.url({ id }), method: Endpoints.GetCourseInfo.method, params: params !== null && params !== void 0 ? params : {}, }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve(res.data); }); }, []); /** * Refresh course */ const refreshCourse = useMemo(() => () => { fetchCourse(id) .then((e) => { setSCCourse(e); }) .catch((err) => { LRUCache.delete(__courseCacheKey); setError(`Error on refresh course with id ${id}`); Logger.error(SCOPE_SC_CORE, `Error on refresh course with id ${id}`); Logger.error(SCOPE_SC_CORE, err.message); }); }, [id, __courseCacheKey, setSCCourse]); /** * If id attempt to get the course by id */ useEffect(() => { if (id !== null && id !== undefined && !course) { fetchCourse(id) .then((e) => { setSCCourse(e); }) .catch((err) => { LRUCache.delete(__courseCacheKey); if (err.status === 403) { setError('You do not have permission to perform this action.'); Logger.error(SCOPE_SC_CORE, 'You do not have permission to perform this action.'); } else { setError(`Course with id ${id} not found`); Logger.error(SCOPE_SC_CORE, `Course with id ${id} not found`); } Logger.error(SCOPE_SC_CORE, err.message); }); } }, [id, course, authUserId]); useDeepCompareEffectNoCheck(() => { if (course) { setSCCourse(course); } }, [course, authUserId]); return { scCourse, setSCCourse, error, refreshCourse }; }