]>
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' |
e309822b | 6 | import { User } from '../../../shared' |
6a84aafd | 7 | import { ServerService } from '../../../core' |
8094a898 | 8 | import { UserEdit } from './user-edit' |
db400f44 | 9 | import { UserUpdate } from '../../../../../../shared' |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 12 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
8094a898 C |
13 | |
14 | @Component({ | |
15 | selector: 'my-user-update', | |
6a84aafd C |
16 | templateUrl: './user-edit.component.html', |
17 | styleUrls: [ './user-edit.component.scss' ] | |
8094a898 C |
18 | }) |
19 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
20 | error: string | |
21 | userId: number | |
22 | username: string | |
23 | ||
8094a898 C |
24 | private paramsSub: Subscription |
25 | ||
26 | constructor ( | |
d18d6478 | 27 | protected formValidatorService: FormValidatorService, |
6a84aafd | 28 | protected serverService: ServerService, |
e309822b | 29 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
30 | private route: ActivatedRoute, |
31 | private router: Router, | |
32 | private notificationsService: NotificationsService, | |
b1d40cff C |
33 | private userService: UserService, |
34 | private i18n: I18n | |
8094a898 C |
35 | ) { |
36 | super() | |
37 | } | |
38 | ||
8094a898 | 39 | ngOnInit () { |
d18d6478 C |
40 | const defaultValues = { videoQuota: '-1' } |
41 | this.buildForm({ | |
e309822b C |
42 | email: this.userValidatorsService.USER_EMAIL, |
43 | role: this.userValidatorsService.USER_ROLE, | |
44 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA | |
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) | |
68 | ||
69 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
70 | () => { | |
b1d40cff C |
71 | this.notificationsService.success( |
72 | this.i18n('Success'), | |
25acef90 | 73 | this.i18n('User {{username}} updated.', { username: this.username }) |
b1d40cff | 74 | ) |
8094a898 C |
75 | this.router.navigate([ '/admin/users/list' ]) |
76 | }, | |
77 | ||
f7354483 | 78 | err => this.error = err.message |
8094a898 C |
79 | ) |
80 | } | |
81 | ||
82 | isCreation () { | |
83 | return false | |
84 | } | |
85 | ||
86 | getFormButtonTitle () { | |
b1d40cff | 87 | return this.i18n('Update user') |
8094a898 C |
88 | } |
89 | ||
90 | private onUserFetched (userJson: User) { | |
91 | this.userId = userJson.id | |
92 | this.username = userJson.username | |
93 | ||
94 | this.form.patchValue({ | |
95 | email: userJson.email, | |
954605a8 | 96 | role: userJson.role, |
8094a898 C |
97 | videoQuota: userJson.videoQuota |
98 | }) | |
99 | } | |
100 | } |