paperless-ngx/src-ui/src/app/services/toast.service.ts

61 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core'
import { Subject, zip } from 'rxjs'
2020-10-27 01:10:18 +01:00
export interface Toast {
2020-10-27 01:10:18 +01:00
title: string
content: string
2020-12-28 21:52:09 +01:00
delay: number
action?: any
actionName?: string
classname?: string
2023-05-17 12:46:04 -07:00
error?: any
2020-10-27 01:10:18 +01:00
}
@Injectable({
providedIn: 'root',
2020-10-27 01:10:18 +01:00
})
export class ToastService {
constructor() {}
2020-10-27 01:10:18 +01:00
private toasts: Toast[] = []
2020-10-29 14:35:36 +01:00
private toastsSubject: Subject<Toast[]> = new Subject()
2020-10-27 01:10:18 +01:00
2020-12-28 21:52:09 +01:00
show(toast: Toast) {
2020-10-27 01:10:18 +01:00
this.toasts.push(toast)
2020-10-29 14:35:36 +01:00
this.toastsSubject.next(this.toasts)
2020-10-27 01:10:18 +01:00
}
2023-05-17 12:46:04 -07:00
showError(content: string, delay: number = 10000, error: any = null) {
this.show({
title: $localize`Error`,
content: content,
delay: delay,
classname: 'error',
2023-05-17 12:46:04 -07:00
error,
})
}
2020-12-28 21:52:09 +01:00
showInfo(content: string, delay: number = 5000) {
this.show({ title: $localize`Information`, content: content, delay: delay })
}
2020-10-27 01:10:18 +01:00
closeToast(toast: Toast) {
let index = this.toasts.findIndex((t) => t == toast)
2020-10-27 01:10:18 +01:00
if (index > -1) {
this.toasts.splice(index, 1)
2020-10-29 14:35:36 +01:00
this.toastsSubject.next(this.toasts)
2020-10-27 01:10:18 +01:00
}
}
getToasts() {
2020-10-29 14:35:36 +01:00
return this.toastsSubject
2020-10-27 01:10:18 +01:00
}
}