UNPKG

@replyke/core

Version:

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

135 lines 6.25 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectCurrentProjectId = exports.selectNotificationTemplates = exports.selectAppNotificationsLimit = exports.selectAppNotificationsPage = exports.selectAppNotificationsHasMore = exports.selectAppNotificationsLoading = exports.selectUnreadCount = exports.selectAppNotifications = exports.handleError = exports.setUnreadCount = exports.markAllAsReadLocally = exports.markAsReadLocally = exports.addNotifications = exports.setLoading = exports.loadMore = exports.resetNotifications = exports.setNotificationTemplates = exports.setLimit = exports.setProjectContext = exports.appNotificationsSlice = void 0; var toolkit_1 = require("@reduxjs/toolkit"); // Initial state var initialState = { notifications: [], unreadCount: 0, loading: false, hasMore: true, page: 1, limit: 10, notificationTemplates: undefined, currentProjectId: undefined, }; // Create the slice exports.appNotificationsSlice = (0, toolkit_1.createSlice)({ name: 'appNotifications', initialState: initialState, reducers: { // Set the current project context setProjectContext: function (state, action) { state.currentProjectId = action.payload; }, // Set pagination limit setLimit: function (state, action) { state.limit = action.payload; }, // Set notification templates setNotificationTemplates: function (state, action) { state.notificationTemplates = action.payload; }, // Reset notifications (clear all and restart pagination) resetNotifications: function (state) { state.notifications = []; state.page = 1; state.hasMore = true; state.loading = false; }, // Load more notifications (increment page) loadMore: function (state) { if (state.hasMore && !state.loading) { state.page += 1; } }, // Set loading state setLoading: function (state, action) { state.loading = action.payload; }, // Add new notifications (for pagination) addNotifications: function (state, action) { var _a; var _b = action.payload, notifications = _b.notifications, _c = _b.isFirstPage, isFirstPage = _c === void 0 ? false : _c; if (isFirstPage) { state.notifications = notifications; } else { // Prevent duplicates when adding new pages var existingIds_1 = new Set(state.notifications.map(function (n) { return n.id; })); var newNotifications = notifications.filter(function (n) { return !existingIds_1.has(n.id); }); (_a = state.notifications).push.apply(_a, newNotifications); } // Update hasMore based on returned count vs limit if (notifications.length < state.limit) { state.hasMore = false; } state.loading = false; }, // Mark notification as read locally (optimistic update) markAsReadLocally: function (state, action) { var notificationId = action.payload; var notification = state.notifications.find(function (n) { return n.id === notificationId; }); if (notification && !notification.isRead) { notification.isRead = true; // Decrease unread count state.unreadCount = Math.max(state.unreadCount - 1, 0); } }, // Mark all notifications as read locally (optimistic update) markAllAsReadLocally: function (state) { state.notifications.forEach(function (notification) { notification.isRead = true; }); // Set unread count to 0 state.unreadCount = 0; }, // Set unread count setUnreadCount: function (state, action) { state.unreadCount = action.payload; }, // Handle errors by stopping loading handleError: function (state) { state.loading = false; }, }, }); // Export actions exports.setProjectContext = (_a = exports.appNotificationsSlice.actions, _a.setProjectContext), exports.setLimit = _a.setLimit, exports.setNotificationTemplates = _a.setNotificationTemplates, exports.resetNotifications = _a.resetNotifications, exports.loadMore = _a.loadMore, exports.setLoading = _a.setLoading, exports.addNotifications = _a.addNotifications, exports.markAsReadLocally = _a.markAsReadLocally, exports.markAllAsReadLocally = _a.markAllAsReadLocally, exports.setUnreadCount = _a.setUnreadCount, exports.handleError = _a.handleError; // Export reducer exports.default = exports.appNotificationsSlice.reducer; // Selectors var selectAppNotifications = function (state) { return state.appNotifications.notifications; }; exports.selectAppNotifications = selectAppNotifications; var selectUnreadCount = function (state) { return state.appNotifications.unreadCount; }; exports.selectUnreadCount = selectUnreadCount; var selectAppNotificationsLoading = function (state) { return state.appNotifications.loading; }; exports.selectAppNotificationsLoading = selectAppNotificationsLoading; var selectAppNotificationsHasMore = function (state) { return state.appNotifications.hasMore; }; exports.selectAppNotificationsHasMore = selectAppNotificationsHasMore; var selectAppNotificationsPage = function (state) { return state.appNotifications.page; }; exports.selectAppNotificationsPage = selectAppNotificationsPage; var selectAppNotificationsLimit = function (state) { return state.appNotifications.limit; }; exports.selectAppNotificationsLimit = selectAppNotificationsLimit; var selectNotificationTemplates = function (state) { return state.appNotifications.notificationTemplates; }; exports.selectNotificationTemplates = selectNotificationTemplates; var selectCurrentProjectId = function (state) { return state.appNotifications.currentProjectId; }; exports.selectCurrentProjectId = selectCurrentProjectId; //# sourceMappingURL=appNotificationsSlice.js.map