handle notification type and navigate using deeplinking
This commit is contained in:
@@ -1,11 +1,25 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
|
||||
// Define the shape of the notification data object
|
||||
export interface NotificationData {
|
||||
msgType: string;
|
||||
body: string;
|
||||
title: string;
|
||||
additionalProp1?: string;
|
||||
additionalProp2?: string;
|
||||
[key: string]: any; // For any additional properties
|
||||
}
|
||||
|
||||
// Define the shape of a notification object
|
||||
export interface Notification {
|
||||
id: string;
|
||||
title: string;
|
||||
message: string;
|
||||
time: string;
|
||||
originalPriority: number;
|
||||
priority: number;
|
||||
sentTime: number;
|
||||
data: NotificationData;
|
||||
from: string;
|
||||
messageId: string;
|
||||
ttl: number;
|
||||
read: boolean;
|
||||
}
|
||||
|
||||
@@ -23,28 +37,59 @@ const notificationSlice = createSlice({
|
||||
name: 'notifications',
|
||||
initialState,
|
||||
reducers: {
|
||||
addNotification: (state, action: PayloadAction<Notification>) => {
|
||||
// Adds the new notification to the top
|
||||
state.notifications.unshift(action.payload);
|
||||
addNotification: (state, action: PayloadAction<Omit<Notification, 'read'>>) => {
|
||||
// Create a new notification with read status and add to the top
|
||||
const newNotification: Notification = {
|
||||
...action.payload,
|
||||
read: false,
|
||||
};
|
||||
state.notifications.unshift(newNotification);
|
||||
},
|
||||
|
||||
removeNotification: (state, action: PayloadAction<string>) => {
|
||||
// Remove notification by id
|
||||
// Remove notification by id (using messageId as id)
|
||||
state.notifications = state.notifications.filter(
|
||||
(item) => item.id !== action.payload
|
||||
(item) => item.messageId !== action.payload
|
||||
);
|
||||
},
|
||||
|
||||
clearAllNotifications: (state) => {
|
||||
state.notifications = [];
|
||||
},
|
||||
|
||||
markAsRead: (state, action: PayloadAction<string>) => {
|
||||
// Mark one notification as read
|
||||
// Mark one notification as read by messageId
|
||||
const notification = state.notifications.find(
|
||||
(item) => item.id === action.payload
|
||||
(item) => item.messageId === action.payload
|
||||
);
|
||||
if (notification) {
|
||||
notification.read = true;
|
||||
}
|
||||
},
|
||||
|
||||
markAllAsRead: (state) => {
|
||||
// Mark all notifications as read
|
||||
state.notifications.forEach(notification => {
|
||||
notification.read = true;
|
||||
});
|
||||
},
|
||||
|
||||
// Additional utility actions that might be useful
|
||||
removeExpiredNotifications: (state) => {
|
||||
const currentTime = Date.now();
|
||||
state.notifications = state.notifications.filter(
|
||||
(notification) => currentTime - notification.sentTime < notification.ttl * 1000
|
||||
);
|
||||
},
|
||||
|
||||
updateNotificationPriority: (state, action: PayloadAction<{ messageId: string; priority: number }>) => {
|
||||
const notification = state.notifications.find(
|
||||
(item) => item.messageId === action.payload.messageId
|
||||
);
|
||||
if (notification) {
|
||||
notification.priority = action.payload.priority;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -53,6 +98,9 @@ export const {
|
||||
removeNotification,
|
||||
clearAllNotifications,
|
||||
markAsRead,
|
||||
markAllAsRead,
|
||||
removeExpiredNotifications,
|
||||
updateNotificationPriority,
|
||||
} = notificationSlice.actions;
|
||||
|
||||
export default notificationSlice.reducer;
|
||||
export default notificationSlice.reducer;
|
||||
Reference in New Issue
Block a user