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