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