]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-ownership/my-accept-ownership/my-accept-ownership.component.ts
Refactor modal buttons style
[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 // Select the first available channel by default
51 this.form.patchValue({
52 channel: this.videoChannels[0].id
53 })
54
55 this.videoChangeOwnership = videoChangeOwnership
56 this.modalService
57 .open(this.modal, { centered: true })
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.notifier.success($localize`Ownership accepted`)
72 if (this.accepted) this.accepted.emit()
73 this.videoChangeOwnership = undefined
74 },
75
76 err => this.notifier.error(err.message)
77 )
78 }
79 }