]>
Commit | Line | Data |
---|---|---|
1 | import { map, switchMap } from 'rxjs/operators' | |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
4 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
5 | import { NotificationsService } from 'angular2-notifications' | |
6 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' | |
7 | import { ServerService } from '../../core' | |
8 | import { AuthService } from '../../core/auth' | |
9 | import { FormReactive } from '../../shared' | |
10 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
11 | import { VideoService } from '../../shared/video/video.service' | |
12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
13 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
15 | import { VideoCaptionService } from '@app/shared/video-caption' | |
16 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' | |
17 | ||
18 | @Component({ | |
19 | selector: 'my-videos-update', | |
20 | styleUrls: [ './shared/video-edit.component.scss' ], | |
21 | templateUrl: './video-update.component.html' | |
22 | }) | |
23 | export class VideoUpdateComponent extends FormReactive implements OnInit { | |
24 | video: VideoEdit | |
25 | ||
26 | isUpdatingVideo = false | |
27 | videoPrivacies: VideoConstant<string>[] = [] | |
28 | userVideoChannels: { id: number, label: string, support: string }[] = [] | |
29 | schedulePublicationPossible = false | |
30 | videoCaptions: VideoCaptionEdit[] = [] | |
31 | ||
32 | private updateDone = false | |
33 | ||
34 | constructor ( | |
35 | protected formValidatorService: FormValidatorService, | |
36 | private route: ActivatedRoute, | |
37 | private router: Router, | |
38 | private notificationsService: NotificationsService, | |
39 | private serverService: ServerService, | |
40 | private videoService: VideoService, | |
41 | private authService: AuthService, | |
42 | private loadingBar: LoadingBarService, | |
43 | private videoChannelService: VideoChannelService, | |
44 | private videoCaptionService: VideoCaptionService, | |
45 | private i18n: I18n | |
46 | ) { | |
47 | super() | |
48 | } | |
49 | ||
50 | ngOnInit () { | |
51 | this.buildForm({}) | |
52 | ||
53 | this.serverService.videoPrivaciesLoaded | |
54 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) | |
55 | ||
56 | this.route.data | |
57 | .pipe(map(data => data.videoData)) | |
58 | .subscribe(({ video, videoChannels, videoCaptions }) => { | |
59 | this.video = new VideoEdit(video) | |
60 | this.userVideoChannels = videoChannels | |
61 | this.videoCaptions = videoCaptions | |
62 | ||
63 | // We cannot set private a video that was not private | |
64 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
65 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString()) | |
66 | } else { // We can schedule video publication only if it it is private | |
67 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
68 | } | |
69 | ||
70 | // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout | |
71 | setTimeout(() => this.hydrateFormFromVideo()) | |
72 | }, | |
73 | ||
74 | err => { | |
75 | console.error(err) | |
76 | this.notificationsService.error(this.i18n('Error'), err.message) | |
77 | } | |
78 | ) | |
79 | } | |
80 | ||
81 | canDeactivate () { | |
82 | if (this.updateDone === true) return { canDeactivate: true } | |
83 | ||
84 | for (const caption of this.videoCaptions) { | |
85 | if (caption.action) return { canDeactivate: false } | |
86 | } | |
87 | ||
88 | return { canDeactivate: this.formChanged === false } | |
89 | } | |
90 | ||
91 | checkForm () { | |
92 | this.forceCheck() | |
93 | ||
94 | return this.form.valid | |
95 | } | |
96 | ||
97 | update () { | |
98 | if (this.checkForm() === false) { | |
99 | return | |
100 | } | |
101 | ||
102 | this.video.patch(this.form.value) | |
103 | ||
104 | this.loadingBar.start() | |
105 | this.isUpdatingVideo = true | |
106 | ||
107 | // Update the video | |
108 | this.videoService.updateVideo(this.video) | |
109 | .pipe( | |
110 | // Then update captions | |
111 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
112 | ) | |
113 | .subscribe( | |
114 | () => { | |
115 | this.updateDone = true | |
116 | this.isUpdatingVideo = false | |
117 | this.loadingBar.complete() | |
118 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) | |
119 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
120 | }, | |
121 | ||
122 | err => { | |
123 | this.loadingBar.complete() | |
124 | this.isUpdatingVideo = false | |
125 | this.notificationsService.error(this.i18n('Error'), err.message) | |
126 | console.error(err) | |
127 | } | |
128 | ) | |
129 | } | |
130 | ||
131 | private hydrateFormFromVideo () { | |
132 | this.form.patchValue(this.video.toFormPatch()) | |
133 | ||
134 | const objects = [ | |
135 | { | |
136 | url: 'thumbnailUrl', | |
137 | name: 'thumbnailfile' | |
138 | }, | |
139 | { | |
140 | url: 'previewUrl', | |
141 | name: 'previewfile' | |
142 | } | |
143 | ] | |
144 | ||
145 | for (const obj of objects) { | |
146 | fetch(this.video[obj.url]) | |
147 | .then(response => response.blob()) | |
148 | .then(data => { | |
149 | this.form.patchValue({ | |
150 | [ obj.name ]: data | |
151 | }) | |
152 | }) | |
153 | } | |
154 | } | |
155 | } |