@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
77 lines (74 loc) • 2.88 kB
JavaScript
import { __awaiter } from "tslib";
import { useEffect, useState } from 'react';
import { SCOPE_SC_CORE } from '../constants/Errors';
import { CategoryService, Endpoints } from '@selfcommunity/api-services';
import { CacheStrategies, Logger, LRUCache } from '@selfcommunity/utils';
import { getCategoriesObjectCacheKey, getCategoryObjectCacheKey } from '../constants/Cache';
const init = { categories: [], isLoading: true };
// HYDRATE the cache
const hydrate = (ids, endpointQueryParams) => {
if (!ids) {
return null;
}
const categories = ids.map((id) => {
const __categoryCacheKey = getCategoryObjectCacheKey(id);
return LRUCache.get(__categoryCacheKey);
});
if (categories.filter((c) => !c).length > 0) {
// REVALIDATE CACHE
return null;
}
if (endpointQueryParams === null || endpointQueryParams === void 0 ? void 0 : endpointQueryParams.can_create_content) {
return categories.filter((c) => !c.content_only_staff);
}
return categories;
};
/**
:::info
This custom hook is used to fetch categories.
:::tip Context can be consumed in this way:
```jsx
const {categories, isLoading} = useSCFetchCategories();
```
:::
* @param props
*/
const useSCFetchCategories = (props) => {
// PROPS
const { cacheStrategy = CacheStrategies.CACHE_FIRST, endpointQueryParams = {} } = props || {};
// CACHE
const __categoriesCacheKey = getCategoriesObjectCacheKey();
// STATE
const categories = cacheStrategy !== CacheStrategies.NETWORK_ONLY ? hydrate(LRUCache.get(__categoriesCacheKey, null), endpointQueryParams) : null;
const [data, setData] = useState(categories !== null ? { categories, isLoading: false } : init);
/**
* Fetch categories
*/
const fetchCategories = (next = Endpoints.CategoryList.url()) => __awaiter(void 0, void 0, void 0, function* () {
const data = yield CategoryService.getAllCategories(Object.assign({ active: true }, endpointQueryParams), { url: next });
return data.next ? data.results.concat(yield fetchCategories(data.next)) : data.results;
});
/**
* Get categories
*/
useEffect(() => {
if (cacheStrategy === CacheStrategies.CACHE_FIRST && categories) {
return;
}
fetchCategories()
.then((data) => {
setData({ categories: data, isLoading: false });
LRUCache.set(__categoriesCacheKey, data.map((cat) => {
const __categoryCacheKey = getCategoryObjectCacheKey(cat.id);
LRUCache.set(__categoryCacheKey, cat);
return cat.id;
}));
})
.catch((error) => {
console.log(error);
Logger.error(SCOPE_SC_CORE, 'Unable to retrieve categories');
});
}, []);
return data;
};
export default useSCFetchCategories;