@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
102 lines (99 loc) • 3.62 kB
JavaScript
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 Cache_1 = require("../constants/Cache");
const SCUserProvider_1 = require("../components/provider/SCUserProvider");
const init = { snippets: [], isLoading: true };
// HYDRATE the cache
const hydrate = (ids) => {
if (!ids) {
return null;
}
const snippets = ids.map((id) => {
const __snippetCacheKey = (0, Cache_1.getPmSnippetObjectCacheKey)(id);
return utils_1.LRUCache.get(__snippetCacheKey);
});
if (snippets.filter((c) => !c).length > 0) {
// REVALIDATE CACHE
return null;
}
return snippets;
};
/**
:::info
This custom hook is used to fetch snippets.
@param object.cacheStrategy
:::tip Context can be consumed in this way:
```jsx
const {snippets, isLoading} = useSCFetchPrivateMessageSnippets();
```
:::
* @param props
*/
const useSCFetchPrivateMessageSnippets = (props) => {
// PROPS
const { cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST } = props || {};
// CACHE
const __snippetsCacheKey = (0, Cache_1.getPmSnippetsObjectCacheKey)();
// STATE
const snippets = cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY ? hydrate(utils_1.LRUCache.get(__snippetsCacheKey, null)) : null;
const [data, setData] = (0, react_1.useState)(snippets !== null ? { snippets, isLoading: false } : init);
// HOOKS
const scUserContext = (0, SCUserProvider_1.useSCUser)();
/**
* Fetch snippets
*/
const fetchSnippets = (next = api_services_1.Endpoints.GetSnippets.url()) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const response = yield api_services_1.http.request({
url: next,
method: api_services_1.Endpoints.GetSnippets.method,
});
const data = response.data;
if (data.next) {
return data.results.concat(yield fetchSnippets(data.next));
}
return data.results;
});
/**
* Get snippets
*/
(0, react_1.useEffect)(() => {
if (cacheStrategy === utils_1.CacheStrategies.CACHE_FIRST && snippets) {
return;
}
fetchSnippets()
.then((data) => {
setData({ snippets: data, isLoading: false });
utils_1.LRUCache.set(__snippetsCacheKey, data.map((snippet) => {
const __snippetCacheKey = (0, Cache_1.getPmSnippetObjectCacheKey)(snippet.id);
utils_1.LRUCache.set(__snippetCacheKey, snippet);
return snippet.id;
}));
})
.catch((error) => {
console.log(error);
utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, 'Unable to retrieve snippets');
});
}, []);
/**
* Updated snippets list
* @param updatedData
*/
const updateSnippets = (updatedData) => {
if (updatedData) {
setData({ snippets: updatedData, isLoading: false });
//cache update
utils_1.LRUCache.set(__snippetsCacheKey, updatedData.map((snippet) => {
const __snippetCacheKey = (0, Cache_1.getPmSnippetObjectCacheKey)(snippet.id);
utils_1.LRUCache.set(__snippetCacheKey, snippet);
return snippet.id;
}));
}
};
return { data, updateSnippets };
};
exports.default = useSCFetchPrivateMessageSnippets;
;