File

src/app/shared/helper.service.ts

Index

Methods

Constructor

constructor(snackBar: MatSnackBar, dialog: MatDialog)
Parameters :
Name Type Optional
snackBar MatSnackBar No
dialog MatDialog No

Methods

Private parseMessage
parseMessage(message: string | object)
Parameters :
Name Type Optional
message string | object No
Returns : any
showConfirmation
showConfirmation(message: string | object, title?: string, confirmButtonText?: string)
Parameters :
Name Type Optional
message string | object No
title string Yes
confirmButtonText string Yes
Returns : any
showError
showError(message: string | object)
Parameters :
Name Type Optional
message string | object No
Returns : void
showSnackBar
showSnackBar(message: string | object)
Parameters :
Name Type Optional
message string | object No
Returns : void
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';

import { AlertDialogComponent } from './dialog/alert-dialog/alert-dialog.component';
import { ConfirmDialogComponent } from './dialog/confirm-dialog/confirm-dialog.component';

@Injectable()
export class HelperService {
  constructor(protected snackBar: MatSnackBar, protected dialog: MatDialog) {}

  private parseMessage(message: string | object) {
    return typeof message === 'string'
      ? message
      : JSON.stringify(message, null, 2);
  }

  showError(message: string | object) {
    this.dialog.open(AlertDialogComponent, {
      data: {
        title: 'Error',
        message: this.parseMessage(message),
      },
    });
  }

  showSnackBar(message: string | object) {
    this.snackBar.open(this.parseMessage(message), 'OK', {
      duration: 2000,
    });
  }

  showConfirmation(
    message: string | object,
    title?: string,
    confirmButtonText?: string
  ) {
    return this.dialog
      .open(ConfirmDialogComponent, {
        data: {
          message: this.parseMessage(message),
          title,
          confirmButtonText,
        },
      })
      .afterClosed()
      .toPromise();
  }
}

results matching ""

    No results matching ""