]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+about/about-instance/contact-admin-modal.component.ts
Fix input size of contact form on mobile view
[github/Chocobozzz/PeerTube.git] / client / src / app / +about / about-instance / contact-admin-modal.component.ts
CommitLineData
dfca0f5f
K
1import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'
2import { ActivatedRoute, Router, NavigationEnd } from '@angular/router'
3import { Subject } from 'rxjs'
4import { takeUntil, filter } from 'rxjs/operators'
26a008fe 5import { Notifier, ServerService } from '@app/core'
7ed1edbb
C
6import {
7 BODY_VALIDATOR,
8 FROM_EMAIL_VALIDATOR,
9 FROM_NAME_VALIDATOR,
10 SUBJECT_VALIDATOR
11} from '@app/shared/form-validators/instance-validators'
12import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
67ed6552 13import { InstanceService } from '@app/shared/shared-instance'
d3e56c0c
C
14import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
15import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
f2eb23cd 16import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
2989628b 17import { HTMLServerConfig } from '@shared/models'
d3e56c0c
C
18
19@Component({
20 selector: 'my-contact-admin-modal',
21 templateUrl: './contact-admin-modal.component.html',
22 styleUrls: [ './contact-admin-modal.component.scss' ]
23})
dfca0f5f 24export class ContactAdminModalComponent extends FormReactive implements OnInit, OnDestroy {
f36da21e 25 @ViewChild('modal', { static: true }) modal: NgbModal
d3e56c0c
C
26
27 error: string
dfca0f5f
K
28 destroy = new Subject<any>()
29
30 subject: string
d3e56c0c
C
31
32 private openedModal: NgbModalRef
2989628b 33 private serverConfig: HTMLServerConfig
d3e56c0c
C
34
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 private modalService: NgbModal,
d3e56c0c 38 private instanceService: InstanceService,
26a008fe 39 private serverService: ServerService,
dfca0f5f
K
40 private notifier: Notifier,
41 private route: ActivatedRoute,
42 private router: Router
d3e56c0c
C
43 ) {
44 super()
45 }
46
26a008fe 47 get instanceName () {
ba430d75 48 return this.serverConfig.instance.name
26a008fe
C
49 }
50
dfca0f5f
K
51 get isContactFormEnabled () {
52 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
53 }
54
d3e56c0c 55 ngOnInit () {
2989628b 56 this.serverConfig = this.serverService.getHTMLConfig()
ba430d75 57
d3e56c0c 58 this.buildForm({
7ed1edbb
C
59 fromName: FROM_NAME_VALIDATOR,
60 fromEmail: FROM_EMAIL_VALIDATOR,
61 subject: SUBJECT_VALIDATOR,
62 body: BODY_VALIDATOR
d3e56c0c 63 })
dfca0f5f
K
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()
d3e56c0c
C
87 }
88
89 show () {
dfca0f5f
K
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
24e7916c 96 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
dfca0f5f
K
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 })
d3e56c0c
C
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 () {
dfca0f5f 115 const fromName = this.form.value[ 'fromName' ]
d3e56c0c 116 const fromEmail = this.form.value[ 'fromEmail' ]
4e9fa5b7 117 const subject = this.form.value[ 'subject' ]
d3e56c0c
C
118 const body = this.form.value[ 'body' ]
119
4e9fa5b7 120 this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
d3e56c0c
C
121 .subscribe(
122 () => {
66357162 123 this.notifier.success($localize`Your message has been sent.`)
d3e56c0c
C
124 this.hide()
125 },
126
127 err => {
f2eb23cd 128 this.error = err.status === HttpStatusCode.FORBIDDEN_403
66357162 129 ? $localize`You already sent this form recently`
d3e56c0c
C
130 : err.message
131 }
132 )
133 }
dfca0f5f
K
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 }
d3e56c0c 146}