]>
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 { HTMLServerConfig } 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: HTMLServerConfig | |
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.getHTMLConfig() | |
45 | ||
46 | this.buildForm({ | |
47 | fromName: FROM_NAME_VALIDATOR, | |
48 | fromEmail: FROM_EMAIL_VALIDATOR, | |
49 | subject: SUBJECT_VALIDATOR, | |
50 | body: BODY_VALIDATOR | |
51 | }) | |
52 | } | |
53 | ||
54 | show () { | |
55 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) | |
56 | } | |
57 | ||
58 | hide () { | |
59 | this.form.reset() | |
60 | this.error = undefined | |
61 | ||
62 | this.openedModal.close() | |
63 | this.openedModal = null | |
64 | } | |
65 | ||
66 | sendForm () { | |
67 | const fromName = this.form.value['fromName'] | |
68 | const fromEmail = this.form.value[ 'fromEmail' ] | |
69 | const subject = this.form.value[ 'subject' ] | |
70 | const body = this.form.value[ 'body' ] | |
71 | ||
72 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) | |
73 | .subscribe( | |
74 | () => { | |
75 | this.notifier.success($localize`Your message has been sent.`) | |
76 | this.hide() | |
77 | }, | |
78 | ||
79 | err => { | |
80 | this.error = err.status === HttpStatusCode.FORBIDDEN_403 | |
81 | ? $localize`You already sent this form recently` | |
82 | : err.message | |
83 | } | |
84 | ) | |
85 | } | |
86 | } |