]>
Commit | Line | Data |
---|---|---|
5c16e6bc C |
1 | import { Component, OnInit, ViewChild } from '@angular/core' |
2 | import { Router } from '@angular/router' | |
26a008fe | 3 | import { Notifier, ServerService } from '@app/core' |
7ed1edbb C |
4 | import { |
5 | BODY_VALIDATOR, | |
6 | FROM_EMAIL_VALIDATOR, | |
7 | FROM_NAME_VALIDATOR, | |
8 | SUBJECT_VALIDATOR | |
9 | } from '@app/shared/form-validators/instance-validators' | |
5c5bcea2 | 10 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' |
67ed6552 | 11 | import { InstanceService } from '@app/shared/shared-instance' |
d3e56c0c C |
12 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
13 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
c0e8b12e | 14 | import { HTMLServerConfig, HttpStatusCode } from '@shared/models' |
d3e56c0c | 15 | |
5c16e6bc C |
16 | type Prefill = { |
17 | subject?: string | |
18 | body?: string | |
19 | } | |
20 | ||
d3e56c0c C |
21 | @Component({ |
22 | selector: 'my-contact-admin-modal', | |
23 | templateUrl: './contact-admin-modal.component.html', | |
24 | styleUrls: [ './contact-admin-modal.component.scss' ] | |
25 | }) | |
5c16e6bc | 26 | export class ContactAdminModalComponent extends FormReactive implements OnInit { |
f36da21e | 27 | @ViewChild('modal', { static: true }) modal: NgbModal |
d3e56c0c C |
28 | |
29 | error: string | |
30 | ||
31 | private openedModal: NgbModalRef | |
2989628b | 32 | private serverConfig: HTMLServerConfig |
d3e56c0c C |
33 | |
34 | constructor ( | |
5c5bcea2 | 35 | protected formReactiveService: FormReactiveService, |
5c16e6bc | 36 | private router: Router, |
d3e56c0c | 37 | private modalService: NgbModal, |
d3e56c0c | 38 | private instanceService: InstanceService, |
26a008fe | 39 | private serverService: ServerService, |
5c16e6bc | 40 | private notifier: Notifier |
d3e56c0c C |
41 | ) { |
42 | super() | |
43 | } | |
44 | ||
26a008fe | 45 | get instanceName () { |
ba430d75 | 46 | return this.serverConfig.instance.name |
26a008fe C |
47 | } |
48 | ||
d3e56c0c | 49 | ngOnInit () { |
2989628b | 50 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 51 | |
d3e56c0c | 52 | this.buildForm({ |
7ed1edbb C |
53 | fromName: FROM_NAME_VALIDATOR, |
54 | fromEmail: FROM_EMAIL_VALIDATOR, | |
55 | subject: SUBJECT_VALIDATOR, | |
56 | body: BODY_VALIDATOR | |
d3e56c0c | 57 | }) |
dfca0f5f K |
58 | } |
59 | ||
5c16e6bc C |
60 | isContactFormEnabled () { |
61 | return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled | |
d3e56c0c C |
62 | } |
63 | ||
5c16e6bc | 64 | show (prefill: Prefill = {}) { |
24e7916c | 65 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) |
dfca0f5f | 66 | |
5c16e6bc C |
67 | this.openedModal.shown.subscribe(() => this.prefillForm(prefill)) |
68 | this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance')) | |
d3e56c0c C |
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 () { | |
9df52d66 C |
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'] | |
d3e56c0c | 84 | |
4e9fa5b7 | 85 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) |
1378c0d3 C |
86 | .subscribe({ |
87 | next: () => { | |
66357162 | 88 | this.notifier.success($localize`Your message has been sent.`) |
d3e56c0c C |
89 | this.hide() |
90 | }, | |
91 | ||
1378c0d3 | 92 | error: err => { |
f2eb23cd | 93 | this.error = err.status === HttpStatusCode.FORBIDDEN_403 |
66357162 | 94 | ? $localize`You already sent this form recently` |
d3e56c0c C |
95 | : err.message |
96 | } | |
1378c0d3 | 97 | }) |
d3e56c0c | 98 | } |
dfca0f5f | 99 | |
5c16e6bc C |
100 | private prefillForm (prefill: Prefill) { |
101 | if (prefill.subject) { | |
102 | this.form.get('subject').setValue(prefill.subject) | |
dfca0f5f K |
103 | } |
104 | ||
5c16e6bc C |
105 | if (prefill.body) { |
106 | this.form.get('body').setValue(prefill.body) | |
dfca0f5f K |
107 | } |
108 | } | |
d3e56c0c | 109 | } |