]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { AuthService, Notifier, ServerService } from '@app/core' | |
4 | import { MyAccountVideoChannelEdit } from './my-account-video-channel-edit' | |
5 | import { VideoChannelUpdate } from '../../../../../shared/models/videos' | |
6 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
7 | import { Subscription } from 'rxjs' | |
8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
10 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
11 | import { VideoChannelValidatorsService } from '@app/shared/forms/form-validators/video-channel-validators.service' | |
12 | import { ServerConfig } from '@shared/models' | |
13 | ||
14 | @Component({ | |
15 | selector: 'my-account-video-channel-update', | |
16 | templateUrl: './my-account-video-channel-edit.component.html', | |
17 | styleUrls: [ './my-account-video-channel-edit.component.scss' ] | |
18 | }) | |
19 | export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy { | |
20 | error: string | |
21 | videoChannelToUpdate: VideoChannel | |
22 | ||
23 | private paramsSub: Subscription | |
24 | private oldSupportField: string | |
25 | private serverConfig: ServerConfig | |
26 | ||
27 | constructor ( | |
28 | protected formValidatorService: FormValidatorService, | |
29 | private authService: AuthService, | |
30 | private videoChannelValidatorsService: VideoChannelValidatorsService, | |
31 | private notifier: Notifier, | |
32 | private router: Router, | |
33 | private route: ActivatedRoute, | |
34 | private videoChannelService: VideoChannelService, | |
35 | private i18n: I18n, | |
36 | private serverService: ServerService | |
37 | ) { | |
38 | super() | |
39 | } | |
40 | ||
41 | ngOnInit () { | |
42 | this.serverConfig = this.serverService.getTmpConfig() | |
43 | this.serverService.getConfig() | |
44 | .subscribe(config => this.serverConfig = config) | |
45 | ||
46 | this.buildForm({ | |
47 | 'display-name': this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME, | |
48 | description: this.videoChannelValidatorsService.VIDEO_CHANNEL_DESCRIPTION, | |
49 | support: this.videoChannelValidatorsService.VIDEO_CHANNEL_SUPPORT, | |
50 | bulkVideosSupportUpdate: null | |
51 | }) | |
52 | ||
53 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
54 | const videoChannelId = routeParams['videoChannelId'] | |
55 | ||
56 | this.videoChannelService.getVideoChannel(videoChannelId).subscribe( | |
57 | videoChannelToUpdate => { | |
58 | this.videoChannelToUpdate = videoChannelToUpdate | |
59 | ||
60 | this.oldSupportField = videoChannelToUpdate.support | |
61 | ||
62 | this.form.patchValue({ | |
63 | 'display-name': videoChannelToUpdate.displayName, | |
64 | description: videoChannelToUpdate.description, | |
65 | support: videoChannelToUpdate.support | |
66 | }) | |
67 | }, | |
68 | ||
69 | err => this.error = err.message | |
70 | ) | |
71 | }) | |
72 | } | |
73 | ||
74 | ngOnDestroy () { | |
75 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
76 | } | |
77 | ||
78 | formValidated () { | |
79 | this.error = undefined | |
80 | ||
81 | const body = this.form.value | |
82 | const videoChannelUpdate: VideoChannelUpdate = { | |
83 | displayName: body['display-name'], | |
84 | description: body.description || null, | |
85 | support: body.support || null, | |
86 | bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false | |
87 | } | |
88 | ||
89 | this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.name, videoChannelUpdate).subscribe( | |
90 | () => { | |
91 | this.authService.refreshUserInformation() | |
92 | ||
93 | this.notifier.success( | |
94 | this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName }) | |
95 | ) | |
96 | ||
97 | this.router.navigate([ '/my-account', 'video-channels' ]) | |
98 | }, | |
99 | ||
100 | err => this.error = err.message | |
101 | ) | |
102 | } | |
103 | ||
104 | onAvatarChange (formData: FormData) { | |
105 | this.videoChannelService.changeVideoChannelAvatar(this.videoChannelToUpdate.name, formData) | |
106 | .subscribe( | |
107 | data => { | |
108 | this.notifier.success(this.i18n('Avatar changed.')) | |
109 | ||
110 | this.videoChannelToUpdate.updateAvatar(data.avatar) | |
111 | }, | |
112 | ||
113 | err => this.notifier.error(err.message) | |
114 | ) | |
115 | } | |
116 | ||
117 | get maxAvatarSize () { | |
118 | return this.serverConfig.avatar.file.size.max | |
119 | } | |
120 | ||
121 | get avatarExtensions () { | |
122 | return this.serverConfig.avatar.file.extensions.join(',') | |
123 | } | |
124 | ||
125 | isCreation () { | |
126 | return false | |
127 | } | |
128 | ||
129 | getFormButtonTitle () { | |
130 | return this.i18n('Update') | |
131 | } | |
132 | ||
133 | isBulkUpdateVideosDisplayed () { | |
134 | if (this.oldSupportField === undefined) return false | |
135 | ||
136 | return this.oldSupportField !== this.form.value['support'] | |
137 | } | |
138 | } |