]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/modal/confirm.component.ts
Correctly unsubscribe on menu destroy
[github/Chocobozzz/PeerTube.git] / client / src / app / modal / confirm.component.ts
CommitLineData
67ed6552 1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
4edee628 2import { HtmlRendererService } from '@app/core'
457bb213 3import { ConfirmService } from '@app/core/confirm/confirm.service'
67ed6552 4import { POP_STATE_MODAL_DISMISS } from '@app/helpers'
63347a0f
C
5import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
6import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
5769e1db 7
5769e1db
C
8@Component({
9 selector: 'my-confirm',
59aa1e5e 10 templateUrl: './confirm.component.html',
1f30a185 11 styleUrls: [ './confirm.component.scss' ]
5769e1db
C
12})
13export class ConfirmComponent implements OnInit {
f36da21e 14 @ViewChild('confirmModal', { static: true }) confirmModal: ElementRef
5769e1db 15
df98563e
C
16 title = ''
17 message = ''
1f30a185
C
18 expectedInputValue = ''
19 inputLabel = ''
20
21 inputValue = ''
22b59e80 22 confirmButtonText = ''
5769e1db 23
d12b40fb
C
24 isPasswordInput = false
25
63347a0f
C
26 private openedModal: NgbModalRef
27
b1d40cff 28 constructor (
63347a0f 29 private modalService: NgbModal,
4edee628 30 private html: HtmlRendererService,
66357162 31 private confirmService: ConfirmService
23db998f 32 ) { }
5769e1db 33
df98563e 34 ngOnInit () {
5769e1db 35 this.confirmService.showConfirm.subscribe(
d12b40fb
C
36 payload => {
37 // Reinit fields
38 this.title = ''
39 this.message = ''
40 this.expectedInputValue = ''
41 this.inputLabel = ''
42 this.inputValue = ''
43 this.confirmButtonText = ''
44 this.isPasswordInput = false
45
46 const { type, title, message, confirmButtonText } = payload
47
df98563e 48 this.title = title
5769e1db 49
d12b40fb
C
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
56 }
1f30a185 57
66357162 58 this.confirmButtonText = confirmButtonText || $localize`Confirm`
22b59e80 59
4edee628
C
60 this.html.toSafeHtml(message)
61 .then(message => {
62 this.message = message
63
64 this.showModal()
65 })
5769e1db 66 }
df98563e 67 )
5769e1db
C
68 }
69
df98563e 70 confirm () {
63347a0f 71 if (this.openedModal) this.openedModal.close()
5769e1db
C
72 }
73
1f30a185
C
74 isConfirmationDisabled () {
75 // No input validation
76 if (!this.inputLabel || !this.expectedInputValue) return false
77
78 return this.expectedInputValue !== this.inputValue
79 }
80
df98563e 81 showModal () {
a7dbc7df 82 this.inputValue = ''
5769e1db 83
54e78847 84 this.openedModal = this.modalService.open(this.confirmModal, { centered: true })
63347a0f
C
85
86 this.openedModal.result
d12b40fb
C
87 .then(() => {
88 this.confirmService.confirmResponse.next({ confirmed: true, value: this.inputValue })
89 })
60c2bc80
C
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) {
d12b40fb 93 this.confirmService.confirmResponse.next({ confirmed: false, value: this.inputValue })
60c2bc80
C
94 }
95 })
5769e1db
C
96 }
97}