]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { map, switchMap } from 'rxjs/operators' |
db7af09b | 2 | import { Component, OnInit } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
68e24d72 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
63c4db6d | 6 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 7 | import { ServerService } from '../../core' |
15a7387d | 8 | import { AuthService } from '../../core/auth' |
4cc66133 | 9 | import { FormReactive } from '../../shared' |
202f6b6c | 10 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 11 | import { VideoService } from '../../shared/video/video.service' |
6200d8d9 | 12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
b1d40cff | 13 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.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 | 20 | }) |
d8e689b8 | 21 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 22 | video: VideoEdit |
4b2f33f3 | 23 | |
68e24d72 | 24 | isUpdatingVideo = false |
ff249f49 | 25 | videoPrivacies = [] |
15a7387d | 26 | userVideoChannels = [] |
dc8bc31b | 27 | |
df98563e | 28 | constructor ( |
d18d6478 | 29 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 30 | private route: ActivatedRoute, |
7ddd02c9 | 31 | private router: Router, |
6e07c3de | 32 | private notificationsService: NotificationsService, |
db7af09b | 33 | private serverService: ServerService, |
15a7387d | 34 | private videoService: VideoService, |
68e24d72 | 35 | private authService: AuthService, |
6200d8d9 | 36 | private loadingBar: LoadingBarService, |
b1d40cff C |
37 | private videoChannelService: VideoChannelService, |
38 | private i18n: I18n | |
4b2f33f3 | 39 | ) { |
df98563e | 40 | super() |
4b2f33f3 | 41 | } |
dc8bc31b | 42 | |
df98563e | 43 | ngOnInit () { |
d18d6478 | 44 | this.buildForm({}) |
d8e689b8 | 45 | |
15a7387d | 46 | this.serverService.videoPrivaciesLoaded |
6de36768 | 47 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 48 | |
db400f44 | 49 | const uuid: string = this.route.snapshot.params[ 'uuid' ] |
2de96f4d | 50 | this.videoService.getVideo(uuid) |
db400f44 C |
51 | .pipe( |
52 | switchMap(video => { | |
53 | return this.videoService | |
54 | .loadCompleteDescription(video.descriptionPath) | |
55 | .pipe(map(description => Object.assign(video, { description }))) | |
6200d8d9 C |
56 | }), |
57 | switchMap(video => { | |
58 | return this.videoChannelService | |
ad9e39fb | 59 | .listAccountVideoChannels(video.account) |
6200d8d9 C |
60 | .pipe( |
61 | map(result => result.data), | |
74af5145 | 62 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), |
6200d8d9 C |
63 | map(videoChannels => ({ video, videoChannels })) |
64 | ) | |
db400f44 C |
65 | }) |
66 | ) | |
67 | .subscribe( | |
6200d8d9 | 68 | ({ video, videoChannels }) => { |
db400f44 | 69 | this.video = new VideoEdit(video) |
6200d8d9 | 70 | this.userVideoChannels = videoChannels |
db400f44 C |
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 | |
fd45e8f4 C |
80 | } |
81 | ||
db400f44 C |
82 | this.hydrateFormFromVideo() |
83 | }, | |
2de96f4d | 84 | |
db400f44 C |
85 | err => { |
86 | console.error(err) | |
b1d40cff | 87 | this.notificationsService.error(this.i18n('Error'), err.message) |
db400f44 C |
88 | } |
89 | ) | |
e822fdae C |
90 | } |
91 | ||
df98563e C |
92 | checkForm () { |
93 | this.forceCheck() | |
c24ac1c1 | 94 | |
df98563e | 95 | return this.form.valid |
c24ac1c1 C |
96 | } |
97 | ||
df98563e | 98 | update () { |
c24ac1c1 | 99 | if (this.checkForm() === false) { |
df98563e | 100 | return |
c24ac1c1 C |
101 | } |
102 | ||
df98563e | 103 | this.video.patch(this.form.value) |
d8e689b8 | 104 | |
68e24d72 C |
105 | this.loadingBar.start() |
106 | this.isUpdatingVideo = true | |
d8e689b8 C |
107 | this.videoService.updateVideo(this.video) |
108 | .subscribe( | |
109 | () => { | |
68e24d72 C |
110 | this.isUpdatingVideo = false |
111 | this.loadingBar.complete() | |
b1d40cff | 112 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) |
0a6658fd | 113 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
114 | }, |
115 | ||
116 | err => { | |
68e24d72 | 117 | this.isUpdatingVideo = false |
b1d40cff | 118 | this.notificationsService.error(this.i18n('Error'), err.message) |
df98563e | 119 | console.error(err) |
d8e689b8 | 120 | } |
df98563e | 121 | ) |
e822fdae | 122 | |
dc8bc31b | 123 | } |
e54163c2 | 124 | |
df98563e C |
125 | private hydrateFormFromVideo () { |
126 | this.form.patchValue(this.video.toJSON()) | |
6de36768 C |
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 | } | |
d8e689b8 | 148 | } |
dc8bc31b | 149 | } |