import { Injectable, signal, OnDestroy } from '@angular/core'; import { ApiService } from './api'; export interface Notification { event_type: 'deadline' | 'expense'; event_id: number; title: string; date: string; } @Injectable({ providedIn: 'root' }) export class NotificationService implements OnDestroy { notifications = signal([]); private intervalId: any; constructor(private api: ApiService) {} start() { this.load(); this.intervalId = setInterval(() => this.load(), 60_000); } stop() { clearInterval(this.intervalId); } ngOnDestroy() { this.stop(); } private load() { this.api.getNotifications().subscribe({ next: (data) => this.notifications.set(data), error: () => {}, }); } markRead(notification: Notification) { this.api.markNotificationRead(notification.event_type, notification.event_id).subscribe({ next: () => { this.notifications.update(list => list.filter(n => !(n.event_type === notification.event_type && n.event_id === notification.event_id)) ); }, }); } markAllRead() { const current = this.notifications(); current.forEach(n => this.api.markNotificationRead(n.event_type, n.event_id).subscribe() ); this.notifications.set([]); } }