paperless-ngx/src-ui/src/app/services/toast.service.ts
2023-09-08 11:49:56 -07:00

61 lines
1.1 KiB
TypeScript

import { Injectable } from '@angular/core'
import { Subject } from 'rxjs'
export interface Toast {
title: string
content: string
delay: number
action?: any
actionName?: string
classname?: string
error?: any
}
@Injectable({
providedIn: 'root',
})
export class ToastService {
constructor() {}
private toasts: Toast[] = []
private toastsSubject: Subject<Toast[]> = new Subject()
show(toast: Toast) {
this.toasts.push(toast)
this.toastsSubject.next(this.toasts)
}
showError(content: string, error: any = null, delay: number = 10000) {
this.show({
title: $localize`Error`,
content: content,
delay: delay,
classname: 'error',
error,
})
}
showInfo(content: string, delay: number = 5000) {
this.show({ title: $localize`Information`, content: content, delay: delay })
}
closeToast(toast: Toast) {
let index = this.toasts.findIndex((t) => t == toast)
if (index > -1) {
this.toasts.splice(index, 1)
this.toastsSubject.next(this.toasts)
}
}
getToasts() {
return this.toastsSubject
}
}