Refractor notification service
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { Notifier } from '@app/core'
5 import { ServerService } from '../../../core'
6 import { UserEdit } from './user-edit'
7 import { User, UserUpdate } from '../../../../../../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11 import { ConfigService } from '@app/+admin/config/shared/config.service'
12 import { UserService } from '@app/shared'
13
14 @Component({
15   selector: 'my-user-update',
16   templateUrl: './user-edit.component.html',
17   styleUrls: [ './user-edit.component.scss' ]
18 })
19 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
20   error: string
21   userId: number
22   username: string
23
24   private paramsSub: Subscription
25
26   constructor (
27     protected formValidatorService: FormValidatorService,
28     protected serverService: ServerService,
29     protected configService: ConfigService,
30     private userValidatorsService: UserValidatorsService,
31     private route: ActivatedRoute,
32     private router: Router,
33     private notifier: Notifier,
34     private userService: UserService,
35     private i18n: I18n
36   ) {
37     super()
38
39     this.buildQuotaOptions()
40   }
41
42   ngOnInit () {
43     const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
44     this.buildForm({
45       email: this.userValidatorsService.USER_EMAIL,
46       role: this.userValidatorsService.USER_ROLE,
47       videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
48       videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
49     }, defaultValues)
50
51     this.paramsSub = this.route.params.subscribe(routeParams => {
52       const userId = routeParams['id']
53       this.userService.getUser(userId).subscribe(
54         user => this.onUserFetched(user),
55
56         err => this.error = err.message
57       )
58     })
59   }
60
61   ngOnDestroy () {
62     this.paramsSub.unsubscribe()
63   }
64
65   formValidated () {
66     this.error = undefined
67
68     const userUpdate: UserUpdate = this.form.value
69
70     // A select in HTML is always mapped as a string, we convert it to number
71     userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
72     userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
73
74     this.userService.updateUser(this.userId, userUpdate).subscribe(
75       () => {
76         this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
77         this.router.navigate([ '/admin/users/list' ])
78       },
79
80       err => this.error = err.message
81     )
82   }
83
84   isCreation () {
85     return false
86   }
87
88   getFormButtonTitle () {
89     return this.i18n('Update user')
90   }
91
92   private onUserFetched (userJson: User) {
93     this.userId = userJson.id
94     this.username = userJson.username
95
96     this.form.patchValue({
97       email: userJson.email,
98       role: userJson.role,
99       videoQuota: userJson.videoQuota,
100       videoQuotaDaily: userJson.videoQuotaDaily
101     })
102   }
103 }