]>
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 = [] |
bbe0f064 | 27 | schedulePublicationPossible = false |
dc8bc31b | 28 | |
df98563e | 29 | constructor ( |
d18d6478 | 30 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 31 | private route: ActivatedRoute, |
7ddd02c9 | 32 | private router: Router, |
6e07c3de | 33 | private notificationsService: NotificationsService, |
db7af09b | 34 | private serverService: ServerService, |
15a7387d | 35 | private videoService: VideoService, |
68e24d72 | 36 | private authService: AuthService, |
6200d8d9 | 37 | private loadingBar: LoadingBarService, |
b1d40cff C |
38 | private videoChannelService: VideoChannelService, |
39 | private i18n: I18n | |
4b2f33f3 | 40 | ) { |
df98563e | 41 | super() |
4b2f33f3 | 42 | } |
dc8bc31b | 43 | |
df98563e | 44 | ngOnInit () { |
d18d6478 | 45 | this.buildForm({}) |
d8e689b8 | 46 | |
15a7387d | 47 | this.serverService.videoPrivaciesLoaded |
6de36768 | 48 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 49 | |
db400f44 | 50 | const uuid: string = this.route.snapshot.params[ 'uuid' ] |
2de96f4d | 51 | this.videoService.getVideo(uuid) |
db400f44 C |
52 | .pipe( |
53 | switchMap(video => { | |
54 | return this.videoService | |
55 | .loadCompleteDescription(video.descriptionPath) | |
56 | .pipe(map(description => Object.assign(video, { description }))) | |
6200d8d9 C |
57 | }), |
58 | switchMap(video => { | |
59 | return this.videoChannelService | |
ad9e39fb | 60 | .listAccountVideoChannels(video.account) |
6200d8d9 C |
61 | .pipe( |
62 | map(result => result.data), | |
74af5145 | 63 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), |
6200d8d9 C |
64 | map(videoChannels => ({ video, videoChannels })) |
65 | ) | |
db400f44 C |
66 | }) |
67 | ) | |
68 | .subscribe( | |
6200d8d9 | 69 | ({ video, videoChannels }) => { |
db400f44 | 70 | this.video = new VideoEdit(video) |
6200d8d9 | 71 | this.userVideoChannels = videoChannels |
db400f44 C |
72 | |
73 | // We cannot set private a video that was not private | |
bbe0f064 C |
74 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { |
75 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE) | |
76 | } else { // We can schedule video publication only if it it is private | |
77 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
fd45e8f4 C |
78 | } |
79 | ||
db400f44 C |
80 | this.hydrateFormFromVideo() |
81 | }, | |
2de96f4d | 82 | |
db400f44 C |
83 | err => { |
84 | console.error(err) | |
b1d40cff | 85 | this.notificationsService.error(this.i18n('Error'), err.message) |
db400f44 C |
86 | } |
87 | ) | |
e822fdae C |
88 | } |
89 | ||
df98563e C |
90 | checkForm () { |
91 | this.forceCheck() | |
c24ac1c1 | 92 | |
df98563e | 93 | return this.form.valid |
c24ac1c1 C |
94 | } |
95 | ||
df98563e | 96 | update () { |
c24ac1c1 | 97 | if (this.checkForm() === false) { |
df98563e | 98 | return |
c24ac1c1 C |
99 | } |
100 | ||
df98563e | 101 | this.video.patch(this.form.value) |
d8e689b8 | 102 | |
68e24d72 C |
103 | this.loadingBar.start() |
104 | this.isUpdatingVideo = true | |
d8e689b8 C |
105 | this.videoService.updateVideo(this.video) |
106 | .subscribe( | |
107 | () => { | |
68e24d72 C |
108 | this.isUpdatingVideo = false |
109 | this.loadingBar.complete() | |
b1d40cff | 110 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) |
0a6658fd | 111 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
112 | }, |
113 | ||
114 | err => { | |
68e24d72 | 115 | this.isUpdatingVideo = false |
b1d40cff | 116 | this.notificationsService.error(this.i18n('Error'), err.message) |
df98563e | 117 | console.error(err) |
d8e689b8 | 118 | } |
df98563e | 119 | ) |
e822fdae | 120 | |
dc8bc31b | 121 | } |
e54163c2 | 122 | |
df98563e | 123 | private hydrateFormFromVideo () { |
bbe0f064 | 124 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
125 | |
126 | const objects = [ | |
127 | { | |
128 | url: 'thumbnailUrl', | |
129 | name: 'thumbnailfile' | |
130 | }, | |
131 | { | |
132 | url: 'previewUrl', | |
133 | name: 'previewfile' | |
134 | } | |
135 | ] | |
136 | ||
137 | for (const obj of objects) { | |
138 | fetch(this.video[obj.url]) | |
139 | .then(response => response.blob()) | |
140 | .then(data => { | |
141 | this.form.patchValue({ | |
142 | [ obj.name ]: data | |
143 | }) | |
144 | }) | |
145 | } | |
d8e689b8 | 146 | } |
dc8bc31b | 147 | } |