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