1 import { Component, ElementRef, HostListener, OnInit, ViewChild } from '@angular/core'
2 import { ConfirmService } from '@app/core/confirm/confirm.service'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
8 selector: 'my-confirm',
9 templateUrl: './confirm.component.html',
10 styleUrls: [ './confirm.component.scss' ]
12 export class ConfirmComponent implements OnInit {
13 @ViewChild('confirmModal') confirmModal: ElementRef
17 expectedInputValue = ''
21 confirmButtonText = ''
23 private openedModal: NgbModalRef
26 private modalService: NgbModal,
27 private confirmService: ConfirmService,
32 this.confirmService.showConfirm.subscribe(
33 ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => {
35 this.message = message
37 this.inputLabel = inputLabel
38 this.expectedInputValue = expectedInputValue
40 this.confirmButtonText = confirmButtonText || this.i18n('Confirm')
47 @HostListener('document:keydown.enter')
49 if (this.openedModal) this.openedModal.close()
52 isConfirmationDisabled () {
53 // No input validation
54 if (!this.inputLabel || !this.expectedInputValue) return false
56 return this.expectedInputValue !== this.inputValue
62 this.openedModal = this.modalService.open(this.confirmModal)
64 this.openedModal.result
65 .then(() => this.confirmService.confirmResponse.next(true))
66 .catch(() => this.confirmService.confirmResponse.next(false))