]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
CommitLineData
0146f351 1import { forkJoin } from 'rxjs'
67ed6552 2import { map, switchMap } from 'rxjs/operators'
2e257e36 3import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
fbad87b0 4import { Router } from '@angular/router'
2e257e36 5import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
0146f351 6import { scrollToTop } from '@app/helpers'
5c5bcea2 7import { FormReactiveService } from '@app/shared/shared-forms'
67ed6552 8import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
67ed6552 9import { LoadingBarService } from '@ngx-loading-bar/core'
42b40636 10import { logger } from '@root-helpers/logger'
1378c0d3 11import { VideoUpdate } from '@shared/models'
c6c0fa6c 12import { hydrateFormFromVideo } from '../shared/video-edit-utils'
66357162 13import { VideoSend } from './video-send'
fbad87b0
C
14
15@Component({
047559af
C
16 selector: 'my-video-import-url',
17 templateUrl: './video-import-url.component.html',
fbad87b0 18 styleUrls: [
78848714 19 '../shared/video-edit.component.scss',
457bb213 20 './video-send.scss'
fbad87b0
C
21 ]
22})
2e257e36 23export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
fbad87b0 24 @Output() firstStepDone = new EventEmitter<string>()
7373507f 25 @Output() firstStepError = new EventEmitter<void>()
fbad87b0
C
26
27 targetUrl = ''
fbad87b0
C
28
29 isImportingVideo = false
30 hasImportedVideo = false
31 isUpdatingVideo = false
32
fbad87b0 33 video: VideoEdit
7373507f 34 error: string
fbad87b0
C
35
36 constructor (
5c5bcea2 37 protected formReactiveService: FormReactiveService,
43620009 38 protected loadingBar: LoadingBarService,
f8b2c1b4 39 protected notifier: Notifier,
43620009
C
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
fbad87b0 44 private router: Router,
2e257e36
C
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
47 ) {
fbad87b0
C
48 super()
49 }
50
51 ngOnInit () {
43620009 52 super.ngOnInit()
fbad87b0
C
53 }
54
2e257e36
C
55 ngAfterViewInit () {
56 this.hooks.runAction('action:video-url-import.init', 'video-edit')
57 }
58
fbad87b0
C
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
fbad87b0 63 isTargetUrlValid () {
9df52d66 64 return this.targetUrl?.match(/https?:\/\//)
fbad87b0
C
65 }
66
97eba003
C
67 isChannelSyncEnabled () {
68 return this.serverConfig.import.videoChannelSynchronization.enabled
69 }
70
fbad87b0
C
71 importVideo () {
72 this.isImportingVideo = true
73
74 const videoUpdate: VideoUpdate = {
a3f45a2a 75 privacy: this.highestPrivacy,
fbad87b0 76 waitTranscoding: false,
fbad87b0
C
77 channelId: this.firstStepChannelId
78 }
79
a02b93ce 80 this.loadingBar.useRef().start()
5d08a6a7 81
50ad0a1c 82 this.videoImportService
83 .importVideoUrl(this.targetUrl, videoUpdate)
84 .pipe(
0146f351
C
85 switchMap(previous => {
86 return forkJoin([
87 this.videoCaptionService.listCaptions(previous.video.uuid),
88 this.videoService.getVideo({ videoId: previous.video.uuid })
89 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
50ad0a1c 90 })
91 )
1378c0d3
C
92 .subscribe({
93 next: ({ video, videoCaptions }) => {
a02b93ce 94 this.loadingBar.useRef().complete()
50ad0a1c 95 this.firstStepDone.emit(video.name)
96 this.isImportingVideo = false
97 this.hasImportedVideo = true
98
0146f351
C
99 this.video = new VideoEdit(video)
100 this.video.patch({ privacy: this.firstStepPrivacyId })
50ad0a1c 101
102 this.videoCaptions = videoCaptions
103
c6c0fa6c 104 hydrateFormFromVideo(this.form, this.video, true)
50ad0a1c 105 },
106
1378c0d3 107 error: err => {
a02b93ce 108 this.loadingBar.useRef().complete()
50ad0a1c 109 this.isImportingVideo = false
110 this.firstStepError.emit()
111 this.notifier.error(err.message)
112 }
1378c0d3 113 })
fbad87b0
C
114 }
115
cc4bf76c
C
116 async updateSecondStep () {
117 if (!await this.isFormValid()) return
fbad87b0
C
118
119 this.video.patch(this.form.value)
120
fbad87b0
C
121 this.isUpdatingVideo = true
122
123 // Update the video
43620009 124 this.updateVideoAndCaptions(this.video)
1378c0d3
C
125 .subscribe({
126 next: () => {
fbad87b0 127 this.isUpdatingVideo = false
66357162 128 this.notifier.success($localize`Video to import updated.`)
fbad87b0 129
17119e4a 130 this.router.navigate([ '/my-library', 'video-imports' ])
fbad87b0
C
131 },
132
1378c0d3 133 error: err => {
7373507f
C
134 this.error = err.message
135 scrollToTop()
42b40636 136 logger.error(err)
fbad87b0 137 }
1378c0d3 138 })
fbad87b0
C
139 }
140}