]>
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' |
db400f44 | 6 | import { User, USER_EMAIL, USER_ROLE, USER_VIDEO_QUOTA } 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' |
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, |
8094a898 C |
28 | private route: ActivatedRoute, |
29 | private router: Router, | |
30 | private notificationsService: NotificationsService, | |
b1d40cff C |
31 | private userService: UserService, |
32 | private i18n: I18n | |
8094a898 C |
33 | ) { |
34 | super() | |
35 | } | |
36 | ||
8094a898 | 37 | ngOnInit () { |
d18d6478 C |
38 | const defaultValues = { videoQuota: '-1' } |
39 | this.buildForm({ | |
40 | email: USER_EMAIL, | |
41 | role: USER_ROLE, | |
42 | videoQuota: USER_VIDEO_QUOTA | |
43 | }, defaultValues) | |
8094a898 C |
44 | |
45 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
46 | const userId = routeParams['id'] | |
47 | this.userService.getUser(userId).subscribe( | |
48 | user => this.onUserFetched(user), | |
49 | ||
f7354483 | 50 | err => this.error = err.message |
8094a898 C |
51 | ) |
52 | }) | |
53 | } | |
54 | ||
55 | ngOnDestroy () { | |
56 | this.paramsSub.unsubscribe() | |
57 | } | |
58 | ||
59 | formValidated () { | |
60 | this.error = undefined | |
61 | ||
62 | const userUpdate: UserUpdate = this.form.value | |
63 | ||
64 | // A select in HTML is always mapped as a string, we convert it to number | |
65 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
66 | ||
67 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
68 | () => { | |
b1d40cff C |
69 | this.notificationsService.success( |
70 | this.i18n('Success'), | |
25acef90 | 71 | this.i18n('User {{username}} updated.', { username: this.username }) |
b1d40cff | 72 | ) |
8094a898 C |
73 | this.router.navigate([ '/admin/users/list' ]) |
74 | }, | |
75 | ||
f7354483 | 76 | err => this.error = err.message |
8094a898 C |
77 | ) |
78 | } | |
79 | ||
80 | isCreation () { | |
81 | return false | |
82 | } | |
83 | ||
84 | getFormButtonTitle () { | |
b1d40cff | 85 | return this.i18n('Update user') |
8094a898 C |
86 | } |
87 | ||
88 | private onUserFetched (userJson: User) { | |
89 | this.userId = userJson.id | |
90 | this.username = userJson.username | |
91 | ||
92 | this.form.patchValue({ | |
93 | email: userJson.email, | |
954605a8 | 94 | role: userJson.role, |
8094a898 C |
95 | videoQuota: userJson.videoQuota |
96 | }) | |
97 | } | |
98 | } |