1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { Notifier, ServerService } from '@app/core'
9 } from '@app/shared/form-validators/instance-validators'
10 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
11 import { InstanceService } from '@app/shared/shared-instance'
12 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
13 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
14 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
15 import { HTMLServerConfig } from '@shared/models'
23 selector: 'my-contact-admin-modal',
24 templateUrl: './contact-admin-modal.component.html',
25 styleUrls: [ './contact-admin-modal.component.scss' ]
27 export class ContactAdminModalComponent extends FormReactive implements OnInit {
28 @ViewChild('modal', { static: true }) modal: NgbModal
32 private openedModal: NgbModalRef
33 private serverConfig: HTMLServerConfig
36 protected formValidatorService: FormValidatorService,
37 private router: Router,
38 private modalService: NgbModal,
39 private instanceService: InstanceService,
40 private serverService: ServerService,
41 private notifier: Notifier
47 return this.serverConfig.instance.name
51 this.serverConfig = this.serverService.getHTMLConfig()
54 fromName: FROM_NAME_VALIDATOR,
55 fromEmail: FROM_EMAIL_VALIDATOR,
56 subject: SUBJECT_VALIDATOR,
61 isContactFormEnabled () {
62 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
65 show (prefill: Prefill = {}) {
66 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
68 this.openedModal.shown.subscribe(() => this.prefillForm(prefill))
69 this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance'))
74 this.error = undefined
76 this.openedModal.close()
77 this.openedModal = null
81 const fromName = this.form.value[ 'fromName' ]
82 const fromEmail = this.form.value[ 'fromEmail' ]
83 const subject = this.form.value[ 'subject' ]
84 const body = this.form.value[ 'body' ]
86 this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
89 this.notifier.success($localize`Your message has been sent.`)
94 this.error = err.status === HttpStatusCode.FORBIDDEN_403
95 ? $localize`You already sent this form recently`
101 private prefillForm (prefill: Prefill) {
102 if (prefill.subject) {
103 this.form.get('subject').setValue(prefill.subject)
107 this.form.get('body').setValue(prefill.body)