]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/my-account/my-account-video-channels/my-account-video-channel-update.component.ts
Add video channel management
[github/Chocobozzz/PeerTube.git] / client / src / app / my-account / my-account-video-channels / my-account-video-channel-update.component.ts
CommitLineData
08c1efbe
C
1import { Component, OnInit, OnDestroy } from '@angular/core'
2import { ActivatedRoute, Router } from '@angular/router'
3import { NotificationsService } from 'angular2-notifications'
4import 'rxjs/add/observable/from'
5import 'rxjs/add/operator/concatAll'
6import { MyAccountVideoChannelEdit } from './my-account-video-channel-edit'
7import { FormBuilder, FormGroup } from '@angular/forms'
8import { VideoChannelUpdate } from '../../../../../shared/models/videos'
9import {
10 VIDEO_CHANNEL_DESCRIPTION,
11 VIDEO_CHANNEL_DISPLAY_NAME,
12 VIDEO_CHANNEL_SUPPORT
13} from '@app/shared/forms/form-validators/video-channel'
14import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
15import { Subscription } from 'rxjs/Subscription'
16import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
17
18@Component({
19 selector: 'my-account-video-channel-update',
20 templateUrl: './my-account-video-channel-edit.component.html',
21 styleUrls: [ './my-account-video-channel-edit.component.scss' ]
22})
23export class MyAccountVideoChannelUpdateComponent extends MyAccountVideoChannelEdit implements OnInit, OnDestroy {
24 error: string
25
26 form: FormGroup
27 formErrors = {
28 'display-name': '',
29 'description': '',
30 'support': ''
31 }
32 validationMessages = {
33 'display-name': VIDEO_CHANNEL_DISPLAY_NAME.MESSAGES,
34 'description': VIDEO_CHANNEL_DESCRIPTION.MESSAGES,
35 'support': VIDEO_CHANNEL_SUPPORT.MESSAGES
36 }
37
38 private videoChannelToUpdate: VideoChannel
39 private paramsSub: Subscription
40
41 constructor (
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'],
93 description: body.description,
94 support: body.support
95 }
96
97 this.videoChannelService.updateVideoChannel(this.videoChannelToUpdate.uuid, videoChannelUpdate).subscribe(
98 () => {
99 this.notificationsService.success('Success', `Video channel ${videoChannelUpdate.displayName} updated.`)
100 this.router.navigate([ '/my-account', 'video-channels' ])
101 },
102
103 err => this.error = err.message
104 )
105 }
106
107 isCreation () {
108 return false
109 }
110
111 getFormButtonTitle () {
112 return this.videoChannelToUpdate
113 ? 'Update ' + this.videoChannelToUpdate.displayName + ' video channel'
114 : 'Update'
115 }
116}