UNPKG

@replyke/core

Version:

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

266 lines 13.4 kB
"use strict"; 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 }); exports.useAppNotificationsActions = useAppNotificationsActions; var react_1 = require("react"); var react_redux_1 = require("react-redux"); var appNotificationsSlice_1 = require("../../store/slices/appNotificationsSlice"); var appNotificationsApi_1 = require("../../store/api/appNotificationsApi"); var handleError_1 = require("../../utils/handleError"); var addNotificationsMessages_1 = __importDefault(require("../../helpers/addNotificationsMessages")); var useProject_1 = __importDefault(require("../projects/useProject")); var user_1 = require("../user"); /** * Hook that provides Redux-powered actions for app notifications * Integrates RTK Query with Redux slice actions */ function useAppNotificationsActions() { var _this = this; var dispatch = (0, react_redux_1.useDispatch)(); // Get current state for actions var projectIdFromSlice = (0, react_redux_1.useSelector)(function (state) { return (0, appNotificationsSlice_1.selectCurrentProjectId)(state); }); var page = (0, react_redux_1.useSelector)(function (state) { return (0, appNotificationsSlice_1.selectAppNotificationsPage)(state); }); var limit = (0, react_redux_1.useSelector)(function (state) { return (0, appNotificationsSlice_1.selectAppNotificationsLimit)(state); }); var notificationTemplates = (0, react_redux_1.useSelector)(function (state) { return (0, appNotificationsSlice_1.selectNotificationTemplates)(state); }); // Get project and user context (fallback to current hooks) var projectIdFromHook = (0, useProject_1.default)().projectId; var user = (0, user_1.useUser)().user; // Use project ID from slice if available, otherwise from hook var projectId = projectIdFromSlice || projectIdFromHook; // RTK Query hooks var triggerFetchNotifications = (0, appNotificationsApi_1.useLazyFetchAppNotificationsQuery)()[0]; var markNotificationAsReadMutation = (0, appNotificationsApi_1.useMarkNotificationAsReadMutation)()[0]; var markAllNotificationsAsReadMutation = (0, appNotificationsApi_1.useMarkAllNotificationsAsReadMutation)()[0]; var triggerCountUnread = (0, appNotificationsApi_1.useLazyCountUnreadNotificationsQuery)()[0]; // Load more action var loadMore = (0, react_1.useCallback)(function () { dispatch((0, appNotificationsSlice_1.loadMore)()); }, [dispatch]); // Mark notification as read action var markNotificationAsRead = (0, react_1.useCallback)(function (notificationId) { return __awaiter(_this, void 0, void 0, function () { var error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !user) { throw new Error("No project ID or authenticated user available"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); // Optimistic update dispatch((0, appNotificationsSlice_1.markAsReadLocally)(notificationId)); // Make API call return [4 /*yield*/, markNotificationAsReadMutation({ projectId: projectId, notificationId: notificationId, }).unwrap()]; case 2: // Make API call _a.sent(); return [3 /*break*/, 4]; case 3: error_1 = _a.sent(); (0, handleError_1.handleError)(error_1, "Failed to mark notification as read:"); throw error_1; case 4: return [2 /*return*/]; } }); }); }, [dispatch, projectId, user, markNotificationAsReadMutation]); // Reset notifications action var resetAppNotifications = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () { var result, completeNotifications, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !user) { throw new Error("No project ID or authenticated user available"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); dispatch((0, appNotificationsSlice_1.setLoading)(true)); dispatch((0, appNotificationsSlice_1.resetNotifications)()); return [4 /*yield*/, triggerFetchNotifications({ projectId: projectId, page: 1, limit: limit, }).unwrap()]; case 2: result = _a.sent(); if (result) { completeNotifications = (0, addNotificationsMessages_1.default)(result, notificationTemplates); dispatch((0, appNotificationsSlice_1.addNotifications)({ notifications: completeNotifications, isFirstPage: true, })); } return [3 /*break*/, 4]; case 3: error_2 = _a.sent(); (0, handleError_1.handleError)(error_2, "Failed to refresh notifications:"); throw error_2; case 4: return [2 /*return*/]; } }); }); }, [ dispatch, projectId, user, triggerFetchNotifications, limit, notificationTemplates, ]); // Fetch more notifications (internal action triggered by page changes) var fetchMoreNotifications = (0, react_1.useCallback)(function (pageToFetch) { return __awaiter(_this, void 0, void 0, function () { var result, completeNotifications, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !user) return [2 /*return*/]; _a.label = 1; case 1: _a.trys.push([1, 3, 4, 5]); dispatch((0, appNotificationsSlice_1.setLoading)(true)); return [4 /*yield*/, triggerFetchNotifications({ projectId: projectId, page: pageToFetch, limit: limit, }).unwrap()]; case 2: result = _a.sent(); if (result) { completeNotifications = (0, addNotificationsMessages_1.default)(result, notificationTemplates); dispatch((0, appNotificationsSlice_1.addNotifications)({ notifications: completeNotifications })); } return [3 /*break*/, 5]; case 3: error_3 = _a.sent(); (0, handleError_1.handleError)(error_3, "Loading more app notifications failed:"); return [3 /*break*/, 5]; case 4: dispatch((0, appNotificationsSlice_1.setLoading)(false)); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); }, [ dispatch, projectId, user, triggerFetchNotifications, limit, notificationTemplates, ]); // Update unread count var updateUnreadCount = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () { var count, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !user) return [2 /*return*/]; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, triggerCountUnread({ projectId: projectId }).unwrap()]; case 2: count = _a.sent(); if (typeof count === "number") { dispatch((0, appNotificationsSlice_1.setUnreadCount)(count)); } return [3 /*break*/, 4]; case 3: error_4 = _a.sent(); (0, handleError_1.handleError)(error_4, "Failed to fetch unread count:"); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }, [dispatch, projectId, user, triggerCountUnread]); // Mark all notifications as read action var markAllNotificationsAsRead = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () { var error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!projectId || !user) { throw new Error("No project ID or authenticated user available"); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); // Optimistic update dispatch((0, appNotificationsSlice_1.markAllAsReadLocally)()); // Make API call return [4 /*yield*/, markAllNotificationsAsReadMutation({ projectId: projectId }).unwrap()]; case 2: // Make API call _a.sent(); return [3 /*break*/, 4]; case 3: error_5 = _a.sent(); (0, handleError_1.handleError)(error_5, "Failed to mark all notifications as read:"); throw error_5; case 4: return [2 /*return*/]; } }); }); }, [dispatch, projectId, user, markAllNotificationsAsReadMutation]); return { loadMore: loadMore, markNotificationAsRead: markNotificationAsRead, markAllNotificationsAsRead: markAllNotificationsAsRead, resetAppNotifications: resetAppNotifications, fetchMoreNotifications: fetchMoreNotifications, // Internal action updateUnreadCount: updateUnreadCount, }; } exports.default = useAppNotificationsActions; //# sourceMappingURL=useAppNotificationsActions.js.map