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