UNPKG

@replyke/core

Version:

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

211 lines 9.58 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectCurrentListId = exports.selectCurrentProjectId = exports.selectListsById = exports.selectSubListsMap = exports.selectListHistory = exports.selectListsLoading = exports.selectSubLists = exports.selectCurrentList = exports.handleError = exports.resetLists = exports.handleListDeletion = exports.removeListFromSubLists = exports.addNewListAndNavigate = exports.updateListInSubLists = exports.updateCurrentList = exports.setSubLists = exports.setCurrentList = exports.goToRoot = exports.goBack = exports.openList = exports.setLoading = exports.setProjectContext = exports.listsSlice = void 0; var toolkit_1 = require("@reduxjs/toolkit"); // Initial state var initialState = { listsById: {}, sublistsMap: {}, currentListId: null, listHistory: [], loading: false, currentProjectId: undefined, }; // Create the slice exports.listsSlice = (0, toolkit_1.createSlice)({ name: "lists", initialState: initialState, reducers: { // Set the current project context setProjectContext: function (state, action) { state.currentProjectId = action.payload; }, // Set loading state setLoading: function (state, action) { state.loading = action.payload; }, // Navigation actions openList: function (state, action) { var list = action.payload; // Store the list if not already stored if (!state.listsById[list.id]) { state.listsById[list.id] = list; } // Push current list ID to history stack before opening new one if (state.currentListId) { state.listHistory.push(state.currentListId); } // Set new current list ID state.currentListId = list.id; }, goBack: function (state) { if (state.listHistory.length === 0) return; var previousListId = state.listHistory.pop(); if (!previousListId) return; state.currentListId = previousListId; }, goToRoot: function (state) { if (state.listHistory.length === 0) return; var rootListId = state.listHistory[0]; state.listHistory = []; state.currentListId = rootListId; }, // Set current list (for initial root list fetch) setCurrentList: function (state, action) { var list = action.payload; if (list) { state.listsById[list.id] = list; state.currentListId = list.id; } else { state.currentListId = null; } }, // Set sub-lists and update mapping setSubLists: function (state, action) { var _a = action.payload, lists = _a.lists, parentListId = _a.parentListId; // Store all lists in listsById lists.forEach(function (list) { state.listsById[list.id] = list; }); // Update parent-child mapping state.sublistsMap[parentListId] = lists.map(function (list) { return list.id; }); }, // Update current list (for entity add/remove operations) updateCurrentList: function (state, action) { var updatedList = action.payload; // Update in listsById (single source of truth) state.listsById[updatedList.id] = updatedList; }, // Update a list (now just updates in listsById) updateListInSubLists: function (state, action) { var updatedList = action.payload; // Update in listsById (single source of truth) state.listsById[updatedList.id] = updatedList; }, // Add new list to sub-lists and navigate to it addNewListAndNavigate: function (state, action) { var newList = action.payload; if (!state.currentListId) return; // Store the new list state.listsById[newList.id] = newList; // Push current list ID to history state.listHistory.push(state.currentListId); // Set new list as current state.currentListId = newList.id; // Update parent-child mapping if (newList.parentId) { if (!state.sublistsMap[newList.parentId]) { state.sublistsMap[newList.parentId] = []; } if (!state.sublistsMap[newList.parentId].includes(newList.id)) { state.sublistsMap[newList.parentId].push(newList.id); } } }, // Remove list from sub-lists and storage removeListFromSubLists: function (state, action) { var listId = action.payload; // Remove from listsById delete state.listsById[listId]; // Remove from all parent-child mappings Object.keys(state.sublistsMap).forEach(function (parentId) { state.sublistsMap[parentId] = state.sublistsMap[parentId].filter(function (id) { return id !== listId; }); }); // Remove its own sublists mapping if it exists delete state.sublistsMap[listId]; }, // Handle list deletion handleListDeletion: function (state, action) { var _a = action.payload, listId = _a.listId, parentId = _a.parentId; // Remove from parent-child mapping if (parentId && state.sublistsMap[parentId]) { state.sublistsMap[parentId] = state.sublistsMap[parentId].filter(function (id) { return id !== listId; }); } // Remove from listsById delete state.listsById[listId]; // Remove its own sublists mapping delete state.sublistsMap[listId]; // If deleted list is current list, go back if (state.currentListId === listId) { if (state.listHistory.length === 0) { state.currentListId = null; return; } var previousListId = state.listHistory.pop(); if (!previousListId) { state.currentListId = null; return; } state.currentListId = previousListId; } }, // Reset all lists data resetLists: function (state) { state.listsById = {}; state.sublistsMap = {}; state.currentListId = null; state.listHistory = []; state.loading = false; }, // Handle errors by stopping loading handleError: function (state) { state.loading = false; }, }, }); // Export actions exports.setProjectContext = (_a = exports.listsSlice.actions, _a.setProjectContext), exports.setLoading = _a.setLoading, exports.openList = _a.openList, exports.goBack = _a.goBack, exports.goToRoot = _a.goToRoot, exports.setCurrentList = _a.setCurrentList, exports.setSubLists = _a.setSubLists, exports.updateCurrentList = _a.updateCurrentList, exports.updateListInSubLists = _a.updateListInSubLists, exports.addNewListAndNavigate = _a.addNewListAndNavigate, exports.removeListFromSubLists = _a.removeListFromSubLists, exports.handleListDeletion = _a.handleListDeletion, exports.resetLists = _a.resetLists, exports.handleError = _a.handleError; // Export reducer exports.default = exports.listsSlice.reducer; // Selectors var selectCurrentList = function (state) { var _a = state.lists, currentListId = _a.currentListId, listsById = _a.listsById; return currentListId ? listsById[currentListId] || null : null; }; exports.selectCurrentList = selectCurrentList; exports.selectSubLists = (0, toolkit_1.createSelector)([function (state) { return state.lists.currentListId; }, function (state) { return state.lists.sublistsMap; }, function (state) { return state.lists.listsById; }], function (currentListId, sublistsMap, listsById) { if (!currentListId || !sublistsMap[currentListId]) { return []; } return sublistsMap[currentListId] .map(function (listId) { return listsById[listId]; }) .filter(Boolean); // Remove any undefined entries }); var selectListsLoading = function (state) { return state.lists.loading; }; exports.selectListsLoading = selectListsLoading; exports.selectListHistory = (0, toolkit_1.createSelector)([function (state) { return state.lists.listHistory; }, function (state) { return state.lists.listsById; }], function (listHistory, listsById) { return listHistory .map(function (listId) { return listsById[listId]; }) .filter(Boolean); // Remove any undefined entries }); // New selector for the sublists mapping var selectSubListsMap = function (state) { return state.lists.sublistsMap; }; exports.selectSubListsMap = selectSubListsMap; // New selector for all lists var selectListsById = function (state) { return state.lists.listsById; }; exports.selectListsById = selectListsById; var selectCurrentProjectId = function (state) { return state.lists.currentProjectId; }; exports.selectCurrentProjectId = selectCurrentProjectId; // New selector for current list ID var selectCurrentListId = function (state) { return state.lists.currentListId; }; exports.selectCurrentListId = selectCurrentListId; //# sourceMappingURL=listsSlice.js.map