mirror of
https://github.com/opelly27/WinStudentGoalTracker.git
synced 2026-05-20 01:47:41 +00:00
26 lines
645 B
TypeScript
26 lines
645 B
TypeScript
import { Injectable, signal } from '@angular/core';
|
|
|
|
export type ToastType = 'success' | 'error' | 'info';
|
|
|
|
export interface Toast {
|
|
id: number;
|
|
message: string;
|
|
type: ToastType;
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class ToastService {
|
|
private nextId = 0;
|
|
readonly toasts = signal<Toast[]>([]);
|
|
|
|
show(message: string, type: ToastType = 'success') {
|
|
const id = ++this.nextId;
|
|
this.toasts.update(list => [...list, { id, message, type }]);
|
|
setTimeout(() => this.dismiss(id), 3000);
|
|
}
|
|
|
|
dismiss(id: number) {
|
|
this.toasts.update(list => list.filter(t => t.id !== id));
|
|
}
|
|
}
|