All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.shared.alert.service.ts Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-FileCopyrightText: 2017-2024 Enedis
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { ToastrService } from 'ngx-toastr';

@Injectable({
  providedIn: 'root'
})
export class AlertService {

  private successTitle: string = '';
  private infoTitle: string = '';
  private errorTitle: string = '';
  private warningTitle: string = '';

  constructor(
    private toastr: ToastrService,
    private translateService: TranslateService
  ) {
    this.initTranslation();
  }

  success(msg: string, config = {}) {
    this.toastr.success(msg, this.successTitle, config);
  }

  info(msg: string, config = {}) {
    this.toastr.info(msg, this.infoTitle, config);
  }

  error(msg: string, config = {}) {
    this.toastr.error(msg, this.errorTitle, config);
  }

  warning(msg: string, config = {}) {
    this.toastr.warning(msg, this.warningTitle, config);
  }

  removeAll() {
    this.toastr.clear();
  }

  private initTranslation() {
    this.getTranslation();
    this.translateService.onLangChange.subscribe(() => {
        this.getTranslation();
    });
  }

  private getTranslation() {
    this.translateService.get('alert.success').subscribe((res: string) => {
        this.successTitle = res;
    });
    this.translateService.get('alert.info').subscribe((res: string) => {
        this.infoTitle = res;
    });
    this.translateService.get('alert.error').subscribe((res: string) => {
        this.errorTitle = res;
    });
    this.translateService.get('alert.warning').subscribe((res: string) => {
        this.warningTitle = res;
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy