]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-send.ts
Reorganize shared models
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-send.ts
1 import { catchError, switchMap, tap } from 'rxjs/operators'
2 import { EventEmitter, OnInit, Directive } from '@angular/core'
3 import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
4 import { populateAsyncUserVideoChannels } from '@app/helpers'
5 import { FormReactive } from '@app/shared/shared-forms'
6 import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { ServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'
9
10 @Directive()
11 export abstract class VideoSend extends FormReactive implements OnInit {
12 userVideoChannels: { id: number, label: string, support: string }[] = []
13 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
14 videoCaptions: VideoCaptionEdit[] = []
15
16 firstStepPrivacyId = 0
17 firstStepChannelId = 0
18
19 abstract firstStepDone: EventEmitter<string>
20 abstract firstStepError: EventEmitter<void>
21 protected abstract readonly DEFAULT_VIDEO_PRIVACY: VideoPrivacy
22
23 protected loadingBar: LoadingBarService
24 protected notifier: Notifier
25 protected authService: AuthService
26 protected serverService: ServerService
27 protected videoService: VideoService
28 protected videoCaptionService: VideoCaptionService
29 protected serverConfig: ServerConfig
30
31 abstract canDeactivate (): CanComponentDeactivateResult
32
33 ngOnInit () {
34 this.buildForm({})
35
36 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
37 .then(() => this.firstStepChannelId = this.userVideoChannels[ 0 ].id)
38
39 this.serverConfig = this.serverService.getTmpConfig()
40 this.serverService.getConfig()
41 .subscribe(config => this.serverConfig = config)
42
43 this.serverService.getVideoPrivacies()
44 .subscribe(
45 privacies => {
46 this.videoPrivacies = privacies
47
48 this.firstStepPrivacyId = this.DEFAULT_VIDEO_PRIVACY
49 })
50 }
51
52 checkForm () {
53 this.forceCheck()
54
55 return this.form.valid
56 }
57
58 protected updateVideoAndCaptions (video: VideoEdit) {
59 this.loadingBar.start()
60
61 return this.videoService.updateVideo(video)
62 .pipe(
63 // Then update captions
64 switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
65 tap(() => this.loadingBar.complete()),
66 catchError(err => {
67 this.loadingBar.complete()
68 throw err
69 })
70 )
71 }
72 }