]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
08c1efbe C |
2 | import { ActivatedRoute, Router } from '@angular/router' |
3 | import { NotificationsService } from 'angular2-notifications' | |
08c1efbe C |
4 | import { MyAccountVideoChannelEdit } from './my-account-video-channel-edit' |
5 | import { FormBuilder, FormGroup } from '@angular/forms' | |
6 | import { VideoChannelUpdate } from '../../../../../shared/models/videos' | |
7 | import { | |
8 | VIDEO_CHANNEL_DESCRIPTION, | |
9 | VIDEO_CHANNEL_DISPLAY_NAME, | |
10 | VIDEO_CHANNEL_SUPPORT | |
11 | } from '@app/shared/forms/form-validators/video-channel' | |
12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
db400f44 | 13 | import { Subscription } from 'rxjs' |
08c1efbe | 14 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
95166f9a | 15 | import { AuthService } from '@app/core' |
08c1efbe C |
16 | |
17 | @Component({ | |
18 | selector: 'my-account-video-channel-update', | |
19 | templateUrl: './my-account-video-channel-edit.component.html', | |
20 | styleUrls: [ './my-account-video-channel-edit.component.scss' ] | |
21 | }) | |
22 | export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy { | |
23 | error: string | |
24 | ||
25 | form: FormGroup | |
26 | formErrors = { | |
27 | 'display-name': '', | |
28 | 'description': '', | |
29 | 'support': '' | |
30 | } | |
31 | validationMessages = { | |
32 | 'display-name': VIDEO_CHANNEL_DISPLAY_NAME.MESSAGES, | |
33 | 'description': VIDEO_CHANNEL_DESCRIPTION.MESSAGES, | |
34 | 'support': VIDEO_CHANNEL_SUPPORT.MESSAGES | |
35 | } | |
36 | ||
37 | private videoChannelToUpdate: VideoChannel | |
38 | private paramsSub: Subscription | |
39 | ||
40 | constructor ( | |
95166f9a | 41 | private authService: AuthService, |
08c1efbe C |
42 | private notificationsService: NotificationsService, |
43 | private router: Router, | |
44 | private route: ActivatedRoute, | |
45 | private formBuilder: FormBuilder, | |
46 | private videoChannelService: VideoChannelService | |
47 | ) { | |
48 | super() | |
49 | } | |
50 | ||
51 | buildForm () { | |
52 | this.form = this.formBuilder.group({ | |
53 | 'display-name': [ '', VIDEO_CHANNEL_DISPLAY_NAME.VALIDATORS ], | |
54 | description: [ '', VIDEO_CHANNEL_DESCRIPTION.VALIDATORS ], | |
55 | support: [ '', VIDEO_CHANNEL_SUPPORT.VALIDATORS ] | |
56 | }) | |
57 | ||
58 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
59 | } | |
60 | ||
61 | ngOnInit () { | |
62 | this.buildForm() | |
63 | ||
64 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
65 | const videoChannelId = routeParams['videoChannelId'] | |
66 | ||
67 | this.videoChannelService.getVideoChannel(videoChannelId).subscribe( | |
68 | videoChannelToUpdate => { | |
69 | this.videoChannelToUpdate = videoChannelToUpdate | |
70 | ||
71 | this.form.patchValue({ | |
72 | 'display-name': videoChannelToUpdate.displayName, | |
73 | description: videoChannelToUpdate.description, | |
74 | support: videoChannelToUpdate.support | |
75 | }) | |
76 | }, | |
77 | ||
78 | err => this.error = err.message | |
79 | ) | |
80 | }) | |
81 | } | |
82 | ||
83 | ngOnDestroy () { | |
84 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
85 | } | |
86 | ||
87 | formValidated () { | |
88 | this.error = undefined | |
89 | ||
90 | const body = this.form.value | |
91 | const videoChannelUpdate: VideoChannelUpdate = { | |
92 | displayName: body['display-name'], | |
360329cc C |
93 | description: body.description || null, |
94 | support: body.support || null | |
08c1efbe C |
95 | } |
96 | ||
97 | this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.uuid, videoChannelUpdate).subscribe( | |
98 | () => { | |
95166f9a | 99 | this.authService.refreshUserInformation() |
08c1efbe C |
100 | this.notificationsService.success('Success', `Video channel ${videoChannelUpdate.displayName} updated.`) |
101 | this.router.navigate([ '/my-account', 'video-channels' ]) | |
102 | }, | |
103 | ||
104 | err => this.error = err.message | |
105 | ) | |
106 | } | |
107 | ||
108 | isCreation () { | |
109 | return false | |
110 | } | |
111 | ||
112 | getFormButtonTitle () { | |
7d8e778a | 113 | return 'Update' |
08c1efbe C |
114 | } |
115 | } |