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'
6 import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants'
9 selector: 'my-confirm',
10 templateUrl: './confirm.component.html',
11 styleUrls: [ './confirm.component.scss' ]
13 export class ConfirmComponent implements OnInit {
14 @ViewChild('confirmModal', { static: true }) confirmModal: ElementRef
18 expectedInputValue = ''
22 confirmButtonText = ''
24 private openedModal: NgbModalRef
27 private modalService: NgbModal,
28 private confirmService: ConfirmService,
33 this.confirmService.showConfirm.subscribe(
34 ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => {
36 this.message = message
38 this.inputLabel = inputLabel
39 this.expectedInputValue = expectedInputValue
41 this.confirmButtonText = confirmButtonText || this.i18n('Confirm')
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, { centered: true })
64 this.openedModal.result
65 .then(() => this.confirmService.confirmResponse.next(true))
66 .catch((reason: string) => {
67 // If the reason was that the user used the back button, we don't care about the confirm dialog result
68 if (!reason || reason !== POP_STATE_MODAL_DISMISS) {
69 this.confirmService.confirmResponse.next(false)