Refactor videos selection components
[oweals/peertube.git] / client / src / app / videos / +video-watch / modal / video-report.component.ts
1 import { Component, Input, OnInit, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { FormReactive, VideoAbuseService } from '../../../shared/index'
4 import { VideoDetails } from '../../../shared/video/video-details.model'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
7 import { VideoAbuseValidatorsService } from '@app/shared/forms/form-validators/video-abuse-validators.service'
8 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
10
11 @Component({
12   selector: 'my-video-report',
13   templateUrl: './video-report.component.html',
14   styleUrls: [ './video-report.component.scss' ]
15 })
16 export class VideoReportComponent extends FormReactive implements OnInit {
17   @Input() video: VideoDetails = null
18
19   @ViewChild('modal') modal: NgbModal
20
21   error: string = null
22
23   private openedModal: NgbModalRef
24
25   constructor (
26     protected formValidatorService: FormValidatorService,
27     private modalService: NgbModal,
28     private videoAbuseValidatorsService: VideoAbuseValidatorsService,
29     private videoAbuseService: VideoAbuseService,
30     private notifier: Notifier,
31     private i18n: I18n
32   ) {
33     super()
34   }
35
36   get currentHost () {
37     return window.location.host
38   }
39
40   get originHost () {
41     if (this.isRemoteVideo()) {
42       return this.video.account.host
43     }
44
45     return ''
46   }
47
48   ngOnInit () {
49     this.buildForm({
50       reason: this.videoAbuseValidatorsService.VIDEO_ABUSE_REASON
51     })
52   }
53
54   show () {
55     this.openedModal = this.modalService.open(this.modal, { keyboard: false })
56   }
57
58   hide () {
59     this.openedModal.close()
60     this.openedModal = null
61   }
62
63   report () {
64     const reason = this.form.value['reason']
65
66     this.videoAbuseService.reportVideo(this.video.id, reason)
67                           .subscribe(
68                             () => {
69                               this.notifier.success(this.i18n('Video reported.'))
70                               this.hide()
71                             },
72
73                             err => this.notifier.error(err.message)
74                            )
75   }
76
77   isRemoteVideo () {
78     return !this.video.isLocal
79   }
80 }