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