a68b452ecf49da35c57ae3c8185c9467a253b45c
[oweals/peertube.git] /
1 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { FormReactive } from '@app/shared'
4 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
5 import { VideoOwnershipService } from '@app/shared/video-ownership'
6 import { VideoChangeOwnership } from '../../../../../../shared/models/videos'
7 import { VideoAcceptOwnershipValidatorsService } from '@app/shared/forms/form-validators'
8 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { AuthService } from '@app/core'
12 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
13
14 @Component({
15   selector: 'my-account-accept-ownership',
16   templateUrl: './my-account-accept-ownership.component.html',
17   styleUrls: [ './my-account-accept-ownership.component.scss' ]
18 })
19 export class MyAccountAcceptOwnershipComponent extends FormReactive implements OnInit {
20   @Output() accepted = new EventEmitter<void>()
21
22   @ViewChild('modal') modal: ElementRef
23
24   videoChangeOwnership: VideoChangeOwnership | undefined = undefined
25
26   videoChannels: VideoChannel[]
27
28   error: string = null
29
30   constructor (
31     protected formValidatorService: FormValidatorService,
32     private videoChangeOwnershipValidatorsService: VideoAcceptOwnershipValidatorsService,
33     private videoOwnershipService: VideoOwnershipService,
34     private notificationsService: NotificationsService,
35     private authService: AuthService,
36     private videoChannelService: VideoChannelService,
37     private modalService: NgbModal,
38     private i18n: I18n
39   ) {
40     super()
41   }
42
43   ngOnInit () {
44     this.videoChannels = []
45
46     this.videoChannelService.listAccountVideoChannels(this.authService.getUser().account)
47       .subscribe(videoChannels => this.videoChannels = videoChannels.data)
48
49     this.buildForm({
50       channel: this.videoChangeOwnershipValidatorsService.CHANNEL
51     })
52   }
53
54   show (videoChangeOwnership: VideoChangeOwnership) {
55     this.videoChangeOwnership = videoChangeOwnership
56     this.modalService
57       .open(this.modal)
58       .result
59       .then(() => this.acceptOwnership())
60       .catch(() => this.videoChangeOwnership = undefined)
61   }
62
63   acceptOwnership () {
64     const channel = this.form.value['channel']
65
66     const videoChangeOwnership = this.videoChangeOwnership
67     this.videoOwnershipService
68       .acceptOwnership(videoChangeOwnership.id, { channelId: channel })
69       .subscribe(
70         () => {
71           this.notificationsService.success(this.i18n('Success'), this.i18n('Ownership accepted'))
72           if (this.accepted) this.accepted.emit()
73           this.videoChangeOwnership = undefined
74         },
75
76         err => this.notificationsService.error(this.i18n('Error'), err.message)
77       )
78   }
79 }