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