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