aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+about/about-instance/contact-admin-modal.component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/+about/about-instance/contact-admin-modal.component.ts')
-rw-r--r--client/src/app/+about/about-instance/contact-admin-modal.component.ts76
1 files changed, 20 insertions, 56 deletions
diff --git a/client/src/app/+about/about-instance/contact-admin-modal.component.ts b/client/src/app/+about/about-instance/contact-admin-modal.component.ts
index 32812d9e0..a528faa20 100644
--- a/client/src/app/+about/about-instance/contact-admin-modal.component.ts
+++ b/client/src/app/+about/about-instance/contact-admin-modal.component.ts
@@ -1,7 +1,5 @@
1import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core' 1import { Component, OnInit, ViewChild } from '@angular/core'
2import { ActivatedRoute, Router, NavigationEnd } from '@angular/router' 2import { Router } from '@angular/router'
3import { Subject } from 'rxjs'
4import { takeUntil, filter } from 'rxjs/operators'
5import { Notifier, ServerService } from '@app/core' 3import { Notifier, ServerService } from '@app/core'
6import { 4import {
7 BODY_VALIDATOR, 5 BODY_VALIDATOR,
@@ -16,30 +14,31 @@ import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
16import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' 14import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
17import { HTMLServerConfig } from '@shared/models' 15import { HTMLServerConfig } from '@shared/models'
18 16
17type Prefill = {
18 subject?: string
19 body?: string
20}
21
19@Component({ 22@Component({
20 selector: 'my-contact-admin-modal', 23 selector: 'my-contact-admin-modal',
21 templateUrl: './contact-admin-modal.component.html', 24 templateUrl: './contact-admin-modal.component.html',
22 styleUrls: [ './contact-admin-modal.component.scss' ] 25 styleUrls: [ './contact-admin-modal.component.scss' ]
23}) 26})
24export class ContactAdminModalComponent extends FormReactive implements OnInit, OnDestroy { 27export class ContactAdminModalComponent extends FormReactive implements OnInit {
25 @ViewChild('modal', { static: true }) modal: NgbModal 28 @ViewChild('modal', { static: true }) modal: NgbModal
26 29
27 error: string 30 error: string
28 destroy = new Subject<any>()
29
30 subject: string
31 31
32 private openedModal: NgbModalRef 32 private openedModal: NgbModalRef
33 private serverConfig: HTMLServerConfig 33 private serverConfig: HTMLServerConfig
34 34
35 constructor ( 35 constructor (
36 protected formValidatorService: FormValidatorService, 36 protected formValidatorService: FormValidatorService,
37 private router: Router,
37 private modalService: NgbModal, 38 private modalService: NgbModal,
38 private instanceService: InstanceService, 39 private instanceService: InstanceService,
39 private serverService: ServerService, 40 private serverService: ServerService,
40 private notifier: Notifier, 41 private notifier: Notifier
41 private route: ActivatedRoute,
42 private router: Router
43 ) { 42 ) {
44 super() 43 super()
45 } 44 }
@@ -48,10 +47,6 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
48 return this.serverConfig.instance.name 47 return this.serverConfig.instance.name
49 } 48 }
50 49
51 get isContactFormEnabled () {
52 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
53 }
54
55 ngOnInit () { 50 ngOnInit () {
56 this.serverConfig = this.serverService.getHTMLConfig() 51 this.serverConfig = this.serverService.getHTMLConfig()
57 52
@@ -61,46 +56,17 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
61 subject: SUBJECT_VALIDATOR, 56 subject: SUBJECT_VALIDATOR,
62 body: BODY_VALIDATOR 57 body: BODY_VALIDATOR
63 }) 58 })
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 } 59 }
84 60
85 ngOnDestroy () { 61 isContactFormEnabled () {
86 this.destroy.next() 62 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
87 } 63 }
88 64
89 show () { 65 show (prefill: Prefill = {}) {
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 }) 66 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
97 67
98 // Go back to /about/instance after the modal is closed 68 this.openedModal.shown.subscribe(() => this.prefillForm(prefill))
99 this.openedModal.result.then(() => { 69 this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance'))
100 this.router.navigateByUrl('/about/instance')
101 }, () => {
102 this.router.navigateByUrl('/about/instance')
103 })
104 } 70 }
105 71
106 hide () { 72 hide () {
@@ -132,15 +98,13 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
132 ) 98 )
133 } 99 }
134 100
135 private prefillForm () { 101 private prefillForm (prefill: Prefill) {
136 const { subject, body } = this.route.snapshot.queryParams 102 if (prefill.subject) {
137 103 this.form.get('subject').setValue(prefill.subject)
138 if (subject) {
139 this.form.get('subject').setValue(subject)
140 } 104 }
141 105
142 if (body) { 106 if (prefill.body) {
143 this.form.get('body').setValue(body) 107 this.form.get('body').setValue(prefill.body)
144 } 108 }
145 } 109 }
146} 110}