]>
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' |
6200d8d9 | 14 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
b1d40cff | 15 | import { I18n } from '@ngx-translate/i18n-polyfill' |
1553e15d | 16 | |
dc8bc31b | 17 | @Component({ |
d8e689b8 | 18 | selector: 'my-videos-update', |
80958c78 | 19 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 20 | templateUrl: './video-update.component.html' |
dc8bc31b | 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 | 39 | private authService: AuthService, |
6200d8d9 | 40 | private loadingBar: LoadingBarService, |
b1d40cff C |
41 | private videoChannelService: VideoChannelService, |
42 | private i18n: I18n | |
4b2f33f3 | 43 | ) { |
df98563e | 44 | super() |
4b2f33f3 | 45 | } |
dc8bc31b | 46 | |
df98563e | 47 | buildForm () { |
ff249f49 | 48 | this.form = this.formBuilder.group({}) |
df98563e | 49 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
50 | } |
51 | ||
df98563e C |
52 | ngOnInit () { |
53 | this.buildForm() | |
d8e689b8 | 54 | |
15a7387d | 55 | this.serverService.videoPrivaciesLoaded |
6de36768 | 56 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 57 | |
db400f44 | 58 | const uuid: string = this.route.snapshot.params[ 'uuid' ] |
2de96f4d | 59 | this.videoService.getVideo(uuid) |
db400f44 C |
60 | .pipe( |
61 | switchMap(video => { | |
62 | return this.videoService | |
63 | .loadCompleteDescription(video.descriptionPath) | |
64 | .pipe(map(description => Object.assign(video, { description }))) | |
6200d8d9 C |
65 | }), |
66 | switchMap(video => { | |
67 | return this.videoChannelService | |
ad9e39fb | 68 | .listAccountVideoChannels(video.account) |
6200d8d9 C |
69 | .pipe( |
70 | map(result => result.data), | |
74af5145 | 71 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), |
6200d8d9 C |
72 | map(videoChannels => ({ video, videoChannels })) |
73 | ) | |
db400f44 C |
74 | }) |
75 | ) | |
76 | .subscribe( | |
6200d8d9 | 77 | ({ video, videoChannels }) => { |
db400f44 | 78 | this.video = new VideoEdit(video) |
6200d8d9 | 79 | this.userVideoChannels = videoChannels |
db400f44 C |
80 | |
81 | // We cannot set private a video that was not private | |
82 | if (video.privacy.id !== VideoPrivacy.PRIVATE) { | |
83 | const newVideoPrivacies = [] | |
84 | for (const p of this.videoPrivacies) { | |
85 | if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p) | |
86 | } | |
87 | ||
88 | this.videoPrivacies = newVideoPrivacies | |
fd45e8f4 C |
89 | } |
90 | ||
db400f44 C |
91 | this.hydrateFormFromVideo() |
92 | }, | |
2de96f4d | 93 | |
db400f44 C |
94 | err => { |
95 | console.error(err) | |
b1d40cff | 96 | this.notificationsService.error(this.i18n('Error'), err.message) |
db400f44 C |
97 | } |
98 | ) | |
e822fdae C |
99 | } |
100 | ||
df98563e C |
101 | checkForm () { |
102 | this.forceCheck() | |
c24ac1c1 | 103 | |
df98563e | 104 | return this.form.valid |
c24ac1c1 C |
105 | } |
106 | ||
df98563e | 107 | update () { |
c24ac1c1 | 108 | if (this.checkForm() === false) { |
df98563e | 109 | return |
c24ac1c1 C |
110 | } |
111 | ||
df98563e | 112 | this.video.patch(this.form.value) |
d8e689b8 | 113 | |
68e24d72 C |
114 | this.loadingBar.start() |
115 | this.isUpdatingVideo = true | |
d8e689b8 C |
116 | this.videoService.updateVideo(this.video) |
117 | .subscribe( | |
118 | () => { | |
68e24d72 C |
119 | this.isUpdatingVideo = false |
120 | this.loadingBar.complete() | |
b1d40cff | 121 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) |
0a6658fd | 122 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
123 | }, |
124 | ||
125 | err => { | |
68e24d72 | 126 | this.isUpdatingVideo = false |
b1d40cff | 127 | this.notificationsService.error(this.i18n('Error'), err.message) |
df98563e | 128 | console.error(err) |
d8e689b8 | 129 | } |
df98563e | 130 | ) |
e822fdae | 131 | |
dc8bc31b | 132 | } |
e54163c2 | 133 | |
df98563e C |
134 | private hydrateFormFromVideo () { |
135 | this.form.patchValue(this.video.toJSON()) | |
6de36768 C |
136 | |
137 | const objects = [ | |
138 | { | |
139 | url: 'thumbnailUrl', | |
140 | name: 'thumbnailfile' | |
141 | }, | |
142 | { | |
143 | url: 'previewUrl', | |
144 | name: 'previewfile' | |
145 | } | |
146 | ] | |
147 | ||
148 | for (const obj of objects) { | |
149 | fetch(this.video[obj.url]) | |
150 | .then(response => response.blob()) | |
151 | .then(data => { | |
152 | this.form.patchValue({ | |
153 | [ obj.name ]: data | |
154 | }) | |
155 | }) | |
156 | } | |
d8e689b8 | 157 | } |
dc8bc31b | 158 | } |