fix notifications not clearing

This commit is contained in:
Face 2025-07-15 19:11:50 +03:00
parent edca4d5ceb
commit 6956149a3d
4 changed files with 44 additions and 28 deletions

View file

@ -2,7 +2,7 @@ import { writable, derived } from 'svelte/store';
export interface Notification {
id: number;
type: string;
type: 'HOPIUM' | 'TRANSFER' | 'RUG_PULL' | 'SYSTEM';
title: string;
message: string;
link?: string;
@ -24,10 +24,10 @@ export async function fetchNotifications(unreadOnly = false) {
if (!response.ok) throw new Error('Failed to fetch notifications');
const data = await response.json();
NOTIFICATIONS.set(data.notifications);
UNREAD_COUNT.set(data.unreadCount);
return data;
} catch (error) {
console.error('Failed to fetch notifications:', error);
@ -35,23 +35,21 @@ export async function fetchNotifications(unreadOnly = false) {
}
}
export async function markNotificationsAsRead(ids: number[]) {
export async function markNotificationsAsRead() {
try {
const response = await fetch('/api/notifications', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids, markAsRead: true })
body: JSON.stringify({ markAsRead: true })
});
if (!response.ok) throw new Error('Failed to mark notifications as read');
NOTIFICATIONS.update(notifications =>
notifications.map(notif =>
ids.includes(notif.id) ? { ...notif, isRead: true } : notif
)
NOTIFICATIONS.update(notifications =>
notifications.map(notif => ({ ...notif, isRead: true }))
);
UNREAD_COUNT.update(count => Math.max(0, count - ids.length));
UNREAD_COUNT.set(0);
} catch (error) {
console.error('Failed to mark notifications as read:', error);
@ -59,4 +57,4 @@ export async function markNotificationsAsRead(ids: number[]) {
}
}
export const hasUnreadNotifications = derived(UNREAD_COUNT, count => count > 0);
export const hasUnreadNotifications = derived(UNREAD_COUNT, count => count > 0);