Select Git revision
Jenkinsfile
snackbar.service.ts 1.97 KiB
import { Injectable } from '@angular/core';
import { MatSnackBar, MatSnackBarRef } from '@angular/material/snack-bar';
import { CommandResource, CommandStatus } from '@goofy-client/command-shared';
import { isNil } from 'lodash-es';
import { Subscription } from 'rxjs';
import { SnackbarErrorComponent } from './snackbar-error/snackbar-error.component';
import { SnackbarComponent } from './snackbar.component';
@Injectable({ providedIn: 'root' })
export class SnackBarService {
private subscription: Subscription;
private snackBarRef: MatSnackBarRef<SnackbarComponent | SnackbarErrorComponent>;
private durationTime: number = 10000;
constructor(private snackBar: MatSnackBar) { }
public show(commandResource: CommandResource, message: string, revokeAction?: () => void): void {
if (commandResource.status === CommandStatus.ERROR) {
this.showError(message);
} else {
this.snackBarRef = this.snackBar.openFromComponent(SnackbarComponent, { data: { message, commandResource }, duration: this.durationTime });
}
this.listenToActions(revokeAction);
}
public showError(message: string): void {
this.snackBarRef = this.snackBar.openFromComponent(SnackbarErrorComponent, { data: { message }, duration: this.durationTime });
this.listenOnAfterDismissed();
}
private listenToActions(revokeAction: () => void): void {
this.listenOnAfterDismissed();
this.listenOnRevokeAction(revokeAction);
}
listenOnRevokeAction(revokeAction: () => void): void {
this.subscription.add(this.snackBarRef.onAction().subscribe(() => {
revokeAction();
this.closeSnackBar();
}));
}
listenOnAfterDismissed(): void {
this.subscription = this.snackBarRef.afterDismissed().subscribe(() => this.closeSnackBar());
}
private closeSnackBar(): void {
this.snackBarRef.dismiss();
this.unsubscribe();
}
private unsubscribe(): void {
if (!isNil(this.subscription)) this.subscription.unsubscribe();
}
}
export interface SnackBarData {
message: string,
commandResource?: CommandResource
}