UNPKG

@selfcommunity/react-core

Version:

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

71 lines (70 loc) 2.88 kB
import { useEffect, useMemo, useState } from 'react'; import { SCOPE_SC_CORE } from '../constants/Errors'; import { Endpoints, http } from '@selfcommunity/api-services'; import { CacheStrategies, Logger, LRUCache, objectWithoutProperties } from '@selfcommunity/utils'; import { getCategoryObjectCacheKey } from '../constants/Cache'; import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'; import { useSCUser } from '../components/provider/SCUserProvider'; /** :::info This custom hook is used to fetch a category object. ::: * @param object * @param object.id * @param object.category * @param object.cacheStrategy */ export default function useSCFetchCategory({ id = null, category = null, cacheStrategy = CacheStrategies.CACHE_FIRST, }) { const __categoryId = category ? category.id : id; // CONTEXT const scUserContext = useSCUser(); const authUserId = scUserContext.user ? scUserContext.user.id : null; // CACHE const __categoryCacheKey = getCategoryObjectCacheKey(__categoryId); const __category = authUserId ? category : objectWithoutProperties(category, ['followed']); const [scCategory, setSCCategory] = useState(cacheStrategy !== CacheStrategies.NETWORK_ONLY ? LRUCache.get(__categoryCacheKey, __category) : null); const [error, setError] = useState(null); /** * Memoized fetchTag */ const fetchCategory = useMemo(() => () => { return http .request({ url: Endpoints.Category.url({ id: __categoryId }), method: Endpoints.Category.method, }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve(res.data); }); }, [__categoryId]); /** * If id attempt to get the category by id */ useEffect(() => { if (__categoryId && (!scCategory || (scCategory && __categoryId !== scCategory.id))) { fetchCategory() .then((obj) => { const _c = authUserId ? obj : objectWithoutProperties(obj, ['followed']); setSCCategory(_c); LRUCache.set(__categoryCacheKey, _c); }) .catch((err) => { LRUCache.delete(__categoryCacheKey); setError(`Category with id ${id} not found`); Logger.error(SCOPE_SC_CORE, `Category with id ${id} not found`); Logger.error(SCOPE_SC_CORE, err.message); }); } }, [__categoryId]); useDeepCompareEffectNoCheck(() => { if (category) { const _c = authUserId ? category : objectWithoutProperties(category, ['followed']); setSCCategory(_c); LRUCache.set(__categoryCacheKey, _c); } }, [category]); return { scCategory, setSCCategory, error }; }