PeerTube/client/src/app/modal/confirm.component.ts

105 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { HtmlRendererService } from '@app/core'
import { ConfirmService } from '@app/core/confirm/confirm.service'
2020-06-23 14:10:17 +02:00
import { POP_STATE_MODAL_DISMISS } from '@app/helpers'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
@Component({
selector: 'my-confirm',
2017-12-07 10:33:16 +01:00
templateUrl: './confirm.component.html',
styleUrls: [ './confirm.component.scss' ]
})
export class ConfirmComponent implements OnInit {
2019-07-24 16:05:59 +02:00
@ViewChild('confirmModal', { static: true }) confirmModal: ElementRef
title = ''
message = ''
expectedInputValue = ''
inputLabel = ''
inputValue = ''
confirmButtonText = ''
errorMessage = ''
2022-10-07 11:06:28 +02:00
isPasswordInput = false
private openedModal: NgbModalRef
2018-06-04 16:21:17 +02:00
constructor (
private modalService: NgbModal,
private html: HtmlRendererService,
private confirmService: ConfirmService
2018-09-22 20:11:16 +02:00
) { }
ngOnInit () {
this.confirmService.showConfirm.subscribe(
2022-10-07 11:06:28 +02:00
payload => {
// Reinit fields
this.title = ''
this.message = ''
this.expectedInputValue = ''
this.inputLabel = ''
this.inputValue = ''
this.confirmButtonText = ''
this.isPasswordInput = false
this.errorMessage = ''
2022-10-07 11:06:28 +02:00
const { type, title, message, confirmButtonText, errorMessage } = payload
2022-10-07 11:06:28 +02:00
this.title = title
2022-10-07 11:06:28 +02:00
if (type === 'confirm-expected-input') {
this.inputLabel = payload.inputLabel
this.expectedInputValue = payload.expectedInputValue
} else if (type === 'confirm-password') {
this.inputLabel = $localize`Confirm your password`
this.isPasswordInput = true
this.errorMessage = errorMessage
2022-10-07 11:06:28 +02:00
}
this.confirmButtonText = confirmButtonText || $localize`Confirm`
this.html.toSafeHtml(message)
.then(html => {
this.message = html
this.showModal()
})
}
)
}
confirm () {
if (this.openedModal) this.openedModal.close()
}
isConfirmationDisabled () {
// No input validation
if (!this.inputLabel || !this.expectedInputValue) return false
return this.expectedInputValue !== this.inputValue
}
hasError () {
return this.errorMessage
}
showModal () {
2018-07-17 11:06:04 +02:00
this.inputValue = ''
this.openedModal = this.modalService.open(this.confirmModal, { centered: true })
this.openedModal.result
2022-10-07 11:06:28 +02:00
.then(() => {
this.confirmService.confirmResponse.next({ confirmed: true, value: this.inputValue })
})
.catch((reason: string) => {
// If the reason was that the user used the back button, we don't care about the confirm dialog result
if (!reason || reason !== POP_STATE_MODAL_DISMISS) {
2022-10-07 11:06:28 +02:00
this.confirmService.confirmResponse.next({ confirmed: false, value: this.inputValue })
}
})
}
}