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