]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } from '@angular/core' | |
2 | import { Notifier, ServerService } from '@app/core' | |
3 | import { | |
4 | BODY_VALIDATOR, | |
5 | FROM_EMAIL_VALIDATOR, | |
6 | FROM_NAME_VALIDATOR, | |
7 | SUBJECT_VALIDATOR | |
8 | } from '@app/shared/form-validators/instance-validators' | |
9 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
10 | import { InstanceService } from '@app/shared/shared-instance' | |
11 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
12 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
13 | import { ServerConfig } from '@shared/models' | |
14 | ||
15 | @Component({ | |
16 | selector: 'my-contact-admin-modal', | |
17 | templateUrl: './contact-admin-modal.component.html', | |
18 | styleUrls: [ './contact-admin-modal.component.scss' ] | |
19 | }) | |
20 | export class ContactAdminModalComponent extends FormReactive implements OnInit { | |
21 | @ViewChild('modal', { static: true }) modal: NgbModal | |
22 | ||
23 | error: string | |
24 | ||
25 | private openedModal: NgbModalRef | |
26 | private serverConfig: ServerConfig | |
27 | ||
28 | constructor ( | |
29 | protected formValidatorService: FormValidatorService, | |
30 | private modalService: NgbModal, | |
31 | private instanceService: InstanceService, | |
32 | private serverService: ServerService, | |
33 | private notifier: Notifier | |
34 | ) { | |
35 | super() | |
36 | } | |
37 | ||
38 | get instanceName () { | |
39 | return this.serverConfig.instance.name | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | this.serverConfig = this.serverService.getTmpConfig() | |
44 | this.serverService.getConfig() | |
45 | .subscribe(config => this.serverConfig = config) | |
46 | ||
47 | this.buildForm({ | |
48 | fromName: FROM_NAME_VALIDATOR, | |
49 | fromEmail: FROM_EMAIL_VALIDATOR, | |
50 | subject: SUBJECT_VALIDATOR, | |
51 | body: BODY_VALIDATOR | |
52 | }) | |
53 | } | |
54 | ||
55 | show () { | |
56 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) | |
57 | } | |
58 | ||
59 | hide () { | |
60 | this.form.reset() | |
61 | this.error = undefined | |
62 | ||
63 | this.openedModal.close() | |
64 | this.openedModal = null | |
65 | } | |
66 | ||
67 | sendForm () { | |
68 | const fromName = this.form.value['fromName'] | |
69 | const fromEmail = this.form.value[ 'fromEmail' ] | |
70 | const subject = this.form.value[ 'subject' ] | |
71 | const body = this.form.value[ 'body' ] | |
72 | ||
73 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) | |
74 | .subscribe( | |
75 | () => { | |
76 | this.notifier.success($localize`Your message has been sent.`) | |
77 | this.hide() | |
78 | }, | |
79 | ||
80 | err => { | |
81 | this.error = err.status === 403 | |
82 | ? $localize`You already sent this form recently` | |
83 | : err.message | |
84 | } | |
85 | ) | |
86 | } | |
87 | } |