]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Form validators refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
15
16 @Component({
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
20 })
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
22 video: VideoEdit
23
24 isUpdatingVideo = false
25 videoPrivacies = []
26 userVideoChannels = []
27
28 constructor (
29 protected formValidatorService: FormValidatorService,
30 private route: ActivatedRoute,
31 private router: Router,
32 private notificationsService: NotificationsService,
33 private serverService: ServerService,
34 private videoService: VideoService,
35 private authService: AuthService,
36 private loadingBar: LoadingBarService,
37 private videoChannelService: VideoChannelService,
38 private i18n: I18n
39 ) {
40 super()
41 }
42
43 ngOnInit () {
44 this.buildForm({})
45
46 this.serverService.videoPrivaciesLoaded
47 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
48
49 const uuid: string = this.route.snapshot.params[ 'uuid' ]
50 this.videoService.getVideo(uuid)
51 .pipe(
52 switchMap(video => {
53 return this.videoService
54 .loadCompleteDescription(video.descriptionPath)
55 .pipe(map(description => Object.assign(video, { description })))
56 }),
57 switchMap(video => {
58 return this.videoChannelService
59 .listAccountVideoChannels(video.account)
60 .pipe(
61 map(result => result.data),
62 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
63 map(videoChannels => ({ video, videoChannels }))
64 )
65 })
66 )
67 .subscribe(
68 ({ video, videoChannels }) => {
69 this.video = new VideoEdit(video)
70 this.userVideoChannels = videoChannels
71
72 // We cannot set private a video that was not private
73 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
74 const newVideoPrivacies = []
75 for (const p of this.videoPrivacies) {
76 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
77 }
78
79 this.videoPrivacies = newVideoPrivacies
80 }
81
82 this.hydrateFormFromVideo()
83 },
84
85 err => {
86 console.error(err)
87 this.notificationsService.error(this.i18n('Error'), err.message)
88 }
89 )
90 }
91
92 checkForm () {
93 this.forceCheck()
94
95 return this.form.valid
96 }
97
98 update () {
99 if (this.checkForm() === false) {
100 return
101 }
102
103 this.video.patch(this.form.value)
104
105 this.loadingBar.start()
106 this.isUpdatingVideo = true
107 this.videoService.updateVideo(this.video)
108 .subscribe(
109 () => {
110 this.isUpdatingVideo = false
111 this.loadingBar.complete()
112 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
113 this.router.navigate([ '/videos/watch', this.video.uuid ])
114 },
115
116 err => {
117 this.isUpdatingVideo = false
118 this.notificationsService.error(this.i18n('Error'), err.message)
119 console.error(err)
120 }
121 )
122
123 }
124
125 private hydrateFormFromVideo () {
126 this.form.patchValue(this.video.toJSON())
127
128 const objects = [
129 {
130 url: 'thumbnailUrl',
131 name: 'thumbnailfile'
132 },
133 {
134 url: 'previewUrl',
135 name: 'previewfile'
136 }
137 ]
138
139 for (const obj of objects) {
140 fetch(this.video[obj.url])
141 .then(response => response.blob())
142 .then(data => {
143 this.form.patchValue({
144 [ obj.name ]: data
145 })
146 })
147 }
148 }
149 }