]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-ownership/my-accept-ownership/my-accept-ownership.component.ts
Merge branch 'develop' into shorter-URLs-channels-accounts
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-ownership / my-accept-ownership / my-accept-ownership.component.ts
1 import { SelectChannelItem } from 'src/types/select-options-item.model'
2 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
3 import { AuthService, Notifier } from '@app/core'
4 import { listUserChannels } from '@app/helpers'
5 import { OWNERSHIP_CHANGE_CHANNEL_VALIDATOR } from '@app/shared/form-validators/video-ownership-change-validators'
6 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoOwnershipService } from '@app/shared/shared-main'
8 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9 import { VideoChangeOwnership } from '@shared/models'
10
11 @Component({
12 selector: 'my-accept-ownership',
13 templateUrl: './my-accept-ownership.component.html',
14 styleUrls: [ './my-accept-ownership.component.scss' ]
15 })
16 export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
17 @Output() accepted = new EventEmitter<void>()
18
19 @ViewChild('modal', { static: true }) modal: ElementRef
20
21 videoChangeOwnership: VideoChangeOwnership | undefined = undefined
22 videoChannels: SelectChannelItem[]
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 modalService: NgbModal
32 ) {
33 super()
34 }
35
36 ngOnInit () {
37 this.videoChannels = []
38
39 listUserChannels(this.authService)
40 .subscribe(channels => this.videoChannels = channels)
41
42 this.buildForm({
43 channel: OWNERSHIP_CHANGE_CHANNEL_VALIDATOR
44 })
45 }
46
47 show (videoChangeOwnership: VideoChangeOwnership) {
48 // Select the first available channel by default
49 this.form.patchValue({
50 channel: this.videoChannels[0].id
51 })
52
53 this.videoChangeOwnership = videoChangeOwnership
54 this.modalService
55 .open(this.modal, { centered: true })
56 .result
57 .then(() => this.acceptOwnership())
58 .catch(() => this.videoChangeOwnership = undefined)
59 }
60
61 acceptOwnership () {
62 const channel = this.form.value['channel']
63
64 const videoChangeOwnership = this.videoChangeOwnership
65 this.videoOwnershipService
66 .acceptOwnership(videoChangeOwnership.id, { channelId: channel })
67 .subscribe(
68 () => {
69 this.notifier.success($localize`Ownership accepted`)
70 if (this.accepted) this.accepted.emit()
71 this.videoChangeOwnership = undefined
72 },
73
74 err => this.notifier.error(err.message)
75 )
76 }
77 }