import { Component, OnInit, OnDestroy, Input } from '@angular/core'; import { BsModalRef } from 'ngx-bootstrap/modal'; import { FormGroup, FormBuilder, Validators, AbstractControl, FormArray } from '@angular/forms'; import { NavigationStart, Router } from '@angular/router'; import { Subject } from 'rxjs'; import { filter, takeUntil } from 'rxjs/operators'; @Component({ selector: 'app-prompt-dialog', templateUrl: './prompt-dialog.component.html', styleUrls: ['./prompt-dialog.component.scss'] }) export class PromptDialogComponent implements OnInit, OnDestroy { @Input() title: string; @Input() prompt: string; @Input() saveButtonText = 'Save changes'; @Input() value: string; @Input() placeholder: string; @Input() required: boolean; save = new Subject(); editorForm: FormGroup; private destroy$ = new Subject(); // ---------------------------------------------------------------------------------------------------------------- constructor(private readonly modalRef: BsModalRef, private readonly router: Router, private readonly fb: FormBuilder) { // When the user navigates away from this route, hide the modal router.events .pipe( takeUntil(this.destroy$), filter(e => e instanceof NavigationStart) ) .subscribe(() => this.modalRef.hide()); } // ---------------------------------------------------------------------------------------------------------------- private createForm() { this.editorForm = this.fb.group( { value: [this.value] }); if (this.required) this.editorForm.get('value').setValidators(Validators.required); } // ---------------------------------------------------------------------------------------------------------------- close() { this.modalRef.hide(); } // ---------------------------------------------------------------------------------------------------------------- saveChanges() { this.save.next(this.editorForm.get('value').value); this.modalRef.hide(); } // ---------------------------------------------------------------------------------------------------------------- ngOnInit() { this.createForm(); } // ---------------------------------------------------------------------------------------------------------------- ngOnDestroy() { this.destroy$.next(); } }