UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

119 lines 4.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const useFetchEntity_1 = __importDefault(require("./useFetchEntity")); const useFetchEntityByForeignId_1 = __importDefault(require("./useFetchEntityByForeignId")); const useFetchEntityByShortId_1 = __importDefault(require("./useFetchEntityByShortId")); const useUpdateEntity_1 = __importDefault(require("./useUpdateEntity")); const useDeleteEntity_1 = __importDefault(require("./useDeleteEntity")); const handleError_1 = require("../../utils/handleError"); function useEntityData({ entityId, foreignId, shortId, entity: entityProp, createIfNotFound, }) { const [entity, setEntity] = (0, react_1.useState)(entityProp); // Cache to store fetched entities keyed by unique identifier const entityCache = (0, react_1.useRef)({}); const fetchEntity = (0, useFetchEntity_1.default)(); const fetchEntityByForeignId = (0, useFetchEntityByForeignId_1.default)(); const fetchEntityByShortId = (0, useFetchEntityByShortId_1.default)(); const updateEntity = (0, useUpdateEntity_1.default)(); const deleteEntity = (0, useDeleteEntity_1.default)(); const handleUpdateEntity = (0, react_1.useCallback)(async ({ update }) => { if (!entity) return; try { const newEntity = await updateEntity({ entityId: entity.id, update, }); if (newEntity) setEntity(newEntity); return newEntity; } catch (err) { (0, handleError_1.handleError)(err, "Failed to update entity"); } }, [entity, updateEntity]); const handleDeleteEntity = (0, react_1.useCallback)(async () => { if (!entity) return; try { await deleteEntity({ entityId: entity.id }); setEntity(undefined); } catch (err) { (0, handleError_1.handleError)(err, "Failed to delete entity"); } }, [entity, deleteEntity]); (0, react_1.useEffect)(() => { const handleFetchEntity = async () => { if (!foreignId && !entityId && !shortId) return; if (entity && entityId && entity.id === entityId) return; if (entity && foreignId && entity.foreignId === foreignId) return; if (entity && shortId && entity.shortId === shortId) return; const uniqueKey = `${entityId ?? ""}-${foreignId ?? ""}-${shortId ?? ""}`; // If we have a cached entity, update the state and exit. if (entityCache.current[uniqueKey]) { setEntity(entityCache.current[uniqueKey]); return; } try { let fetchedEntity = null; if (entityId) { fetchedEntity = await fetchEntity({ entityId, }); } else if (foreignId) { fetchedEntity = await fetchEntityByForeignId({ foreignId, createIfNotFound, }); } else if (shortId) { fetchedEntity = await fetchEntityByShortId({ shortId, }); } if (fetchedEntity) { // Store the fetched entity in cache. entityCache.current[uniqueKey] = fetchedEntity; setEntity(fetchedEntity); } else { setEntity(null); } } catch (err) { (0, handleError_1.handleError)(err, "Failed to fetch entity"); } }; handleFetchEntity(); }, [ fetchEntity, fetchEntityByForeignId, fetchEntityByShortId, entityId, foreignId, shortId, entity, createIfNotFound, ]); (0, react_1.useEffect)(() => { if (entityProp) setEntity(entityProp); }, [entityProp]); return { entity, setEntity, updateEntity: handleUpdateEntity, deleteEntity: handleDeleteEntity, }; } exports.default = useEntityData; //# sourceMappingURL=useEntityData.js.map