fix: Remove notifications from the list when marked as read while viewing the 'unread' tab.
This commit is contained in:
@@ -64,10 +64,16 @@ export default function AgentNotificationsPage() {
|
|||||||
const markAsRead = async (id: string) => {
|
const markAsRead = async (id: string) => {
|
||||||
try {
|
try {
|
||||||
await notificationsApiService.markAsRead(id);
|
await notificationsApiService.markAsRead(id);
|
||||||
setNotifications((prev) =>
|
if (filter === 'unread') {
|
||||||
prev.map((n) => (n.id === id ? { ...n, read: true } : n))
|
// Remove from list when viewing unread tab
|
||||||
);
|
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
||||||
|
} else {
|
||||||
|
setNotifications((prev) =>
|
||||||
|
prev.map((n) => (n.id === id ? { ...n, read: true } : n))
|
||||||
|
);
|
||||||
|
}
|
||||||
setUnreadCount((prev) => Math.max(0, prev - 1));
|
setUnreadCount((prev) => Math.max(0, prev - 1));
|
||||||
|
setTotalCount((prev) => prev); // total doesn't change, notification still exists
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to mark as read:', err);
|
console.error('Failed to mark as read:', err);
|
||||||
}
|
}
|
||||||
@@ -76,7 +82,11 @@ export default function AgentNotificationsPage() {
|
|||||||
const markAllAsRead = async () => {
|
const markAllAsRead = async () => {
|
||||||
try {
|
try {
|
||||||
await notificationsApiService.markAllAsRead();
|
await notificationsApiService.markAllAsRead();
|
||||||
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
|
if (filter === 'unread') {
|
||||||
|
setNotifications([]);
|
||||||
|
} else {
|
||||||
|
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
|
||||||
|
}
|
||||||
setUnreadCount(0);
|
setUnreadCount(0);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to mark all as read:', err);
|
console.error('Failed to mark all as read:', err);
|
||||||
|
|||||||
@@ -62,9 +62,13 @@ export default function UserNotificationsPage() {
|
|||||||
const markAsRead = async (id: string) => {
|
const markAsRead = async (id: string) => {
|
||||||
try {
|
try {
|
||||||
await notificationsApiService.markAsRead(id);
|
await notificationsApiService.markAsRead(id);
|
||||||
setNotifications((prev) =>
|
if (filter === 'unread') {
|
||||||
prev.map((n) => (n.id === id ? { ...n, read: true } : n))
|
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
||||||
);
|
} else {
|
||||||
|
setNotifications((prev) =>
|
||||||
|
prev.map((n) => (n.id === id ? { ...n, read: true } : n))
|
||||||
|
);
|
||||||
|
}
|
||||||
setUnreadCount((prev) => Math.max(0, prev - 1));
|
setUnreadCount((prev) => Math.max(0, prev - 1));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to mark as read:', err);
|
console.error('Failed to mark as read:', err);
|
||||||
@@ -74,7 +78,11 @@ export default function UserNotificationsPage() {
|
|||||||
const markAllAsRead = async () => {
|
const markAllAsRead = async () => {
|
||||||
try {
|
try {
|
||||||
await notificationsApiService.markAllAsRead();
|
await notificationsApiService.markAllAsRead();
|
||||||
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
|
if (filter === 'unread') {
|
||||||
|
setNotifications([]);
|
||||||
|
} else {
|
||||||
|
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
|
||||||
|
}
|
||||||
setUnreadCount(0);
|
setUnreadCount(0);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to mark all as read:', err);
|
console.error('Failed to mark all as read:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user