]>
Commit | Line | Data |
---|---|---|
52d9f792 | 1 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
08c1efbe C |
2 | import { ActivatedRoute, Router } from '@angular/router' |
3 | import { NotificationsService } from 'angular2-notifications' | |
08c1efbe | 4 | import { MyAccountVideoChannelEdit } from './my-account-video-channel-edit' |
08c1efbe | 5 | import { VideoChannelUpdate } from '../../../../../shared/models/videos' |
08c1efbe | 6 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
db400f44 | 7 | import { Subscription } from 'rxjs' |
08c1efbe | 8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
52d9f792 | 9 | import { AuthService, ServerService } from '@app/core' |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 12 | import { VideoChannelValidatorsService } from '@app/shared/forms/form-validators/video-channel-validators.service' |
08c1efbe C |
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 { | |
244b4ae3 | 20 | @ViewChild('avatarfileInput') avatarfileInput: any |
52d9f792 | 21 | |
08c1efbe C |
22 | error: string |
23 | ||
e1807a94 | 24 | videoChannelToUpdate: VideoChannel |
08c1efbe C |
25 | private paramsSub: Subscription |
26 | ||
27 | constructor ( | |
d18d6478 | 28 | protected formValidatorService: FormValidatorService, |
95166f9a | 29 | private authService: AuthService, |
e309822b | 30 | private videoChannelValidatorsService: VideoChannelValidatorsService, |
08c1efbe C |
31 | private notificationsService: NotificationsService, |
32 | private router: Router, | |
33 | private route: ActivatedRoute, | |
b1d40cff | 34 | private videoChannelService: VideoChannelService, |
52d9f792 C |
35 | private i18n: I18n, |
36 | private serverService: ServerService | |
08c1efbe C |
37 | ) { |
38 | super() | |
39 | } | |
40 | ||
08c1efbe | 41 | ngOnInit () { |
d18d6478 | 42 | this.buildForm({ |
e309822b C |
43 | 'display-name': this.videoChannelValidatorsService.VIDEO_CHANNEL_DISPLAY_NAME, |
44 | description: this.videoChannelValidatorsService.VIDEO_CHANNEL_DESCRIPTION, | |
45 | support: this.videoChannelValidatorsService.VIDEO_CHANNEL_SUPPORT | |
d18d6478 | 46 | }) |
08c1efbe C |
47 | |
48 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
49 | const videoChannelId = routeParams['videoChannelId'] | |
50 | ||
51 | this.videoChannelService.getVideoChannel(videoChannelId).subscribe( | |
52 | videoChannelToUpdate => { | |
53 | this.videoChannelToUpdate = videoChannelToUpdate | |
54 | ||
55 | this.form.patchValue({ | |
56 | 'display-name': videoChannelToUpdate.displayName, | |
57 | description: videoChannelToUpdate.description, | |
58 | support: videoChannelToUpdate.support | |
59 | }) | |
60 | }, | |
61 | ||
62 | err => this.error = err.message | |
63 | ) | |
64 | }) | |
65 | } | |
66 | ||
67 | ngOnDestroy () { | |
68 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
69 | } | |
70 | ||
71 | formValidated () { | |
72 | this.error = undefined | |
73 | ||
74 | const body = this.form.value | |
75 | const videoChannelUpdate: VideoChannelUpdate = { | |
76 | displayName: body['display-name'], | |
360329cc C |
77 | description: body.description || null, |
78 | support: body.support || null | |
08c1efbe C |
79 | } |
80 | ||
22a16e36 | 81 | this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.name, videoChannelUpdate).subscribe( |
08c1efbe | 82 | () => { |
95166f9a | 83 | this.authService.refreshUserInformation() |
b1d40cff C |
84 | this.notificationsService.success( |
85 | this.i18n('Success'), | |
25acef90 | 86 | this.i18n('Video channel {{videoChannelName}} updated.', { videoChannelName: videoChannelUpdate.displayName }) |
b1d40cff | 87 | ) |
08c1efbe C |
88 | this.router.navigate([ '/my-account', 'video-channels' ]) |
89 | }, | |
90 | ||
91 | err => this.error = err.message | |
92 | ) | |
93 | } | |
94 | ||
52d9f792 | 95 | onAvatarChange (formData: FormData) { |
22a16e36 | 96 | this.videoChannelService.changeVideoChannelAvatar(this.videoChannelToUpdate.name, formData) |
52d9f792 C |
97 | .subscribe( |
98 | data => { | |
99 | this.notificationsService.success(this.i18n('Success'), this.i18n('Avatar changed.')) | |
100 | ||
101 | this.videoChannelToUpdate.updateAvatar(data.avatar) | |
102 | }, | |
103 | ||
104 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
105 | ) | |
106 | } | |
107 | ||
108 | get maxAvatarSize () { | |
109 | return this.serverService.getConfig().avatar.file.size.max | |
110 | } | |
111 | ||
112 | get avatarExtensions () { | |
113 | return this.serverService.getConfig().avatar.file.extensions.join(',') | |
114 | } | |
115 | ||
08c1efbe C |
116 | isCreation () { |
117 | return false | |
118 | } | |
119 | ||
120 | getFormButtonTitle () { | |
b1d40cff | 121 | return this.i18n('Update') |
08c1efbe C |
122 | } |
123 | } |