2 Component, EventEmitter, Input, OnInit, Output, TemplateRef, ViewChild
3 } from '@angular/core';
4 import { FormControl, FormGroup, FormGroupDirective, Validators } from '@angular/forms';
6 import { BsModalRef, BsModalService } from 'ngx-bootstrap';
7 import { Observable } from 'rxjs/Observable';
9 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
12 selector: 'cd-deletion-link',
13 templateUrl: './deletion-link.component.html',
14 styleUrls: ['./deletion-link.component.scss']
16 export class DeletionLinkComponent implements OnInit {
17 @ViewChild(SubmitButtonComponent) submitButton: SubmitButtonComponent;
18 @Input() metaType: string;
19 @Input() pattern = 'yes';
20 @Input() deletionObserver: () => Observable<any>;
21 @Output() toggleDeletion = new EventEmitter();
22 bsModalRef: BsModalRef;
23 deletionForm: FormGroup;
24 confirmation: FormControl;
27 constructor(public modalService: BsModalService) {}
30 this.confirmation = new FormControl('', {
33 Validators.pattern(this.pattern)
37 this.deletionForm = new FormGroup({
38 confirmation: this.confirmation
42 showModal(template: TemplateRef<any>) {
43 this.deletionForm.reset();
44 this.bsModalRef = this.modalService.show(template);
46 this.submitButton.submit();
50 invalidControl(submitted: boolean, error?: string): boolean {
51 const control = this.confirmation;
53 (submitted || control.dirty) &&
55 (error ? control.errors[error] : true)
59 updateConfirmation($e) {
60 if ($e.key !== 'Enter') {
63 this.confirmation.setValue($e.target.value);
64 this.confirmation.markAsDirty();
65 this.confirmation.updateValueAndValidity();
69 if (this.deletionObserver) {
70 this.deletionObserver().subscribe(
72 () => this.stopLoadingSpinner(),
73 () => this.hideModal()
76 this.toggleDeletion.emit();
81 this.bsModalRef.hide();
84 stopLoadingSpinner() {
85 this.submitButton.loading = false;