All files / lib/services/toaster toaster.service.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                          3x 29x                   29x                 18x 18x                     2x 1x        
import { inject, Injectable } from '@angular/core';
import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
import { toasterExtraClasses, TToasterExtraClasses, TToastType } from '@app/client-util';
 
/**
 * Toaster service for user feedback.
 * Use to notify user about server responses in human readable format.
 * Usage example:
 * this.toaster.showToaster(error, 'error', 5000);
 */
@Injectable({
  providedIn: 'root',
})
export class AppToasterService {
  private readonly snackBar = inject(MatSnackBar);
 
  /**
   * Snackbar reference.
   */
  private snackBarRef?: MatSnackBarRef<SimpleSnackBar>;
 
  /**
   * Default toaster duration value.
   */
  private readonly defaultDuration = 7000;
 
  /**
   * Shows snackbar.
   * @param message text message to be displayed
   * @param type event type, colorizes snackbar
   * @param duration snackbar visibility duration in milliseconds
   */
  public showToaster(message: string, type: TToastType = 'primary', duration: number = this.defaultDuration): void {
    const ec: TToasterExtraClasses = toasterExtraClasses(type);
    this.snackBarRef = this.snackBar.open(message, void 0, {
      panelClass: ec,
      verticalPosition: 'bottom',
      duration,
    });
  }
 
  /**
   * Dismisses snackbar.
   */
  public hideToaster(): void {
    if (typeof this.snackBarRef !== 'undefined') {
      this.snackBarRef.dismiss();
    }
  }
}