@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
124 lines • 5.01 kB
JavaScript
var _a;
import { createSlice } from "@reduxjs/toolkit";
// Initial state
var initialState = {
notifications: [],
unreadCount: 0,
loading: false,
hasMore: true,
page: 1,
limit: 10,
notificationTemplates: undefined,
currentProjectId: undefined,
};
// Create the slice
export var appNotificationsSlice = 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
export var setProjectContext = (_a = appNotificationsSlice.actions, _a.setProjectContext), setLimit = _a.setLimit, setNotificationTemplates = _a.setNotificationTemplates, resetNotifications = _a.resetNotifications, loadMore = _a.loadMore, setLoading = _a.setLoading, addNotifications = _a.addNotifications, markAsReadLocally = _a.markAsReadLocally, markAllAsReadLocally = _a.markAllAsReadLocally, setUnreadCount = _a.setUnreadCount, handleError = _a.handleError;
// Export reducer
export default appNotificationsSlice.reducer;
// Selectors
export var selectAppNotifications = function (state) {
return state.appNotifications.notifications;
};
export var selectUnreadCount = function (state) {
return state.appNotifications.unreadCount;
};
export var selectAppNotificationsLoading = function (state) {
return state.appNotifications.loading;
};
export var selectAppNotificationsHasMore = function (state) {
return state.appNotifications.hasMore;
};
export var selectAppNotificationsPage = function (state) {
return state.appNotifications.page;
};
export var selectAppNotificationsLimit = function (state) {
return state.appNotifications.limit;
};
export var selectNotificationTemplates = function (state) {
return state.appNotifications.notificationTemplates;
};
export var selectCurrentProjectId = function (state) {
return state.appNotifications.currentProjectId;
};
//# sourceMappingURL=appNotificationsSlice.js.map