UNPKG

@selfcommunity/react-core

Version:

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

89 lines (88 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const react_1 = require("react"); const Errors_1 = require("../constants/Errors"); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const useIsComponentMountedRef_1 = tslib_1.__importDefault(require("../utils/hooks/useIsComponentMountedRef")); const Cache_1 = require("../constants/Cache"); /** :::info This custom hook is used to fetch a custom adv object. ::: * @param object * @param object.id * @param object.position * @param object.categoriesId * @param object.cacheStrategy */ function useSCFetchCustomAdv({ id = null, position = null, categoriesId = null, active = true, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST, }) { const [scCustomAdv, setSCCustomAdv] = (0, react_1.useState)(id !== null && cacheStrategy === utils_1.CacheStrategies.CACHE_FIRST && utils_1.LRUCache.get((0, Cache_1.getAdvObjectCacheKey)(id)) ? utils_1.LRUCache.get((0, Cache_1.getAdvObjectCacheKey)(id)) : null); const [error, setError] = (0, react_1.useState)(null); // REFS const mounted = (0, useIsComponentMountedRef_1.default)(); /** * Cache advertising object */ const storeCache = (0, react_1.useMemo)(() => (data) => { data.map((d) => { utils_1.LRUCache.set((0, Cache_1.getAdvObjectCacheKey)(d.id), d); }); }, []); /** * Memoized fetchCustomAdv */ const fetchCustomAdv = (0, react_1.useMemo)(() => () => { if (id !== null) { return api_services_1.http .request({ url: `${api_services_1.Endpoints.CustomAdv.url({ id })}?active=${Boolean(active)}`, method: api_services_1.Endpoints.CustomAdv.method, }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve([res.data]); }); } return api_services_1.http .request({ url: `${api_services_1.Endpoints.CustomAdvSearch.url({})}?active=${Boolean(active)}`, method: api_services_1.Endpoints.CustomAdvSearch.method, params: Object.assign(Object.assign({}, (position && { position: position })), { categories: categoriesId ? `[${categoriesId.toString()}]` : '[]' }), }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve(res.data.results); }); }, [id, position, `${categoriesId}`]); /** * If id attempt to get the category by id */ (0, react_1.useEffect)(() => { if (!scCustomAdv || cacheStrategy !== utils_1.CacheStrategies.CACHE_FIRST) { fetchCustomAdv() .then((data) => { if (mounted.current) { setSCCustomAdv(data[Math.floor(Math.random() * data.length)]); } storeCache(data); }) .catch((err) => { if (mounted.current) { setError(`Custom ADV with position ${position} not found`); } utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, `Custom ADV with position ${position} not found`); utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message); }); } }, [id, position, `${categoriesId}`, scCustomAdv]); return { scCustomAdv, setSCCustomAdv, error }; } exports.default = useSCFetchCustomAdv;