zminay w alertach cenowych
This commit is contained in:
@@ -12,6 +12,7 @@ export type ServerNotification = {
|
||||
title: string;
|
||||
body: string | null;
|
||||
link: string | null;
|
||||
dedupeKey: string | null;
|
||||
read: boolean;
|
||||
createdAt: string;
|
||||
};
|
||||
@@ -41,6 +42,9 @@ type NotificationsContextValue = {
|
||||
markRead: (id: number) => void;
|
||||
markAllRead: () => void;
|
||||
notify: (input: NotifyInput) => void;
|
||||
// Kasuje powiadomienia o kluczu zaczynajacym sie od prefiksu - uzywane przy usuwaniu
|
||||
// alertu cenowego, zeby nie zostal po nim slad.
|
||||
dismissByDedupePrefix: (prefix: string) => void;
|
||||
refresh: () => void;
|
||||
};
|
||||
|
||||
@@ -162,8 +166,26 @@ export function NotificationsProvider({ children }: { children: ReactNode }) {
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
|
||||
const dismissByDedupePrefix = useCallback((prefix: string) => {
|
||||
if (!getToken() || !prefix) {
|
||||
return;
|
||||
}
|
||||
// Znikaja od razu z listy, reszte sprzata backend.
|
||||
setNotifications((current) => current.filter((item) => !(item.dedupeKey ?? '').startsWith(prefix)));
|
||||
// Zdejmujemy tez blokade deduplikacji, inaczej po ponownym dodaniu alertu ta sama
|
||||
// zmiana ceny nie wygenerowalaby juz powiadomienia w tej sesji.
|
||||
sentDedupeKeys.current.forEach((key) => {
|
||||
if (key.startsWith(prefix)) {
|
||||
sentDedupeKeys.current.delete(key);
|
||||
}
|
||||
});
|
||||
apiFetch(`/notifications?dedupePrefix=${encodeURIComponent(prefix)}`, { method: 'DELETE' }).catch(() => {});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<NotificationsContext.Provider value={{ notifications, unreadCount, markRead, markAllRead, notify, refresh }}>
|
||||
<NotificationsContext.Provider
|
||||
value={{ notifications, unreadCount, markRead, markAllRead, notify, dismissByDedupePrefix, refresh }}
|
||||
>
|
||||
{children}
|
||||
</NotificationsContext.Provider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user