UNPKG

@replyke/core

Version:

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

338 lines 15.4 kB
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 }; } }; import { useCallback, useMemo } from "react"; import { useDispatch } from "react-redux"; import { setLoading, openList, goBack, goToRoot, setCurrentList, setSubLists, updateCurrentList, updateListInSubLists, addNewListAndNavigate, handleListDeletion, resetLists, handleError, } from "../../store/slices/listsSlice"; import { useLazyFetchRootListQuery, useLazyFetchSubListsQuery, useCreateListMutation, useUpdateListMutation, useDeleteListMutation, useAddToListMutation, useRemoveFromListMutation, } from "../../store/api/listsApi"; import { handleError as handleErrorUtil } from "../../utils/handleError"; /** * Redux-powered hook that provides all lists actions * This replaces the individual hooks and provides a centralized way to manage lists */ export function useListsActions() { var _this = this; var dispatch = useDispatch(); // RTK Query hooks var fetchRootListQuery = useLazyFetchRootListQuery()[0]; var fetchSubListsQuery = useLazyFetchSubListsQuery()[0]; var createListMutation = useCreateListMutation()[0]; var updateListMutation = useUpdateListMutation()[0]; var deleteListMutation = useDeleteListMutation()[0]; var addToListMutation = useAddToListMutation()[0]; var removeFromListMutation = useRemoveFromListMutation()[0]; // Navigation actions var openListAction = useCallback(function (list) { dispatch(openList(list)); }, [dispatch]); var goBackAction = useCallback(function () { dispatch(goBack()); }, [dispatch]); var goToRootAction = useCallback(function () { dispatch(goToRoot()); }, [dispatch]); // Fetch root list var fetchRootList = useCallback(function (projectId) { return __awaiter(_this, void 0, void 0, function () { var result, rootList, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId) { console.warn("Can't fetch root list without projectId."); return [2 /*return*/]; } dispatch(setLoading(true)); _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); return [4 /*yield*/, fetchRootListQuery({ projectId: projectId }).unwrap()]; case 2: result = _a.sent(); if (result) { rootList = __assign(__assign({}, result), { parentId: null }); dispatch(setCurrentList(rootList)); } return [3 /*break*/, 5]; case 3: err_1 = _a.sent(); handleErrorUtil(err_1, "Failed fetching root list"); dispatch(handleError()); return [3 /*break*/, 5]; case 4: dispatch(setLoading(false)); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }, [dispatch, fetchRootListQuery]); // Fetch sub-lists var fetchSubLists = useCallback(function (projectId, listId) { return __awaiter(_this, void 0, void 0, function () { var result, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !listId) { console.warn("Can't fetch sub-lists without projectId and listId."); return [2 /*return*/]; } dispatch(setLoading(true)); _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); return [4 /*yield*/, fetchSubListsQuery({ projectId: projectId, listId: listId }).unwrap()]; case 2: result = _a.sent(); if (result) { dispatch(setSubLists({ lists: result, parentListId: listId })); } return [3 /*break*/, 5]; case 3: err_2 = _a.sent(); handleErrorUtil(err_2, "Failed fetching sub-lists"); dispatch(handleError()); return [3 /*break*/, 5]; case 4: dispatch(setLoading(false)); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }, [dispatch, fetchSubListsQuery]); // Create list var createList = useCallback(function (projectId, parentListId, listName) { return __awaiter(_this, void 0, void 0, function () { var result, err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !parentListId || !listName) { console.error("Missing required parameters for creating list."); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, createListMutation({ projectId: projectId, parentListId: parentListId, listName: listName, }).unwrap()]; case 2: result = _a.sent(); if (result) { dispatch(addNewListAndNavigate(result)); } return [3 /*break*/, 4]; case 3: err_3 = _a.sent(); handleErrorUtil(err_3, "Failed to create list"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [createListMutation, dispatch]); // Update list var updateList = useCallback(function (projectId, listId, update) { return __awaiter(_this, void 0, void 0, function () { var result, err_4; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !listId) { console.error("Missing required parameters for updating list."); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, updateListMutation({ projectId: projectId, listId: listId, update: update, }).unwrap()]; case 2: result = _a.sent(); if (result) { // Check if it's the current list or a sub-list dispatch(updateListInSubLists(result)); } return [3 /*break*/, 4]; case 3: err_4 = _a.sent(); handleErrorUtil(err_4, "Failed to update list"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [updateListMutation, dispatch]); // Delete list var deleteList = useCallback(function (projectId, list) { return __awaiter(_this, void 0, void 0, function () { var err_5; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !list) { console.error("Missing required parameters for deleting list."); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, deleteListMutation({ projectId: projectId, listId: list.id, }).unwrap()]; case 2: _a.sent(); dispatch(handleListDeletion({ listId: list.id, parentId: list.parentId })); return [3 /*break*/, 4]; case 3: err_5 = _a.sent(); handleErrorUtil(err_5, "Failed to delete list"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [deleteListMutation, dispatch]); // Add entity to list var addToList = useCallback(function (projectId, listId, entityId) { return __awaiter(_this, void 0, void 0, function () { var result, err_6; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !listId || !entityId) { console.error("Missing required parameters for adding to list."); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, addToListMutation({ projectId: projectId, listId: listId, entityId: entityId, }).unwrap()]; case 2: result = _a.sent(); if (result) { dispatch(updateCurrentList(result)); } return [3 /*break*/, 4]; case 3: err_6 = _a.sent(); handleErrorUtil(err_6, "Failed to add entity to list"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [addToListMutation, dispatch]); // Remove entity from list var removeFromList = useCallback(function (projectId, listId, entityId) { return __awaiter(_this, void 0, void 0, function () { var result, err_7; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !listId || !entityId) { console.error("Missing required parameters for removing from list."); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, removeFromListMutation({ projectId: projectId, listId: listId, entityId: entityId, }).unwrap()]; case 2: result = _a.sent(); if (result) { dispatch(updateCurrentList(result)); } return [3 /*break*/, 4]; case 3: err_7 = _a.sent(); handleErrorUtil(err_7, "Failed to remove entity from list"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [removeFromListMutation, dispatch]); // Reset lists var resetListsAction = useCallback(function () { dispatch(resetLists()); }, [dispatch]); return useMemo(function () { return ({ // Navigation openList: openListAction, goBack: goBackAction, goToRoot: goToRootAction, // Data fetching fetchRootList: fetchRootList, fetchSubLists: fetchSubLists, // CRUD operations createList: createList, updateList: updateList, deleteList: deleteList, addToList: addToList, removeFromList: removeFromList, // Utility resetLists: resetListsAction, }); }, [ openListAction, goBackAction, goToRootAction, fetchRootList, fetchSubLists, createList, updateList, deleteList, addToList, removeFromList, resetListsAction, ]); } export default useListsActions; //# sourceMappingURL=useListsActions.js.map