PeerTube/client/src/app/+about/about-instance/contact-admin-modal.compone...

110 lines
3.0 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
2019-01-10 11:51:25 +01:00
import { Notifier, ServerService } from '@app/core'
import {
BODY_VALIDATOR,
FROM_EMAIL_VALIDATOR,
FROM_NAME_VALIDATOR,
SUBJECT_VALIDATOR
} from '@app/shared/form-validators/instance-validators'
2022-10-07 15:26:53 +02:00
import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
2020-06-23 14:10:17 +02:00
import { InstanceService } from '@app/shared/shared-instance'
2019-01-10 11:12:41 +01:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
import { HTMLServerConfig, HttpStatusCode } from '@peertube/peertube-models'
2019-01-10 11:12:41 +01:00
type Prefill = {
subject?: string
body?: string
}
2019-01-10 11:12:41 +01:00
@Component({
selector: 'my-contact-admin-modal',
templateUrl: './contact-admin-modal.component.html',
styleUrls: [ './contact-admin-modal.component.scss' ]
})
export class ContactAdminModalComponent extends FormReactive implements OnInit {
2019-07-24 16:05:59 +02:00
@ViewChild('modal', { static: true }) modal: NgbModal
2019-01-10 11:12:41 +01:00
error: string
private openedModal: NgbModalRef
2021-06-04 13:31:41 +02:00
private serverConfig: HTMLServerConfig
2019-01-10 11:12:41 +01:00
constructor (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
private router: Router,
2019-01-10 11:12:41 +01:00
private modalService: NgbModal,
private instanceService: InstanceService,
2019-01-10 11:51:25 +01:00
private serverService: ServerService,
private notifier: Notifier
2019-01-10 11:12:41 +01:00
) {
super()
}
2019-01-10 11:51:25 +01:00
get instanceName () {
2019-12-18 15:31:54 +01:00
return this.serverConfig.instance.name
2019-01-10 11:51:25 +01:00
}
2019-01-10 11:12:41 +01:00
ngOnInit () {
2021-06-04 13:31:41 +02:00
this.serverConfig = this.serverService.getHTMLConfig()
2019-12-18 15:31:54 +01:00
2019-01-10 11:12:41 +01:00
this.buildForm({
fromName: FROM_NAME_VALIDATOR,
fromEmail: FROM_EMAIL_VALIDATOR,
subject: SUBJECT_VALIDATOR,
body: BODY_VALIDATOR
2019-01-10 11:12:41 +01:00
})
}
isContactFormEnabled () {
return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
2019-01-10 11:12:41 +01:00
}
show (prefill: Prefill = {}) {
this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
this.openedModal.shown.subscribe(() => this.prefillForm(prefill))
this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance'))
2019-01-10 11:12:41 +01:00
}
hide () {
this.form.reset()
this.error = undefined
this.openedModal.close()
this.openedModal = null
}
sendForm () {
2021-08-17 14:42:53 +02:00
const fromName = this.form.value['fromName']
const fromEmail = this.form.value['fromEmail']
const subject = this.form.value['subject']
const body = this.form.value['body']
2019-01-10 11:12:41 +01:00
this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`Your message has been sent.`)
2019-01-10 11:12:41 +01:00
this.hide()
},
2021-08-17 11:27:47 +02:00
error: err => {
this.error = err.status === HttpStatusCode.FORBIDDEN_403
? $localize`You already sent this form recently`
2019-01-10 11:12:41 +01:00
: err.message
}
2021-08-17 11:27:47 +02:00
})
2019-01-10 11:12:41 +01:00
}
private prefillForm (prefill: Prefill) {
if (prefill.subject) {
this.form.get('subject').setValue(prefill.subject)
}
if (prefill.body) {
this.form.get('body').setValue(prefill.body)
}
}
2019-01-10 11:12:41 +01:00
}