]>
Commit | Line | Data |
---|---|---|
d3e56c0c | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
26a008fe | 2 | import { Notifier, ServerService } from '@app/core' |
d3e56c0c C |
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' | |
ba430d75 | 9 | import { ServerConfig } from '@shared/models' |
d3e56c0c C |
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 { | |
f36da21e | 17 | @ViewChild('modal', { static: true }) modal: NgbModal |
d3e56c0c C |
18 | |
19 | error: string | |
20 | ||
21 | private openedModal: NgbModalRef | |
ba430d75 | 22 | private serverConfig: ServerConfig |
d3e56c0c C |
23 | |
24 | constructor ( | |
25 | protected formValidatorService: FormValidatorService, | |
26 | private modalService: NgbModal, | |
27 | private instanceValidatorsService: InstanceValidatorsService, | |
28 | private instanceService: InstanceService, | |
26a008fe | 29 | private serverService: ServerService, |
d3e56c0c C |
30 | private notifier: Notifier, |
31 | private i18n: I18n | |
32 | ) { | |
33 | super() | |
34 | } | |
35 | ||
26a008fe | 36 | get instanceName () { |
ba430d75 | 37 | return this.serverConfig.instance.name |
26a008fe C |
38 | } |
39 | ||
d3e56c0c | 40 | ngOnInit () { |
ba430d75 C |
41 | this.serverConfig = this.serverService.getTmpConfig() |
42 | this.serverService.getConfig() | |
43 | .subscribe(config => this.serverConfig = config) | |
44 | ||
d3e56c0c C |
45 | this.buildForm({ |
46 | fromName: this.instanceValidatorsService.FROM_NAME, | |
47 | fromEmail: this.instanceValidatorsService.FROM_EMAIL, | |
4e9fa5b7 | 48 | subject: this.instanceValidatorsService.SUBJECT, |
d3e56c0c C |
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' ] | |
4e9fa5b7 | 68 | const subject = this.form.value[ 'subject' ] |
d3e56c0c C |
69 | const body = this.form.value[ 'body' ] |
70 | ||
4e9fa5b7 | 71 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) |
d3e56c0c C |
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 | } |