@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
202 lines • 8.51 kB
JavaScript
var _a;
import { createSlice, createSelector } from "@reduxjs/toolkit";
// Initial state
var initialState = {
listsById: {},
sublistsMap: {},
currentListId: null,
listHistory: [],
loading: false,
currentProjectId: undefined,
};
// Create the slice
export var listsSlice = 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
export var setProjectContext = (_a = listsSlice.actions, _a.setProjectContext), setLoading = _a.setLoading, openList = _a.openList, goBack = _a.goBack, goToRoot = _a.goToRoot, setCurrentList = _a.setCurrentList, setSubLists = _a.setSubLists, updateCurrentList = _a.updateCurrentList, updateListInSubLists = _a.updateListInSubLists, addNewListAndNavigate = _a.addNewListAndNavigate, removeListFromSubLists = _a.removeListFromSubLists, handleListDeletion = _a.handleListDeletion, resetLists = _a.resetLists, handleError = _a.handleError;
// Export reducer
export default listsSlice.reducer;
// Selectors
export var selectCurrentList = function (state) {
var _a = state.lists, currentListId = _a.currentListId, listsById = _a.listsById;
return currentListId ? listsById[currentListId] || null : null;
};
export var selectSubLists = 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
});
export var selectListsLoading = function (state) {
return state.lists.loading;
};
export var selectListHistory = 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
export var selectSubListsMap = function (state) {
return state.lists.sublistsMap;
};
// New selector for all lists
export var selectListsById = function (state) {
return state.lists.listsById;
};
export var selectCurrentProjectId = function (state) {
return state.lists.currentProjectId;
};
// New selector for current list ID
export var selectCurrentListId = function (state) {
return state.lists.currentListId;
};
//# sourceMappingURL=listsSlice.js.map