]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, switchMap, tap } from 'rxjs/operators' | |
2 | import { SelectChannelItem } from 'src/types/select-options-item.model' | |
3 | import { Directive, EventEmitter, OnInit } from '@angular/core' | |
4 | import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core' | |
5 | import { listUserChannels } from '@app/helpers' | |
6 | import { FormReactive } from '@app/shared/shared-forms' | |
7 | import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' | |
8 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
9 | import { ServerConfig, VideoConstant, VideoPrivacy } from '@shared/models' | |
10 | ||
11 | @Directive() | |
12 | // tslint:disable-next-line: directive-class-suffix | |
13 | export abstract class VideoSend extends FormReactive implements OnInit { | |
14 | userVideoChannels: SelectChannelItem[] = [] | |
15 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] | |
16 | videoCaptions: VideoCaptionEdit[] = [] | |
17 | ||
18 | firstStepPrivacyId = 0 | |
19 | firstStepChannelId = 0 | |
20 | ||
21 | abstract firstStepDone: EventEmitter<string> | |
22 | abstract firstStepError: EventEmitter<void> | |
23 | protected abstract readonly DEFAULT_VIDEO_PRIVACY: VideoPrivacy | |
24 | ||
25 | protected loadingBar: LoadingBarService | |
26 | protected notifier: Notifier | |
27 | protected authService: AuthService | |
28 | protected serverService: ServerService | |
29 | protected videoService: VideoService | |
30 | protected videoCaptionService: VideoCaptionService | |
31 | protected serverConfig: ServerConfig | |
32 | ||
33 | abstract canDeactivate (): CanComponentDeactivateResult | |
34 | ||
35 | ngOnInit () { | |
36 | this.buildForm({}) | |
37 | ||
38 | listUserChannels(this.authService) | |
39 | .subscribe(channels => { | |
40 | this.userVideoChannels = channels | |
41 | this.firstStepChannelId = this.userVideoChannels[0].id | |
42 | }) | |
43 | ||
44 | this.serverConfig = this.serverService.getTmpConfig() | |
45 | this.serverService.getConfig() | |
46 | .subscribe(config => this.serverConfig = config) | |
47 | ||
48 | this.serverService.getVideoPrivacies() | |
49 | .subscribe( | |
50 | privacies => { | |
51 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies) | |
52 | ||
53 | this.firstStepPrivacyId = this.DEFAULT_VIDEO_PRIVACY | |
54 | }) | |
55 | } | |
56 | ||
57 | checkForm () { | |
58 | this.forceCheck() | |
59 | ||
60 | return this.form.valid | |
61 | } | |
62 | ||
63 | protected updateVideoAndCaptions (video: VideoEdit) { | |
64 | this.loadingBar.useRef().start() | |
65 | ||
66 | return this.videoService.updateVideo(video) | |
67 | .pipe( | |
68 | // Then update captions | |
69 | switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)), | |
70 | tap(() => this.loadingBar.useRef().complete()), | |
71 | catchError(err => { | |
72 | this.loadingBar.useRef().complete() | |
73 | throw err | |
74 | }) | |
75 | ) | |
76 | } | |
77 | } |