]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-ownership/my-accept-ownership/my-accept-ownership.component.ts
Fix accept ownership change accept
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-ownership / my-accept-ownership / my-accept-ownership.component.ts
1 import { switchMap } from 'rxjs/operators'
2 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { AuthService, Notifier } from '@app/core'
4 import { OWNERSHIP_CHANGE_CHANNEL_VALIDATOR } from '@app/shared/form-validators/video-ownership-change-validators'
5 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
6 import { VideoChannelService, VideoOwnershipService } from '@app/shared/shared-main'
7 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
8 import { VideoChangeOwnership, VideoChannel } from '@shared/models'
9
10 @Component({
11 selector: 'my-accept-ownership',
12 templateUrl: './my-accept-ownership.component.html',
13 styleUrls: [ './my-accept-ownership.component.scss' ]
14 })
15 export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
16 @Output() accepted = new EventEmitter<void>()
17
18 @ViewChild('modal', { static: true }) modal: ElementRef
19
20 videoChangeOwnership: VideoChangeOwnership | undefined = undefined
21
22 videoChannels: VideoChannel[]
23
24 error: string = null
25
26 constructor (
27 protected formValidatorService: FormValidatorService,
28 private videoOwnershipService: VideoOwnershipService,
29 private notifier: Notifier,
30 private authService: AuthService,
31 private videoChannelService: VideoChannelService,
32 private modalService: NgbModal
33 ) {
34 super()
35 }
36
37 ngOnInit () {
38 this.videoChannels = []
39
40 this.authService.userInformationLoaded
41 .pipe(switchMap(() => this.videoChannelService.listAccountVideoChannels(this.authService.getUser().account)))
42 .subscribe(videoChannels => this.videoChannels = videoChannels.data)
43
44 this.buildForm({
45 channel: OWNERSHIP_CHANGE_CHANNEL_VALIDATOR
46 })
47 }
48
49 show (videoChangeOwnership: VideoChangeOwnership) {
50 this.videoChangeOwnership = videoChangeOwnership
51 this.modalService
52 .open(this.modal, { centered: true })
53 .result
54 .then(() => this.acceptOwnership())
55 .catch(() => this.videoChangeOwnership = undefined)
56 }
57
58 acceptOwnership () {
59 const channel = this.form.value['channel']
60
61 const videoChangeOwnership = this.videoChangeOwnership
62 this.videoOwnershipService
63 .acceptOwnership(videoChangeOwnership.id, { channelId: channel })
64 .subscribe(
65 () => {
66 this.notifier.success($localize`Ownership accepted`)
67 if (this.accepted) this.accepted.emit()
68 this.videoChangeOwnership = undefined
69 },
70
71 err => this.notifier.error(err.message)
72 )
73 }
74 }