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