fix notifications not clearing
This commit is contained in:
parent
edca4d5ceb
commit
6956149a3d
4 changed files with 44 additions and 28 deletions
|
|
@ -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);
|
||||
|
|
@ -55,24 +55,22 @@ export const PATCH: RequestHandler = async ({ request }) => {
|
|||
if (!session?.user) throw error(401, 'Not authenticated');
|
||||
|
||||
const userId = Number(session.user.id);
|
||||
const { ids, markAsRead } = await request.json();
|
||||
const { markAsRead } = await request.json();
|
||||
|
||||
if (!Array.isArray(ids) || typeof markAsRead !== 'boolean') {
|
||||
if (typeof markAsRead !== 'boolean') {
|
||||
throw error(400, 'Invalid request body');
|
||||
}
|
||||
|
||||
try {
|
||||
await db
|
||||
.update(notifications)
|
||||
.set({ isRead: markAsRead })
|
||||
.where(and(
|
||||
eq(notifications.userId, userId),
|
||||
inArray(notifications.id, ids)
|
||||
));
|
||||
if (markAsRead) {
|
||||
await db.update(notifications)
|
||||
.set({ isRead: true })
|
||||
.where(eq(notifications.userId, userId));
|
||||
}
|
||||
|
||||
return json({ success: true });
|
||||
} catch (e) {
|
||||
console.error('Failed to update notifications:', e);
|
||||
throw error(500, 'Failed to update notifications');
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
@ -13,10 +13,32 @@ const REWARD_TIERS = [
|
|||
1500, // Day 2
|
||||
1800, // Day 3
|
||||
2100, // Day 4
|
||||
2500, // Day 5
|
||||
3000, // Day 6
|
||||
3500, // Day 7
|
||||
4000, // Day 8+
|
||||
2500, // Day 5
|
||||
3000, // Day 6
|
||||
3500, // Day 7
|
||||
4000, // Day 8
|
||||
4200, // Day 9
|
||||
4400, // Day 10
|
||||
4600, // Day 11
|
||||
4800, // Day 12
|
||||
5000, // Day 13
|
||||
5200, // Day 14
|
||||
5400, // Day 15
|
||||
5600, // Day 16
|
||||
5800, // Day 17
|
||||
6000, // Day 18
|
||||
6200, // Day 19
|
||||
6400, // Day 20
|
||||
6600, // Day 21
|
||||
6800, // Day 22
|
||||
7000, // Day 23
|
||||
7200, // Day 24
|
||||
7400, // Day 25
|
||||
7600, // Day 26
|
||||
7800, // Day 27
|
||||
8000, // Day 28
|
||||
8200, // Day 29
|
||||
8500 // Day 30+
|
||||
];
|
||||
|
||||
const PRESTIGE_MULTIPLIERS = {
|
||||
|
|
|
|||
|
|
@ -33,9 +33,7 @@
|
|||
const unreadIds = ($NOTIFICATIONS || []).filter((n) => !n.isRead).map((n) => n.id);
|
||||
newNotificationIds = unreadIds;
|
||||
|
||||
if (unreadIds.length > 0) {
|
||||
await markNotificationsAsRead(unreadIds);
|
||||
}
|
||||
await markNotificationsAsRead();
|
||||
} catch (error) {
|
||||
toast.error('Failed to load notifications');
|
||||
} finally {
|
||||
|
|
|
|||
Reference in a new issue