@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
391 lines • 18.9 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var useCreateList_1 = __importDefault(require("./useCreateList"));
var useDeleteList_1 = __importDefault(require("./useDeleteList"));
var useUpdateList_1 = __importDefault(require("./useUpdateList"));
var useAddToList_1 = __importDefault(require("./useAddToList"));
var useRemoveFromList_1 = __importDefault(require("./useRemoveFromList"));
var useFetchRootList_1 = __importDefault(require("./useFetchRootList"));
var useFetchSubLists_1 = __importDefault(require("./useFetchSubLists"));
var handleError_1 = require("../../utils/handleError");
var useUser_1 = __importDefault(require("../users/useUser"));
function useListsData(_) {
var _this = this;
var user = (0, useUser_1.default)().user;
var _a = (0, react_1.useState)(null), currentList = _a[0], setCurrentList = _a[1];
var _b = (0, react_1.useState)([]), subLists = _b[0], setSubLists = _b[1];
var loading = (0, react_1.useRef)(false);
var _c = (0, react_1.useState)(false), loadingState = _c[0], setLoadingState = _c[1];
// Cache to store sub-lists for each list by their ID
var subListCache = (0, react_1.useRef)({});
// Stack to keep track of list history
var listHistory = (0, react_1.useRef)([]);
var openList = function (list) {
setSubLists([]);
if (currentList) {
// Push the current list to the history stack before opening a new one
listHistory.current.push(currentList);
}
setCurrentList(list);
};
var goBack = function () {
if (listHistory.current.length === 0)
return;
var previousList = listHistory.current.pop();
if (!previousList)
return;
setCurrentList(previousList);
// Check if sub-lists for this list are already in the cache
if (subListCache.current[previousList.id]) {
setSubLists(subListCache.current[previousList.id]);
return; // No need to fetch, we have cached data
}
};
var goToRoot = function () {
if (listHistory.current.length === 0)
return;
var rootList = listHistory.current[0];
listHistory.current = [];
setCurrentList(rootList);
// Check if sub-lists for this list are already in the cache
if (subListCache.current[rootList.id]) {
setSubLists(subListCache.current[rootList.id]);
return; // No need to fetch, we have cached data
}
};
var isEntityInList = function (selectedEntityId) {
return !!(currentList &&
currentList.entityIds.some(function (entityId) { return entityId === selectedEntityId; }));
};
var fetchRootList = (0, useFetchRootList_1.default)();
var fetchSubLists = (0, useFetchSubLists_1.default)();
var createList = (0, useCreateList_1.default)();
var updateList = (0, useUpdateList_1.default)();
var addToList = (0, useAddToList_1.default)();
var removeFromList = (0, useRemoveFromList_1.default)();
var deleteList = (0, useDeleteList_1.default)();
var handleFetchRootList = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var list, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, fetchRootList()];
case 1:
list = _a.sent();
if (list)
setCurrentList(__assign(__assign({}, list), { parentId: null }));
return [3 /*break*/, 3];
case 2:
err_1 = _a.sent();
(0, handleError_1.handleError)(err_1, "Failed fetching root list");
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); }, [fetchRootList]);
var handleFetchSubLists = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var newSubLists, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!currentList) {
console.warn("Can't fetch sub-lists before fetching root list.");
return [2 /*return*/];
}
// Check if sub-lists for this list are already in the cache
if (subListCache.current[currentList.id]) {
setSubLists(subListCache.current[currentList.id]);
return [2 /*return*/]; // No need to fetch, we have cached data
}
if (loading.current)
return [2 /*return*/];
loading.current = true;
setLoadingState(true);
_a.label = 1;
case 1:
_a.trys.push([1, 3, 4, 5]);
return [4 /*yield*/, fetchSubLists({ listId: currentList.id })];
case 2:
newSubLists = _a.sent();
if (newSubLists) {
setSubLists(newSubLists);
// Cache the fetched sub-lists
subListCache.current[currentList.id] = newSubLists;
}
return [3 /*break*/, 5];
case 3:
error_1 = _a.sent();
(0, handleError_1.handleError)(error_1, "Failed fetching sub-lists");
return [3 /*break*/, 5];
case 4:
loading.current = false;
setLoadingState(false);
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
}); }, [fetchSubLists, currentList]);
var handleCreateList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var newList, err_2;
var listName = _b.listName;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!listName) {
console.error("No entityId provided.");
return [2 /*return*/];
}
if (!currentList) {
console.error("No current list.");
return [2 /*return*/];
}
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, createList({
listName: listName,
parentListId: currentList.id,
})];
case 2:
newList = _c.sent();
if (newList) {
setSubLists([]);
listHistory.current.push(currentList);
setCurrentList(newList);
// Update the cache for the current list
if (currentList && subListCache.current[currentList.id]) {
subListCache.current[currentList.id] = __spreadArray(__spreadArray([], (subListCache.current[currentList.id] || []), true), [
newList,
], false);
}
}
return [3 /*break*/, 4];
case 3:
err_2 = _c.sent();
(0, handleError_1.handleError)(err_2, "Failed to create list");
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); }, [createList, currentList]);
var handleUpdateList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var newList_1, err_3;
var listId = _b.listId, update = _b.update;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4 /*yield*/, updateList({ listId: listId, update: update })];
case 1:
newList_1 = _c.sent();
if (newList_1) {
if (listId === (currentList === null || currentList === void 0 ? void 0 : currentList.id)) {
setCurrentList(newList_1);
}
else {
setSubLists(function (prevLists) {
return prevLists.map(function (list) { return (list.id === listId ? newList_1 : list); });
});
}
}
return [3 /*break*/, 3];
case 2:
err_3 = _c.sent();
(0, handleError_1.handleError)(err_3, "Failed to update list");
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); }, [updateList, currentList]);
var handleAddToList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var newList_2, parentSubLists, err_4;
var entityId = _b.entityId;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!entityId) {
console.error("No entityId provided.");
return [2 /*return*/];
}
if (!currentList) {
console.error("No current list.");
return [2 /*return*/];
}
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, addToList({
entityId: entityId,
listId: currentList.id,
})];
case 2:
newList_2 = _c.sent();
if (newList_2) {
setCurrentList(newList_2);
if (currentList.parentId) {
parentSubLists = subListCache.current[currentList.parentId] || [];
// Update the subListCache with the new data for the current list
subListCache.current[currentList.parentId] = parentSubLists.map(function (list) { return (list.id === newList_2.id ? newList_2 : list); });
}
}
return [3 /*break*/, 4];
case 3:
err_4 = _c.sent();
(0, handleError_1.handleError)(err_4, "Failed to add entity to lists: ");
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); }, [currentList, addToList]);
var handleRemoveFromList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var newList_3, parentSubLists, err_5;
var entityId = _b.entityId;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!currentList) {
console.error("No current list.");
return [2 /*return*/];
}
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, removeFromList({
entityId: entityId,
listId: currentList.id,
})];
case 2:
newList_3 = _c.sent();
if (newList_3) {
setCurrentList(newList_3);
if (currentList.parentId) {
parentSubLists = subListCache.current[currentList.parentId] || [];
// Update the subListCache with the new data for the current list
subListCache.current[currentList.parentId] = parentSubLists.map(function (list) { return (list.id === newList_3.id ? newList_3 : list); });
}
}
return [3 /*break*/, 4];
case 3:
err_5 = _c.sent();
(0, handleError_1.handleError)(err_5, "Failed to remove entity from lists: ");
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); }, [removeFromList, currentList]);
var handleDeleteList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var filteredSubLists, err_6;
var list = _b.list;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4 /*yield*/, deleteList({ listId: list.id })];
case 1:
_c.sent();
// Update the cache by removing the deleted list from its parent's sub-list cache
if (list.parentId && subListCache.current[list.parentId]) {
filteredSubLists = subListCache.current[list.parentId].filter(function (cachedList) { return cachedList.id !== list.id; });
subListCache.current[list.parentId] = filteredSubLists;
}
if (list.id === (currentList === null || currentList === void 0 ? void 0 : currentList.id)) {
goBack();
}
else {
setSubLists(function (prevLists) { return prevLists.filter(function (f) { return f.id !== list.id; }); });
}
return [3 /*break*/, 3];
case 2:
err_6 = _c.sent();
(0, handleError_1.handleError)(err_6, "Failed to delete list");
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); }, [deleteList, currentList]);
(0, react_1.useEffect)(function () {
if (!user)
return;
handleFetchRootList();
}, [handleFetchRootList, user]);
(0, react_1.useEffect)(function () {
handleFetchSubLists();
}, [handleFetchSubLists]);
return {
currentList: currentList,
subLists: subLists,
loading: loadingState,
openList: openList,
goBack: goBack,
goToRoot: goToRoot,
isEntityInList: isEntityInList,
createList: handleCreateList,
updateList: handleUpdateList,
addToList: handleAddToList,
removeFromList: handleRemoveFromList,
deleteList: handleDeleteList,
};
}
exports.default = useListsData;
//# sourceMappingURL=useListsData.js.map