@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
239 lines • 11.3 kB
JavaScript
;
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 __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 react_redux_1 = require("react-redux");
var listsSlice_1 = require("../../store/slices/listsSlice");
var useListsActions_1 = require("./useListsActions");
var useProject_1 = __importDefault(require("../projects/useProject"));
var user_1 = require("../user");
/**
* Redux-powered hook that provides the exact same interface as useListsData()
* This is a drop-in replacement for the Context-based hook
*/
function useLists(_) {
var _this = this;
if (_ === void 0) { _ = {}; }
var dispatch = (0, react_redux_1.useDispatch)();
// Get external context
var projectId = (0, useProject_1.default)().projectId;
var user = (0, user_1.useUser)().user;
// Get Redux state
var currentList = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectCurrentList)(state);
});
var subLists = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectSubLists)(state);
});
var loading = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectListsLoading)(state);
});
var listHistory = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectListHistory)(state);
});
var subListsMap = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectSubListsMap)(state);
});
var currentProjectId = (0, react_redux_1.useSelector)(function (state) {
return (0, listsSlice_1.selectCurrentProjectId)(state);
});
// Get actions
var _a = (0, useListsActions_1.useListsActions)(), openList = _a.openList, goBack = _a.goBack, goToRoot = _a.goToRoot, fetchRootList = _a.fetchRootList, fetchSubLists = _a.fetchSubLists, createListAction = _a.createList, updateListAction = _a.updateList, deleteListAction = _a.deleteList, addToListAction = _a.addToList, removeFromListAction = _a.removeFromList;
// Update Redux state when project changes
(0, react_1.useEffect)(function () {
if (projectId && projectId !== currentProjectId) {
dispatch((0, listsSlice_1.setProjectContext)(projectId));
}
}, [dispatch, projectId, currentProjectId]);
// Fetch root list when user and project are available
(0, react_1.useEffect)(function () {
if (!user || !projectId)
return;
fetchRootList(projectId);
}, [fetchRootList, user, projectId]);
// Fetch sub-lists when current list changes
(0, react_1.useEffect)(function () {
if (!user || !projectId || !currentList)
return;
// Check if sub-lists for this list are already fetched
if (subListsMap[currentList.id] !== undefined) {
return; // No need to fetch, we already have the mapping (even if empty)
}
fetchSubLists(projectId, currentList.id);
}, [fetchSubLists, user, projectId, currentList, subListsMap]);
// Entity membership checker
var isEntityInList = (0, react_1.useCallback)(function (selectedEntityId) {
if (!currentList)
return false;
return currentList.entityIds.some(function (entityId) { return entityId === selectedEntityId; });
}, [currentList]);
// Wrapped CRUD operations that match the original interface
var handleCreateList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var listName = _b.listName;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!listName) {
console.error("No listName provided.");
return [2 /*return*/];
}
if (!currentList) {
console.error("No current list.");
return [2 /*return*/];
}
if (!projectId) {
console.error("No projectId available.");
return [2 /*return*/];
}
return [4 /*yield*/, createListAction(projectId, currentList.id, listName)];
case 1:
_c.sent();
return [2 /*return*/];
}
});
}); }, [createListAction, currentList, projectId]);
var handleUpdateList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var listId = _b.listId, update = _b.update;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!projectId) {
console.error("No projectId available.");
return [2 /*return*/];
}
return [4 /*yield*/, updateListAction(projectId, listId, update)];
case 1:
_c.sent();
return [2 /*return*/];
}
});
}); }, [updateListAction, projectId]);
var handleDeleteList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var list = _b.list;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!projectId) {
console.error("No projectId available.");
return [2 /*return*/];
}
return [4 /*yield*/, deleteListAction(projectId, list)];
case 1:
_c.sent();
return [2 /*return*/];
}
});
}); }, [deleteListAction, projectId]);
var handleAddToList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
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*/];
}
if (!projectId) {
console.error("No projectId available.");
return [2 /*return*/];
}
return [4 /*yield*/, addToListAction(projectId, currentList.id, entityId)];
case 1:
_c.sent();
return [2 /*return*/];
}
});
}); }, [addToListAction, currentList, projectId]);
var handleRemoveFromList = (0, react_1.useCallback)(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
var entityId = _b.entityId;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!currentList) {
console.error("No current list.");
return [2 /*return*/];
}
if (!projectId) {
console.error("No projectId available.");
return [2 /*return*/];
}
return [4 /*yield*/, removeFromListAction(projectId, currentList.id, entityId)];
case 1:
_c.sent();
return [2 /*return*/];
}
});
}); }, [removeFromListAction, currentList, projectId]);
// Return the same interface as the original hook
return (0, react_1.useMemo)(function () { return ({
currentList: currentList,
subLists: subLists,
loading: loading,
openList: openList,
goBack: goBack,
goToRoot: goToRoot,
isEntityInList: isEntityInList,
createList: handleCreateList,
updateList: handleUpdateList,
deleteList: handleDeleteList,
addToList: handleAddToList,
removeFromList: handleRemoveFromList,
}); }, [
currentList,
subLists,
loading,
openList,
goBack,
goToRoot,
isEntityInList,
handleCreateList,
handleUpdateList,
handleDeleteList,
handleAddToList,
handleRemoveFromList,
]);
}
exports.default = useLists;
//# sourceMappingURL=useLists.js.map