mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-14 00:26:21 +00:00
Feature: better toast notifications management (#8980)
This commit is contained in:
@@ -25,6 +25,33 @@ describe('ToastService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('adds a unique id to toast on show', () => {
|
||||
const toast = {
|
||||
title: 'Title',
|
||||
content: 'content',
|
||||
delay: 5000,
|
||||
}
|
||||
toastService.show(toast)
|
||||
|
||||
toastService.getToasts().subscribe((toasts) => {
|
||||
expect(toasts[0].id).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
it('parses error string to object on show', () => {
|
||||
const toast = {
|
||||
title: 'Title',
|
||||
content: 'content',
|
||||
delay: 5000,
|
||||
error: 'Error string',
|
||||
}
|
||||
toastService.show(toast)
|
||||
|
||||
toastService.getToasts().subscribe((toasts) => {
|
||||
expect(toasts[0].error).toEqual('Error string')
|
||||
})
|
||||
})
|
||||
|
||||
it('creates toasts with defaults on showInfo and showError', () => {
|
||||
toastService.showInfo('Info toast')
|
||||
toastService.showError('Error toast')
|
||||
@@ -54,4 +81,29 @@ describe('ToastService', () => {
|
||||
expect(toasts).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
it('clears all toasts on clearToasts', () => {
|
||||
toastService.showInfo('Info toast')
|
||||
toastService.showError('Error toast')
|
||||
toastService.clearToasts()
|
||||
|
||||
toastService.getToasts().subscribe((toasts) => {
|
||||
expect(toasts).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
it('suppresses popup toasts if suppressPopupToasts is true', (finish) => {
|
||||
toastService.showToast.subscribe((toast) => {
|
||||
expect(toast).not.toBeNull()
|
||||
})
|
||||
toastService.showInfo('Info toast')
|
||||
|
||||
toastService.showToast.subscribe((toast) => {
|
||||
expect(toast).toBeNull()
|
||||
finish()
|
||||
})
|
||||
|
||||
toastService.suppressPopupToasts = true
|
||||
toastService.showInfo('Info toast')
|
||||
})
|
||||
})
|
||||
|
@@ -1,7 +1,10 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { Subject } from 'rxjs'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
export interface Toast {
|
||||
id?: string
|
||||
|
||||
content: string
|
||||
|
||||
delay: number
|
||||
@@ -22,13 +25,32 @@ export interface Toast {
|
||||
})
|
||||
export class ToastService {
|
||||
constructor() {}
|
||||
_suppressPopupToasts: boolean
|
||||
|
||||
set suppressPopupToasts(value: boolean) {
|
||||
this._suppressPopupToasts = value
|
||||
this.showToast.next(null)
|
||||
}
|
||||
|
||||
private toasts: Toast[] = []
|
||||
|
||||
private toastsSubject: Subject<Toast[]> = new Subject()
|
||||
|
||||
public showToast: Subject<Toast> = new Subject()
|
||||
|
||||
show(toast: Toast) {
|
||||
this.toasts.push(toast)
|
||||
if (!toast.id) {
|
||||
toast.id = uuidv4()
|
||||
}
|
||||
if (typeof toast.error === 'string') {
|
||||
try {
|
||||
toast.error = JSON.parse(toast.error)
|
||||
} catch (e) {}
|
||||
}
|
||||
this.toasts.unshift(toast)
|
||||
if (!this._suppressPopupToasts) {
|
||||
this.showToast.next(toast)
|
||||
}
|
||||
this.toastsSubject.next(this.toasts)
|
||||
}
|
||||
|
||||
@@ -46,7 +68,7 @@ export class ToastService {
|
||||
}
|
||||
|
||||
closeToast(toast: Toast) {
|
||||
let index = this.toasts.findIndex((t) => t == toast)
|
||||
let index = this.toasts.findIndex((t) => t.id == toast.id)
|
||||
if (index > -1) {
|
||||
this.toasts.splice(index, 1)
|
||||
this.toastsSubject.next(this.toasts)
|
||||
@@ -56,4 +78,10 @@ export class ToastService {
|
||||
getToasts() {
|
||||
return this.toastsSubject
|
||||
}
|
||||
|
||||
clearToasts() {
|
||||
this.toasts = []
|
||||
this.toastsSubject.next(this.toasts)
|
||||
this.showToast.next(null)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user