5199402e6f636be6dcae9b77a135d46050bc92db
[oweals/peertube.git] / client / src / app / +about / about-instance / contact-admin-modal.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { FormReactive, FormValidatorService, InstanceValidatorsService } from '@app/shared/shared-forms'
4 import { InstanceService } from '@app/shared/shared-instance'
5 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
6 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { ServerConfig } from '@shared/models'
9
10 @Component({
11   selector: 'my-contact-admin-modal',
12   templateUrl: './contact-admin-modal.component.html',
13   styleUrls: [ './contact-admin-modal.component.scss' ]
14 })
15 export class ContactAdminModalComponent extends FormReactive implements OnInit {
16   @ViewChild('modal', { static: true }) modal: NgbModal
17
18   error: string
19
20   private openedModal: NgbModalRef
21   private serverConfig: ServerConfig
22
23   constructor (
24     protected formValidatorService: FormValidatorService,
25     private modalService: NgbModal,
26     private instanceValidatorsService: InstanceValidatorsService,
27     private instanceService: InstanceService,
28     private serverService: ServerService,
29     private notifier: Notifier,
30     private i18n: I18n
31   ) {
32     super()
33   }
34
35   get instanceName () {
36     return this.serverConfig.instance.name
37   }
38
39   ngOnInit () {
40     this.serverConfig = this.serverService.getTmpConfig()
41     this.serverService.getConfig()
42         .subscribe(config => this.serverConfig = config)
43
44     this.buildForm({
45       fromName: this.instanceValidatorsService.FROM_NAME,
46       fromEmail: this.instanceValidatorsService.FROM_EMAIL,
47       subject: this.instanceValidatorsService.SUBJECT,
48       body: this.instanceValidatorsService.BODY
49     })
50   }
51
52   show () {
53     this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
54   }
55
56   hide () {
57     this.form.reset()
58     this.error = undefined
59
60     this.openedModal.close()
61     this.openedModal = null
62   }
63
64   sendForm () {
65     const fromName = this.form.value['fromName']
66     const fromEmail = this.form.value[ 'fromEmail' ]
67     const subject = this.form.value[ 'subject' ]
68     const body = this.form.value[ 'body' ]
69
70     this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
71         .subscribe(
72           () => {
73             this.notifier.success(this.i18n('Your message has been sent.'))
74             this.hide()
75           },
76
77           err => {
78             this.error = err.status === 403
79               ? this.i18n('You already sent this form recently')
80               : err.message
81           }
82         )
83   }
84 }