]>
Commit | Line | Data |
---|---|---|
db7af09b | 1 | import { Component, OnInit } from '@angular/core' |
df98563e C |
2 | import { FormBuilder, FormGroup } from '@angular/forms' |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
68e24d72 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
202f6b6c | 6 | import 'rxjs/add/observable/forkJoin' |
63c4db6d | 7 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 8 | import { ServerService } from '../../core' |
15a7387d | 9 | import { AuthService } from '../../core/auth' |
4cc66133 | 10 | import { FormReactive } from '../../shared' |
63c4db6d | 11 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' |
15a7387d | 12 | import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' |
202f6b6c | 13 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 14 | import { 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 | 22 | export 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 | 55 | |
0a6658fd | 56 | const uuid: string = this.route.snapshot.params['uuid'] |
2de96f4d C |
57 | this.videoService.getVideo(uuid) |
58 | .switchMap(video => { | |
59 | return this.videoService | |
60 | .loadCompleteDescription(video.descriptionPath) | |
61b3e146 | 61 | .map(description => Object.assign(video, { description })) |
2de96f4d C |
62 | }) |
63 | .subscribe( | |
64 | video => { | |
65 | this.video = new VideoEdit(video) | |
66 | ||
6221f311 C |
67 | this.userVideoChannels = [ |
68 | { | |
69 | id: video.channel.id, | |
70 | label: video.channel.displayName | |
71 | } | |
72 | ] | |
73 | ||
ff249f49 | 74 | // We cannot set private a video that was not private |
fd45e8f4 C |
75 | if (video.privacy !== VideoPrivacy.PRIVATE) { |
76 | const newVideoPrivacies = [] | |
77 | for (const p of this.videoPrivacies) { | |
78 | if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p) | |
79 | } | |
80 | ||
81 | this.videoPrivacies = newVideoPrivacies | |
82 | } | |
83 | ||
2de96f4d C |
84 | this.hydrateFormFromVideo() |
85 | }, | |
86 | ||
87 | err => { | |
88 | console.error(err) | |
ce5496d6 | 89 | this.notificationsService.error('Error', err.message) |
2de96f4d C |
90 | } |
91 | ) | |
e822fdae C |
92 | } |
93 | ||
df98563e C |
94 | checkForm () { |
95 | this.forceCheck() | |
c24ac1c1 | 96 | |
df98563e | 97 | return this.form.valid |
c24ac1c1 C |
98 | } |
99 | ||
df98563e | 100 | update () { |
c24ac1c1 | 101 | if (this.checkForm() === false) { |
df98563e | 102 | return |
c24ac1c1 C |
103 | } |
104 | ||
df98563e | 105 | this.video.patch(this.form.value) |
d8e689b8 | 106 | |
68e24d72 C |
107 | this.loadingBar.start() |
108 | this.isUpdatingVideo = true | |
d8e689b8 C |
109 | this.videoService.updateVideo(this.video) |
110 | .subscribe( | |
111 | () => { | |
68e24d72 C |
112 | this.isUpdatingVideo = false |
113 | this.loadingBar.complete() | |
df98563e | 114 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 115 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
116 | }, |
117 | ||
118 | err => { | |
68e24d72 | 119 | this.isUpdatingVideo = false |
ce5496d6 | 120 | this.notificationsService.error('Error', err.message) |
df98563e | 121 | console.error(err) |
d8e689b8 | 122 | } |
df98563e | 123 | ) |
e822fdae | 124 | |
dc8bc31b | 125 | } |
e54163c2 | 126 | |
df98563e C |
127 | private hydrateFormFromVideo () { |
128 | this.form.patchValue(this.video.toJSON()) | |
6de36768 C |
129 | |
130 | const objects = [ | |
131 | { | |
132 | url: 'thumbnailUrl', | |
133 | name: 'thumbnailfile' | |
134 | }, | |
135 | { | |
136 | url: 'previewUrl', | |
137 | name: 'previewfile' | |
138 | } | |
139 | ] | |
140 | ||
141 | for (const obj of objects) { | |
142 | fetch(this.video[obj.url]) | |
143 | .then(response => response.blob()) | |
144 | .then(data => { | |
145 | this.form.patchValue({ | |
146 | [ obj.name ]: data | |
147 | }) | |
148 | }) | |
149 | } | |
d8e689b8 | 150 | } |
dc8bc31b | 151 | } |