UNPKG

first-npm-package-nicule

Version:

This isi first npm package

55 lines (46 loc) 2.14 kB
import { Directive, Input, OnDestroy } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { LabelingService } from 'first-npm-package-nicule/core'; import { TranslateService } from '@ngx-translate/core'; import { HypermediaForm } from '../components'; import { Subscription } from 'rxjs'; @Directive({ selector: 'hm-form' }) export class SuccessMessageDirective implements OnDestroy { @Input() interpolate: any; private actionSuccessSubscription: Subscription; constructor(private snackBar: MatSnackBar, private labelingService: LabelingService, private translate: TranslateService, private hmForm: HypermediaForm) { this.actionSuccessSubscription = this.hmForm.actionSuccess.subscribe(event => this.onSuccess(event)); } ngOnDestroy(): void { if (this.actionSuccessSubscription) { this.actionSuccessSubscription.unsubscribe(); } } onSuccess(event): void { const { properties = {} as any } = event; let { msg } = properties; if (!msg || (msg && msg.indexOf(' ') >= 0)) { const translationKeys = ['successMessage', 'success_message', 'success']; translationKeys.forEach(translationKey => { const untranslated = this.labelingService.form(translationKey, this.hmForm.action.name); const translated = this.translate.instant(untranslated, this.interpolate); if (untranslated !== translated && translated !== undefined && translated !== translationKey) { msg = translated; } }); } else if (msg) { const translated = this.translate.instant(this.labelingService.form(properties.msg, this.hmForm.action.name), this.interpolate); if (translated !== properties.msg && translated !== undefined) { msg = translated; } } if (msg) { this.snackBar.open(msg, undefined, { duration: 4000 }); } } }