Skip to content
Snippets Groups Projects
Select Git revision
  • OZG-7689_erweitern
  • main default protected
  • OZG-8483_basicauth
  • release
  • OZG-7679_dateien_uebertragen
  • 0.9.0
  • 0.8.0
  • 0.7.0
  • 0.6.0
  • 0.5.0
  • 0.4.1
  • 0.4.0
  • 0.3.0
  • 0.2.0
  • 0.1.0
  • 0.0.1
16 results

Jenkinsfile

Blame
  • 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
    }