1 import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router, NavigationEnd } from '@angular/router'
3 import { Subject } from 'rxjs'
4 import { takeUntil, filter } from 'rxjs/operators'
5 import { Notifier, ServerService } from '@app/core'
11 } from '@app/shared/form-validators/instance-validators'
12 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
13 import { InstanceService } from '@app/shared/shared-instance'
14 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
15 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
16 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
17 import { HTMLServerConfig } from '@shared/models'
20 selector: 'my-contact-admin-modal',
21 templateUrl: './contact-admin-modal.component.html',
22 styleUrls: [ './contact-admin-modal.component.scss' ]
24 export class ContactAdminModalComponent extends FormReactive implements OnInit, OnDestroy {
25 @ViewChild('modal', { static: true }) modal: NgbModal
28 destroy = new Subject<any>()
32 private openedModal: NgbModalRef
33 private serverConfig: HTMLServerConfig
36 protected formValidatorService: FormValidatorService,
37 private modalService: NgbModal,
38 private instanceService: InstanceService,
39 private serverService: ServerService,
40 private notifier: Notifier,
41 private route: ActivatedRoute,
42 private router: Router
48 return this.serverConfig.instance.name
51 get isContactFormEnabled () {
52 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
56 this.serverConfig = this.serverService.getHTMLConfig()
59 fromName: FROM_NAME_VALIDATOR,
60 fromEmail: FROM_EMAIL_VALIDATOR,
61 subject: SUBJECT_VALIDATOR,
66 if (/^\/about\/instance\/contact/.test(this.router.url)) {
74 takeUntil(this.destroy),
75 filter(event => event instanceof NavigationEnd)
77 .subscribe((event: NavigationEnd) => {
78 if (/^\/about\/instance\/contact/.test(event.url)) {
90 // If contactForm not enabled redirect to 404
91 if (!this.isContactFormEnabled) {
92 return this.router.navigate([ '/404' ], { state: { type: 'other', obj: { status: 404 } }, skipLocationChange: true })
96 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
98 // Go back to /about/instance after the modal is closed
99 this.openedModal.result.then(() => {
100 this.router.navigateByUrl('/about/instance')
102 this.router.navigateByUrl('/about/instance')
108 this.error = undefined
110 this.openedModal.close()
111 this.openedModal = null
115 const fromName = this.form.value[ 'fromName' ]
116 const fromEmail = this.form.value[ 'fromEmail' ]
117 const subject = this.form.value[ 'subject' ]
118 const body = this.form.value[ 'body' ]
120 this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
123 this.notifier.success($localize`Your message has been sent.`)
128 this.error = err.status === HttpStatusCode.FORBIDDEN_403
129 ? $localize`You already sent this form recently`
135 private prefillForm () {
136 const { subject, body } = this.route.snapshot.queryParams
139 this.form.get('subject').setValue(subject)
143 this.form.get('body').setValue(body)