video-send.ts 2.5 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
import { catchError, switchMap, tap } from 'rxjs/operators'
Chocobozzz's avatar
Chocobozzz committed
2
import { SelectChannelItem } from 'src/types/select-options-item.model'
Chocobozzz's avatar
Chocobozzz committed
3
import { Directive, EventEmitter, OnInit } from '@angular/core'
Chocobozzz's avatar
Chocobozzz committed
4
import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
5
import { listUserChannels } from '@app/helpers'
Chocobozzz's avatar
Chocobozzz committed
6
import { FormReactive } from '@app/shared/shared-forms'
Chocobozzz's avatar
Chocobozzz committed
7
import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
Chocobozzz's avatar
Chocobozzz committed
8
import { LoadingBarService } from '@ngx-loading-bar/core'
Chocobozzz's avatar
Chocobozzz committed
9
import { HTMLServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'
Chocobozzz's avatar
Chocobozzz committed
10

Chocobozzz's avatar
Chocobozzz committed
11
@Directive()
Chocobozzz's avatar
Chocobozzz committed
12
// tslint:disable-next-line: directive-class-suffix
Chocobozzz's avatar
Chocobozzz committed
13
export abstract class VideoSend extends FormReactive implements OnInit {
Chocobozzz's avatar
Chocobozzz committed
14
  userVideoChannels: SelectChannelItem[] = []
Chocobozzz's avatar
Chocobozzz committed
15
  videoPrivacies: VideoConstant<VideoPrivacy>[] = []
Chocobozzz's avatar
Chocobozzz committed
16
17
18
19
20
  videoCaptions: VideoCaptionEdit[] = []

  firstStepPrivacyId = 0
  firstStepChannelId = 0

21
  abstract firstStepDone: EventEmitter<string>
22
  abstract firstStepError: EventEmitter<void>
Chocobozzz's avatar
Chocobozzz committed
23
24

  protected loadingBar: LoadingBarService
Chocobozzz's avatar
Chocobozzz committed
25
  protected notifier: Notifier
Chocobozzz's avatar
Chocobozzz committed
26
27
28
29
  protected authService: AuthService
  protected serverService: ServerService
  protected videoService: VideoService
  protected videoCaptionService: VideoCaptionService
Chocobozzz's avatar
Chocobozzz committed
30
  protected serverConfig: HTMLServerConfig
Chocobozzz's avatar
Chocobozzz committed
31

Chocobozzz's avatar
Chocobozzz committed
32
  abstract canDeactivate (): CanComponentDeactivateResult
Chocobozzz's avatar
Chocobozzz committed
33
34
35
36

  ngOnInit () {
    this.buildForm({})

37
38
39
40
41
    listUserChannels(this.authService)
      .subscribe(channels => {
        this.userVideoChannels = channels
        this.firstStepChannelId = this.userVideoChannels[0].id
      })
Chocobozzz's avatar
Chocobozzz committed
42

Chocobozzz's avatar
Chocobozzz committed
43
    this.serverConfig = this.serverService.getHTMLConfig()
Chocobozzz's avatar
Chocobozzz committed
44
45

    this.serverService.getVideoPrivacies()
Chocobozzz's avatar
Chocobozzz committed
46
        .subscribe(
Chocobozzz's avatar
Chocobozzz committed
47
          privacies => {
48
            const { videoPrivacies, defaultPrivacyId } = this.videoService.explainedPrivacyLabels(privacies)
Chocobozzz's avatar
Chocobozzz committed
49

50
51
            this.videoPrivacies = videoPrivacies
            this.firstStepPrivacyId = defaultPrivacyId
Chocobozzz's avatar
Chocobozzz committed
52
53
54
55
56
57
58
59
60
61
          })
  }

  checkForm () {
    this.forceCheck()

    return this.form.valid
  }

  protected updateVideoAndCaptions (video: VideoEdit) {
Chocobozzz's avatar
Chocobozzz committed
62
    this.loadingBar.useRef().start()
Chocobozzz's avatar
Chocobozzz committed
63
64
65
66
67

    return this.videoService.updateVideo(video)
        .pipe(
          // Then update captions
          switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
Chocobozzz's avatar
Chocobozzz committed
68
          tap(() => this.loadingBar.useRef().complete()),
Chocobozzz's avatar
Chocobozzz committed
69
          catchError(err => {
Chocobozzz's avatar
Chocobozzz committed
70
            this.loadingBar.useRef().complete()
Chocobozzz's avatar
Chocobozzz committed
71
72
73
74
75
            throw err
          })
        )
  }
}