All files / src/app/layout/notification notification.component.ts

75% Statements 18/24
47.05% Branches 8/17
100% Functions 6/6
92.3% Lines 12/13

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 372x 2x 2x   2x 2x                 4x 3x     2x 1x   1x             1x   1x            
import { Component, OnInit } from '@angular/core';
import { NotificationService } from '@core/services/notification.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { AppNotification } from '@core/model/app-notification.interface';
import { ApiError } from '@core/errors/api-error';
import { TranslatorService } from '@core/services/translator.service';
 
@Component({
  selector: 'app-notification',
  standalone: true,
  imports: [],
  templateUrl: './notification.component.html',
  styleUrl: './notification.component.scss'
})
export class NotificationComponent implements OnInit {
  constructor(private notificationService: NotificationService, private matSnackBar: MatSnackBar, private translatorService: TranslatorService) {}
 
  ngOnInit(): void {
    this.notificationService.notification$.subscribe((notification: AppNotification | null) => {
        EIif(notification != null) {
          // in case notification is a confirmation or an information, just display it
          Iif(notification.type != 'error') {
            this.matSnackBar.open(notification.message, $localize `:@@action.close:Close`, {duration: 3000});
          }
          else {
            // display snack bar only if error is not 401 returned from the API
            // when a 401 is returned from the API the notification MUST be handled by a session service or a auth-related component
            // in case of an error we call the translator service to handle backend error codes
            EIif(notification.error == null || !(notification.error instanceof ApiError) || notification.error.httpStatus !== 401) {
              // only ApiError messages are translatable by the TranslatorService
              this.matSnackBar.open((notification.error instanceof ApiError ? this.translatorService.translateError(notification.message) : notification.message), $localize `:@@action.close:Close`, {duration: 3000});
            }
          }
        }
    });
  }
}