1 import { mergeMap } from 'rxjs'
2 import { SelectChannelItem } from 'src/types'
3 import { Component, OnInit } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, Notifier } from '@app/core'
6 import { listUserChannelsForSelect } from '@app/helpers'
7 import { VIDEO_CHANNEL_EXTERNAL_URL_VALIDATOR } from '@app/shared/form-validators/video-channel-validators'
8 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
9 import { VideoChannelService, VideoChannelSyncService } from '@app/shared/shared-main'
10 import { VideoChannelSyncCreate } from '@shared/models/videos'
13 selector: 'my-video-channel-sync-edit',
14 templateUrl: './video-channel-sync-edit.component.html',
15 styleUrls: [ './video-channel-sync-edit.component.scss' ]
17 export class VideoChannelSyncEditComponent extends FormReactive implements OnInit {
19 userVideoChannels: SelectChannelItem[] = []
20 existingVideosStrategy: string
23 protected formValidatorService: FormValidatorService,
24 private authService: AuthService,
25 private router: Router,
26 private notifier: Notifier,
27 private videoChannelSyncService: VideoChannelSyncService,
28 private videoChannelService: VideoChannelService
35 externalChannelUrl: VIDEO_CHANNEL_EXTERNAL_URL_VALIDATOR,
37 existingVideoStrategy: null
40 listUserChannelsForSelect(this.authService)
41 .subscribe(channels => this.userVideoChannels = channels)
44 getFormButtonTitle () {
45 return $localize`Create`
49 this.error = undefined
51 const body = this.form.value
52 const videoChannelSyncCreate: VideoChannelSyncCreate = {
53 externalChannelUrl: body.externalChannelUrl,
54 videoChannelId: body.videoChannel
57 const importExistingVideos = body['existingVideoStrategy'] === 'import'
59 this.videoChannelSyncService.createSync(videoChannelSyncCreate)
60 .pipe(mergeMap(({ videoChannelSync }) => {
61 return importExistingVideos
62 ? this.videoChannelService.importVideos(videoChannelSync.channel.name, videoChannelSync.externalChannelUrl, videoChannelSync.id)
63 : Promise.resolve(null)
67 this.notifier.success($localize`Synchronization created successfully.`)
68 this.router.navigate([ '/my-library', 'video-channel-syncs' ])
72 this.error = err.message