0aa4c32ee5186034be8e2b120e7a8559e8e2cbd7
[oweals/peertube.git] /
1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
4 import { FormReactive, UserService } from '../../../shared/index'
5 import { Video } from '@app/shared/video/video.model'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService, VideoChangeOwnershipValidatorsService } from '@app/shared'
8 import { VideoOwnershipService } from '@app/shared/video-ownership'
9
10 @Component({
11   selector: 'my-video-change-ownership',
12   templateUrl: './video-change-ownership.component.html',
13   styleUrls: [ './video-change-ownership.component.scss' ]
14 })
15 export class VideoChangeOwnershipComponent extends FormReactive implements OnInit {
16   @ViewChild('modal') modal: ElementRef
17
18   usernamePropositions: string[]
19
20   error: string = null
21
22   private video: Video | undefined = undefined
23
24   constructor (
25     protected formValidatorService: FormValidatorService,
26     private videoChangeOwnershipValidatorsService: VideoChangeOwnershipValidatorsService,
27     private videoOwnershipService: VideoOwnershipService,
28     private notificationsService: NotificationsService,
29     private userService: UserService,
30     private modalService: NgbModal,
31     private i18n: I18n
32   ) {
33     super()
34   }
35
36   ngOnInit () {
37     this.buildForm({
38       username: this.videoChangeOwnershipValidatorsService.USERNAME
39     })
40     this.usernamePropositions = []
41   }
42
43   show (video: Video) {
44     this.video = video
45     this.modalService
46       .open(this.modal)
47       .result
48       .then(() => this.changeOwnership())
49       .catch((_) => _) // Called when closing (cancel) the modal without validating, do nothing
50   }
51
52   search (event) {
53     const query = event.query
54     this.userService.autocomplete(query)
55       .subscribe(
56         (usernames) => {
57           this.usernamePropositions = usernames
58         },
59
60         err => this.notificationsService.error('Error', err.message)
61       )
62   }
63
64   changeOwnership () {
65     const username = this.form.value['username']
66
67     this.videoOwnershipService
68       .changeOwnership(this.video.id, username)
69       .subscribe(
70         () => this.notificationsService.success(this.i18n('Success'), this.i18n('Ownership changed.')),
71
72         err => this.notificationsService.error(this.i18n('Error'), err.message)
73       )
74   }
75 }