]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { map, switchMap } from 'rxjs/operators' |
db7af09b | 2 | import { Component, OnInit } from '@angular/core' |
df98563e C |
3 | import { FormBuilder, FormGroup } from '@angular/forms' |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
68e24d72 | 5 | import { LoadingBarService } from '@ngx-loading-bar/core' |
df98563e | 6 | import { NotificationsService } from 'angular2-notifications' |
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' |
202f6b6c | 12 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 13 | import { VideoService } from '../../shared/video/video.service' |
0f320037 | 14 | import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils' |
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 | 20 | }) |
d8e689b8 | 21 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 22 | video: VideoEdit |
4b2f33f3 | 23 | |
68e24d72 | 24 | isUpdatingVideo = false |
df98563e | 25 | form: FormGroup |
ff249f49 C |
26 | formErrors: { [ id: string ]: string } = {} |
27 | validationMessages: ValidatorMessage = {} | |
28 | videoPrivacies = [] | |
15a7387d | 29 | userVideoChannels = [] |
dc8bc31b | 30 | |
df98563e | 31 | constructor ( |
4b2f33f3 | 32 | private formBuilder: FormBuilder, |
d8e689b8 | 33 | private route: ActivatedRoute, |
7ddd02c9 | 34 | private router: Router, |
6e07c3de | 35 | private notificationsService: NotificationsService, |
db7af09b | 36 | private serverService: ServerService, |
15a7387d | 37 | private videoService: VideoService, |
68e24d72 C |
38 | private authService: AuthService, |
39 | private loadingBar: LoadingBarService | |
4b2f33f3 | 40 | ) { |
df98563e | 41 | super() |
4b2f33f3 | 42 | } |
dc8bc31b | 43 | |
df98563e | 44 | buildForm () { |
ff249f49 | 45 | this.form = this.formBuilder.group({}) |
df98563e | 46 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
47 | } |
48 | ||
df98563e C |
49 | ngOnInit () { |
50 | this.buildForm() | |
d8e689b8 | 51 | |
15a7387d | 52 | this.serverService.videoPrivaciesLoaded |
6de36768 | 53 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 54 | |
db400f44 | 55 | const uuid: string = this.route.snapshot.params[ 'uuid' ] |
2de96f4d | 56 | this.videoService.getVideo(uuid) |
db400f44 C |
57 | .pipe( |
58 | switchMap(video => { | |
59 | return this.videoService | |
60 | .loadCompleteDescription(video.descriptionPath) | |
61 | .pipe(map(description => Object.assign(video, { description }))) | |
62 | }) | |
63 | ) | |
64 | .subscribe( | |
65 | video => { | |
66 | this.video = new VideoEdit(video) | |
67 | ||
68 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) | |
69 | .catch(err => console.error(err)) | |
70 | ||
71 | // We cannot set private a video that was not private | |
72 | if (video.privacy.id !== 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 | |
fd45e8f4 C |
79 | } |
80 | ||
db400f44 C |
81 | this.hydrateFormFromVideo() |
82 | }, | |
2de96f4d | 83 | |
db400f44 C |
84 | err => { |
85 | console.error(err) | |
86 | this.notificationsService.error('Error', err.message) | |
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 | } |