]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { Notifier, ServerService } from '@app/core' | |
4 | import { | |
5 | BODY_VALIDATOR, | |
6 | FROM_EMAIL_VALIDATOR, | |
7 | FROM_NAME_VALIDATOR, | |
8 | SUBJECT_VALIDATOR | |
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' | |
16 | ||
17 | type Prefill = { | |
18 | subject?: string | |
19 | body?: string | |
20 | } | |
21 | ||
22 | @Component({ | |
23 | selector: 'my-contact-admin-modal', | |
24 | templateUrl: './contact-admin-modal.component.html', | |
25 | styleUrls: [ './contact-admin-modal.component.scss' ] | |
26 | }) | |
27 | export class ContactAdminModalComponent extends FormReactive implements OnInit { | |
28 | @ViewChild('modal', { static: true }) modal: NgbModal | |
29 | ||
30 | error: string | |
31 | ||
32 | private openedModal: NgbModalRef | |
33 | private serverConfig: HTMLServerConfig | |
34 | ||
35 | constructor ( | |
36 | protected formValidatorService: FormValidatorService, | |
37 | private router: Router, | |
38 | private modalService: NgbModal, | |
39 | private instanceService: InstanceService, | |
40 | private serverService: ServerService, | |
41 | private notifier: Notifier | |
42 | ) { | |
43 | super() | |
44 | } | |
45 | ||
46 | get instanceName () { | |
47 | return this.serverConfig.instance.name | |
48 | } | |
49 | ||
50 | ngOnInit () { | |
51 | this.serverConfig = this.serverService.getHTMLConfig() | |
52 | ||
53 | this.buildForm({ | |
54 | fromName: FROM_NAME_VALIDATOR, | |
55 | fromEmail: FROM_EMAIL_VALIDATOR, | |
56 | subject: SUBJECT_VALIDATOR, | |
57 | body: BODY_VALIDATOR | |
58 | }) | |
59 | } | |
60 | ||
61 | isContactFormEnabled () { | |
62 | return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled | |
63 | } | |
64 | ||
65 | show (prefill: Prefill = {}) { | |
66 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) | |
67 | ||
68 | this.openedModal.shown.subscribe(() => this.prefillForm(prefill)) | |
69 | this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance')) | |
70 | } | |
71 | ||
72 | hide () { | |
73 | this.form.reset() | |
74 | this.error = undefined | |
75 | ||
76 | this.openedModal.close() | |
77 | this.openedModal = null | |
78 | } | |
79 | ||
80 | sendForm () { | |
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' ] | |
85 | ||
86 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) | |
87 | .subscribe( | |
88 | () => { | |
89 | this.notifier.success($localize`Your message has been sent.`) | |
90 | this.hide() | |
91 | }, | |
92 | ||
93 | err => { | |
94 | this.error = err.status === HttpStatusCode.FORBIDDEN_403 | |
95 | ? $localize`You already sent this form recently` | |
96 | : err.message | |
97 | } | |
98 | ) | |
99 | } | |
100 | ||
101 | private prefillForm (prefill: Prefill) { | |
102 | if (prefill.subject) { | |
103 | this.form.get('subject').setValue(prefill.subject) | |
104 | } | |
105 | ||
106 | if (prefill.body) { | |
107 | this.form.get('body').setValue(prefill.body) | |
108 | } | |
109 | } | |
110 | } |