]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core' | |
2 | import { ActivatedRoute, Router, NavigationEnd } from '@angular/router' | |
3 | import { Subject } from 'rxjs' | |
4 | import { takeUntil, filter } from 'rxjs/operators' | |
5 | import { Notifier, ServerService } from '@app/core' | |
6 | import { | |
7 | BODY_VALIDATOR, | |
8 | FROM_EMAIL_VALIDATOR, | |
9 | FROM_NAME_VALIDATOR, | |
10 | SUBJECT_VALIDATOR | |
11 | } from '@app/shared/form-validators/instance-validators' | |
12 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
13 | import { InstanceService } from '@app/shared/shared-instance' | |
14 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
15 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | |
17 | import { HTMLServerConfig } from '@shared/models' | |
18 | ||
19 | @Component({ | |
20 | selector: 'my-contact-admin-modal', | |
21 | templateUrl: './contact-admin-modal.component.html', | |
22 | styleUrls: [ './contact-admin-modal.component.scss' ] | |
23 | }) | |
24 | export class ContactAdminModalComponent extends FormReactive implements OnInit, OnDestroy { | |
25 | @ViewChild('modal', { static: true }) modal: NgbModal | |
26 | ||
27 | error: string | |
28 | destroy = new Subject<any>() | |
29 | ||
30 | subject: string | |
31 | ||
32 | private openedModal: NgbModalRef | |
33 | private serverConfig: HTMLServerConfig | |
34 | ||
35 | constructor ( | |
36 | protected formValidatorService: FormValidatorService, | |
37 | private modalService: NgbModal, | |
38 | private instanceService: InstanceService, | |
39 | private serverService: ServerService, | |
40 | private notifier: Notifier, | |
41 | private route: ActivatedRoute, | |
42 | private router: Router | |
43 | ) { | |
44 | super() | |
45 | } | |
46 | ||
47 | get instanceName () { | |
48 | return this.serverConfig.instance.name | |
49 | } | |
50 | ||
51 | get isContactFormEnabled () { | |
52 | return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled | |
53 | } | |
54 | ||
55 | ngOnInit () { | |
56 | this.serverConfig = this.serverService.getHTMLConfig() | |
57 | ||
58 | this.buildForm({ | |
59 | fromName: FROM_NAME_VALIDATOR, | |
60 | fromEmail: FROM_EMAIL_VALIDATOR, | |
61 | subject: SUBJECT_VALIDATOR, | |
62 | body: BODY_VALIDATOR | |
63 | }) | |
64 | ||
65 | // Direct access | |
66 | if (/^\/about\/instance\/contact/.test(this.router.url)) { | |
67 | this.show() | |
68 | this.prefillForm() | |
69 | } | |
70 | ||
71 | // Router access | |
72 | this.router.events | |
73 | .pipe( | |
74 | takeUntil(this.destroy), | |
75 | filter(event => event instanceof NavigationEnd) | |
76 | ) | |
77 | .subscribe((event: NavigationEnd) => { | |
78 | if (/^\/about\/instance\/contact/.test(event.url)) { | |
79 | this.show() | |
80 | this.prefillForm() | |
81 | } | |
82 | }) | |
83 | } | |
84 | ||
85 | ngOnDestroy () { | |
86 | this.destroy.next() | |
87 | } | |
88 | ||
89 | show () { | |
90 | // If contactForm not enabled redirect to 404 | |
91 | if (!this.isContactFormEnabled) { | |
92 | return this.router.navigate([ '/404' ], { state: { type: 'other', obj: { status: 404 } }, skipLocationChange: true }) | |
93 | } | |
94 | ||
95 | // Open modal | |
96 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) | |
97 | ||
98 | // Go back to /about/instance after the modal is closed | |
99 | this.openedModal.result.then(() => { | |
100 | this.router.navigateByUrl('/about/instance') | |
101 | }, () => { | |
102 | this.router.navigateByUrl('/about/instance') | |
103 | }) | |
104 | } | |
105 | ||
106 | hide () { | |
107 | this.form.reset() | |
108 | this.error = undefined | |
109 | ||
110 | this.openedModal.close() | |
111 | this.openedModal = null | |
112 | } | |
113 | ||
114 | sendForm () { | |
115 | const fromName = this.form.value[ 'fromName' ] | |
116 | const fromEmail = this.form.value[ 'fromEmail' ] | |
117 | const subject = this.form.value[ 'subject' ] | |
118 | const body = this.form.value[ 'body' ] | |
119 | ||
120 | this.instanceService.contactAdministrator(fromEmail, fromName, subject, body) | |
121 | .subscribe( | |
122 | () => { | |
123 | this.notifier.success($localize`Your message has been sent.`) | |
124 | this.hide() | |
125 | }, | |
126 | ||
127 | err => { | |
128 | this.error = err.status === HttpStatusCode.FORBIDDEN_403 | |
129 | ? $localize`You already sent this form recently` | |
130 | : err.message | |
131 | } | |
132 | ) | |
133 | } | |
134 | ||
135 | private prefillForm () { | |
136 | const { subject, body } = this.route.snapshot.queryParams | |
137 | ||
138 | if (subject) { | |
139 | this.form.get('subject').setValue(subject) | |
140 | } | |
141 | ||
142 | if (body) { | |
143 | this.form.get('body').setValue(body) | |
144 | } | |
145 | } | |
146 | } |