UNPKG

@selfcommunity/react-core

Version:

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

82 lines (79 loc) 2.64 kB
import { __awaiter } from "tslib"; import { useEffect, useState } from 'react'; import { SCOPE_SC_CORE } from '../constants/Errors'; import { Endpoints, http } from '@selfcommunity/api-services'; import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils'; import { getCourseObjectCacheKey, getCoursesObjectCacheKey } from '../constants/Cache'; const init = { courses: [], isLoading: true }; // HYDRATE the cache const hydrate = (ids) => { if (!ids) { return null; } const courses = ids.map((id) => { const __courseCacheKey = getCourseObjectCacheKey(id); return LRUCache.get(__courseCacheKey); }); if (courses.filter((c) => !c).length > 0) { // REVALIDATE CACHE return null; } return courses; }; /** :::info This custom hook is used to fetch courses. @param object.cacheStrategy :::tip Context can be consumed in this way: ```jsx const {courses, isLoading} = useSCFetchCourses(); ``` ::: * @param props */ const useSCFetchCourses = (props) => { // PROPS const { cacheStrategy = CacheStrategies.CACHE_FIRST } = props || {}; // CACHE const __coursesCacheKey = getCoursesObjectCacheKey(); // STATE const courses = cacheStrategy !== CacheStrategies.NETWORK_ONLY ? hydrate(LRUCache.get(__coursesCacheKey, null)) : null; const [data, setData] = useState(courses !== null ? { courses, isLoading: false } : init); /** * Fetch courses */ const fetchCourses = (next = Endpoints.GetJoinedCourses.url()) => __awaiter(void 0, void 0, void 0, function* () { const response = yield http.request({ url: next, method: Endpoints.GetJoinedCourses.method, }); const data = response.data; if (data.next) { return data.results.concat(yield fetchCourses(data.next)); } return data.results; }); /** * Get courses */ useEffect(() => { if (cacheStrategy === CacheStrategies.CACHE_FIRST && courses) { return; } fetchCourses() .then((data) => { setData({ courses: data, isLoading: false }); LRUCache.set(__coursesCacheKey, data.map((course) => { const __courseCacheKey = getCourseObjectCacheKey(course.id); LRUCache.set(__courseCacheKey, course); return course.id; })); }) .catch((error) => { console.log(error); Logger.error(SCOPE_SC_CORE, 'Unable to retrieve courses'); }); }, []); return data; }; export default useSCFetchCourses;