1 import { catchError, switchMap, tap } from 'rxjs/operators'
2 import { Directive, EventEmitter, OnInit } from '@angular/core'
3 import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
4 import { populateAsyncUserVideoChannels } from '@app/helpers'
5 import { FormReactive, SelectChannelItem } 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'
11 // tslint:disable-next-line: directive-class-suffix
12 export abstract class VideoSend extends FormReactive implements OnInit {
13 userVideoChannels: SelectChannelItem[] = []
14 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
15 videoCaptions: VideoCaptionEdit[] = []
17 firstStepPrivacyId = 0
18 firstStepChannelId = 0
20 abstract firstStepDone: EventEmitter<string>
21 abstract firstStepError: EventEmitter<void>
22 protected abstract readonly DEFAULT_VIDEO_PRIVACY: VideoPrivacy
24 protected loadingBar: LoadingBarService
25 protected notifier: Notifier
26 protected authService: AuthService
27 protected serverService: ServerService
28 protected videoService: VideoService
29 protected videoCaptionService: VideoCaptionService
30 protected serverConfig: ServerConfig
32 abstract canDeactivate (): CanComponentDeactivateResult
37 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
38 .then(() => this.firstStepChannelId = this.userVideoChannels[ 0 ].id)
40 this.serverConfig = this.serverService.getTmpConfig()
41 this.serverService.getConfig()
42 .subscribe(config => this.serverConfig = config)
44 this.serverService.getVideoPrivacies()
47 this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies)
49 this.firstStepPrivacyId = this.DEFAULT_VIDEO_PRIVACY
56 return this.form.valid
59 protected updateVideoAndCaptions (video: VideoEdit) {
60 this.loadingBar.useRef().start()
62 return this.videoService.updateVideo(video)
64 // Then update captions
65 switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
66 tap(() => this.loadingBar.useRef().complete()),
68 this.loadingBar.useRef().complete()