]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Add `etc` on some points of the CoC (#305)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
CommitLineData
db7af09b 1import { Component, OnInit } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router'
68e24d72 4import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 5import { NotificationsService } from 'angular2-notifications'
202f6b6c 6import 'rxjs/add/observable/forkJoin'
63c4db6d 7import { VideoPrivacy } from '../../../../../shared/models/videos'
db7af09b 8import { ServerService } from '../../core'
15a7387d 9import { AuthService } from '../../core/auth'
4cc66133 10import { FormReactive } from '../../shared'
63c4db6d 11import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15a7387d 12import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
202f6b6c 13import { VideoEdit } from '../../shared/video/video-edit.model'
4cc66133 14import { VideoService } from '../../shared/video/video.service'
1553e15d 15
dc8bc31b 16@Component({
d8e689b8 17 selector: 'my-videos-update',
80958c78 18 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 19 templateUrl: './video-update.component.html'
dc8bc31b
C
20})
21
d8e689b8 22export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 23 video: VideoEdit
4b2f33f3 24
68e24d72 25 isUpdatingVideo = false
df98563e 26 form: FormGroup
ff249f49
C
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
29 videoPrivacies = []
15a7387d 30 userVideoChannels = []
dc8bc31b 31
df98563e 32 constructor (
4b2f33f3 33 private formBuilder: FormBuilder,
d8e689b8 34 private route: ActivatedRoute,
7ddd02c9 35 private router: Router,
6e07c3de 36 private notificationsService: NotificationsService,
db7af09b 37 private serverService: ServerService,
15a7387d 38 private videoService: VideoService,
68e24d72
C
39 private authService: AuthService,
40 private loadingBar: LoadingBarService
4b2f33f3 41 ) {
df98563e 42 super()
4b2f33f3 43 }
dc8bc31b 44
df98563e 45 buildForm () {
ff249f49 46 this.form = this.formBuilder.group({})
df98563e 47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
48 }
49
df98563e
C
50 ngOnInit () {
51 this.buildForm()
d8e689b8 52
15a7387d 53 this.serverService.videoPrivaciesLoaded
6de36768 54 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
15a7387d
C
55
56 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
6de36768 57 .catch(err => console.error('Cannot populate async user video channels.', err))
6e07c3de 58
0a6658fd 59 const uuid: string = this.route.snapshot.params['uuid']
2de96f4d
C
60 this.videoService.getVideo(uuid)
61 .switchMap(video => {
62 return this.videoService
63 .loadCompleteDescription(video.descriptionPath)
64 .do(description => video.description = description)
65 .map(() => video)
66 })
67 .subscribe(
68 video => {
69 this.video = new VideoEdit(video)
70
ff249f49 71 // We cannot set private a video that was not private
fd45e8f4
C
72 if (video.privacy !== VideoPrivacy.PRIVATE) {
73 const newVideoPrivacies = []
74 for (const p of this.videoPrivacies) {
75 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
76 }
77
78 this.videoPrivacies = newVideoPrivacies
79 }
80
2de96f4d
C
81 this.hydrateFormFromVideo()
82 },
83
84 err => {
85 console.error(err)
ce5496d6 86 this.notificationsService.error('Error', err.message)
2de96f4d
C
87 }
88 )
e822fdae
C
89 }
90
df98563e
C
91 checkForm () {
92 this.forceCheck()
c24ac1c1 93
df98563e 94 return this.form.valid
c24ac1c1
C
95 }
96
df98563e 97 update () {
c24ac1c1 98 if (this.checkForm() === false) {
df98563e 99 return
c24ac1c1
C
100 }
101
df98563e 102 this.video.patch(this.form.value)
d8e689b8 103
68e24d72
C
104 this.loadingBar.start()
105 this.isUpdatingVideo = true
d8e689b8
C
106 this.videoService.updateVideo(this.video)
107 .subscribe(
108 () => {
68e24d72
C
109 this.isUpdatingVideo = false
110 this.loadingBar.complete()
df98563e 111 this.notificationsService.success('Success', 'Video updated.')
0a6658fd 112 this.router.navigate([ '/videos/watch', this.video.uuid ])
d8e689b8
C
113 },
114
115 err => {
68e24d72 116 this.isUpdatingVideo = false
ce5496d6 117 this.notificationsService.error('Error', err.message)
df98563e 118 console.error(err)
d8e689b8 119 }
df98563e 120 )
e822fdae 121
dc8bc31b 122 }
e54163c2 123
df98563e
C
124 private hydrateFormFromVideo () {
125 this.form.patchValue(this.video.toJSON())
6de36768
C
126
127 const objects = [
128 {
129 url: 'thumbnailUrl',
130 name: 'thumbnailfile'
131 },
132 {
133 url: 'previewUrl',
134 name: 'previewfile'
135 }
136 ]
137
138 for (const obj of objects) {
139 fetch(this.video[obj.url])
140 .then(response => response.blob())
141 .then(data => {
142 this.form.patchValue({
143 [ obj.name ]: data
144 })
145 })
146 }
d8e689b8 147 }
dc8bc31b 148}