1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { HtmlRendererService } from '@app/core'
3 import { ConfirmService } from '@app/core/confirm/confirm.service'
4 import { POP_STATE_MODAL_DISMISS } from '@app/helpers'
5 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
6 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
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 isPasswordInput = false
26 private openedModal: NgbModalRef
29 private modalService: NgbModal,
30 private html: HtmlRendererService,
31 private confirmService: ConfirmService
35 this.confirmService.showConfirm.subscribe(
40 this.expectedInputValue = ''
43 this.confirmButtonText = ''
44 this.isPasswordInput = false
46 const { type, title, message, confirmButtonText } = payload
50 if (type === 'confirm-expected-input') {
51 this.inputLabel = payload.inputLabel
52 this.expectedInputValue = payload.expectedInputValue
53 } else if (type === 'confirm-password') {
54 this.inputLabel = $localize`Confirm your password`
55 this.isPasswordInput = true
58 this.confirmButtonText = confirmButtonText || $localize`Confirm`
60 this.html.toSafeHtml(message)
62 this.message = message
71 if (this.openedModal) this.openedModal.close()
74 isConfirmationDisabled () {
75 // No input validation
76 if (!this.inputLabel || !this.expectedInputValue) return false
78 return this.expectedInputValue !== this.inputValue
84 this.openedModal = this.modalService.open(this.confirmModal, { centered: true })
86 this.openedModal.result
88 this.confirmService.confirmResponse.next({ confirmed: true, value: this.inputValue })
90 .catch((reason: string) => {
91 // If the reason was that the user used the back button, we don't care about the confirm dialog result
92 if (!reason || reason !== POP_STATE_MODAL_DISMISS) {
93 this.confirmService.confirmResponse.next({ confirmed: false, value: this.inputValue })