UNPKG

@selfcommunity/react-core

Version:

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

111 lines (110 loc) 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const api_services_1 = require("@selfcommunity/api-services"); const types_1 = require("@selfcommunity/types"); const utils_1 = require("@selfcommunity/utils"); const react_1 = require("react"); const use_deep_compare_effect_1 = require("use-deep-compare-effect"); const SCUserProvider_1 = require("../components/provider/SCUserProvider"); const Cache_1 = require("../constants/Cache"); const Errors_1 = require("../constants/Errors"); /** :::info This custom hook is used to fetch an event object. ::: * @param object * @param object.id * @param object.event * @param object.autosubscribe * @param object.cacheStrategy */ function useSCFetchEvent({ id = null, event = null, autoSubscribe = true, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST, }) { const __eventId = (0, react_1.useMemo)(() => (event === null || event === void 0 ? void 0 : event.id) || id, [event, id]); // CONTEXT const scUserContext = (0, SCUserProvider_1.useSCUser)(); const authUserId = (0, react_1.useMemo)(() => { var _a; return ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id) || null; }, [scUserContext.user]); // CACHE const __eventCacheKey = (0, react_1.useMemo)(() => (0, Cache_1.getEventObjectCacheKey)(__eventId), [__eventId]); const __event = (0, react_1.useMemo)(() => (authUserId ? event : (0, utils_1.objectWithoutProperties)(event, ['subscription_status'])), [authUserId, event]); const [scEvent, setScEvent] = (0, react_1.useState)(cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? utils_1.LRUCache.get(__eventCacheKey, __event) : null); const [error, setError] = (0, react_1.useState)(null); /** * Memoized setSCEvent (auto-subscription if need it) */ const setSCEvent = (0, react_1.useMemo)(() => (e) => { if (autoSubscribe && authUserId !== null && ((e.privacy === types_1.SCEventPrivacyType.PUBLIC && !e.subscription_status) || e.subscription_status === types_1.SCEventSubscriptionStatusType.INVITED)) { // Auto subscribe to the event api_services_1.EventService.subscribeToEvent(e.id) .then(() => { const updatedEvent = Object.assign(Object.assign({}, e), { subscription_status: types_1.SCEventSubscriptionStatusType.SUBSCRIBED }); setScEvent(updatedEvent); utils_1.LRUCache.set(__eventCacheKey, updatedEvent); }) .catch(() => { setScEvent(e); utils_1.LRUCache.set(__eventCacheKey, e); }); } else { setScEvent(e); utils_1.LRUCache.set(__eventCacheKey, e); } }, [autoSubscribe, authUserId, setScEvent]); /** * Memoized fetchTag */ const fetchEvent = (0, react_1.useMemo)(() => (id) => { return api_services_1.http .request({ url: api_services_1.Endpoints.GetEventInfo.url({ id }), method: api_services_1.Endpoints.GetEventInfo.method, }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve(res.data); }); }, []); /** * Refresh event */ const refreshEvent = (0, react_1.useMemo)(() => () => () => { fetchEvent(id) .then((e) => { setSCEvent(e); }) .catch((err) => { utils_1.LRUCache.delete(__eventCacheKey); setError(`Error on refresh event with id ${id}`); utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, `Error on refresh event with id ${id}`); utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message); }); }, [id, setSCEvent, __eventCacheKey]); /** * If id attempt to get the event by id */ (0, react_1.useEffect)(() => { if (id !== null && id !== undefined && !event) { fetchEvent(id) .then((e) => { setSCEvent(e); }) .catch((err) => { utils_1.LRUCache.delete(__eventCacheKey); setError(`Event with id ${id} not found`); utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, `Event with id ${id} not found`); utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, err.message); }); } }, [id, event, authUserId]); (0, use_deep_compare_effect_1.useDeepCompareEffectNoCheck)(() => { if (event) { setSCEvent(event); } }, [event, authUserId]); return { scEvent, setSCEvent, error, refreshEvent }; } exports.default = useSCFetchEvent;