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