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'
11 selector: 'my-accept-ownership',
12 templateUrl: './my-accept-ownership.component.html',
13 styleUrls: [ './my-accept-ownership.component.scss' ]
15 export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
16 @Output() accepted = new EventEmitter<void>()
18 @ViewChild('modal', { static: true }) modal: ElementRef
20 videoChangeOwnership: VideoChangeOwnership | undefined = undefined
22 videoChannels: VideoChannel[]
27 protected formValidatorService: FormValidatorService,
28 private videoOwnershipService: VideoOwnershipService,
29 private notifier: Notifier,
30 private authService: AuthService,
31 private videoChannelService: VideoChannelService,
32 private modalService: NgbModal
38 this.videoChannels = []
40 this.authService.userInformationLoaded
41 .pipe(switchMap(() => this.videoChannelService.listAccountVideoChannels(this.authService.getUser().account)))
42 .subscribe(videoChannels => this.videoChannels = videoChannels.data)
45 channel: OWNERSHIP_CHANGE_CHANNEL_VALIDATOR
49 show (videoChangeOwnership: VideoChangeOwnership) {
50 // Select the first available channel by default
51 this.form.patchValue({
52 channel: this.videoChannels[0].id
55 this.videoChangeOwnership = videoChangeOwnership
57 .open(this.modal, { centered: true })
59 .then(() => this.acceptOwnership())
60 .catch(() => this.videoChangeOwnership = undefined)
64 const channel = this.form.value['channel']
66 const videoChangeOwnership = this.videoChangeOwnership
67 this.videoOwnershipService
68 .acceptOwnership(videoChangeOwnership.id, { channelId: channel })
71 this.notifier.success($localize`Ownership accepted`)
72 if (this.accepted) this.accepted.emit()
73 this.videoChangeOwnership = undefined
76 err => this.notifier.error(err.message)