]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
457bb213 | 2 | import { ConfirmService } from '@app/core/confirm/confirm.service' |
67ed6552 | 3 | import { POP_STATE_MODAL_DISMISS } from '@app/helpers' |
63347a0f C |
4 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
5769e1db | 6 | |
5769e1db C |
7 | @Component({ |
8 | selector: 'my-confirm', | |
59aa1e5e | 9 | templateUrl: './confirm.component.html', |
1f30a185 | 10 | styleUrls: [ './confirm.component.scss' ] |
5769e1db C |
11 | }) |
12 | export class ConfirmComponent implements OnInit { | |
f36da21e | 13 | @ViewChild('confirmModal', { static: true }) confirmModal: ElementRef |
5769e1db | 14 | |
df98563e C |
15 | title = '' |
16 | message = '' | |
1f30a185 C |
17 | expectedInputValue = '' |
18 | inputLabel = '' | |
19 | ||
20 | inputValue = '' | |
22b59e80 | 21 | confirmButtonText = '' |
5769e1db | 22 | |
63347a0f C |
23 | private openedModal: NgbModalRef |
24 | ||
b1d40cff | 25 | constructor ( |
63347a0f | 26 | private modalService: NgbModal, |
66357162 | 27 | private confirmService: ConfirmService |
23db998f | 28 | ) { } |
5769e1db | 29 | |
df98563e | 30 | ngOnInit () { |
5769e1db | 31 | this.confirmService.showConfirm.subscribe( |
22b59e80 | 32 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { |
df98563e C |
33 | this.title = title |
34 | this.message = message | |
5769e1db | 35 | |
1f30a185 C |
36 | this.inputLabel = inputLabel |
37 | this.expectedInputValue = expectedInputValue | |
38 | ||
66357162 | 39 | this.confirmButtonText = confirmButtonText || $localize`Confirm` |
22b59e80 | 40 | |
df98563e | 41 | this.showModal() |
5769e1db | 42 | } |
df98563e | 43 | ) |
5769e1db C |
44 | } |
45 | ||
df98563e | 46 | confirm () { |
63347a0f | 47 | if (this.openedModal) this.openedModal.close() |
5769e1db C |
48 | } |
49 | ||
1f30a185 C |
50 | isConfirmationDisabled () { |
51 | // No input validation | |
52 | if (!this.inputLabel || !this.expectedInputValue) return false | |
53 | ||
54 | return this.expectedInputValue !== this.inputValue | |
55 | } | |
56 | ||
df98563e | 57 | showModal () { |
a7dbc7df | 58 | this.inputValue = '' |
5769e1db | 59 | |
54e78847 | 60 | this.openedModal = this.modalService.open(this.confirmModal, { centered: true }) |
63347a0f C |
61 | |
62 | this.openedModal.result | |
63 | .then(() => this.confirmService.confirmResponse.next(true)) | |
60c2bc80 C |
64 | .catch((reason: string) => { |
65 | // If the reason was that the user used the back button, we don't care about the confirm dialog result | |
66 | if (!reason || reason !== POP_STATE_MODAL_DISMISS) { | |
67 | this.confirmService.confirmResponse.next(false) | |
68 | } | |
69 | }) | |
5769e1db C |
70 | } |
71 | } |