]>
Commit | Line | Data |
---|---|---|
8094a898 C |
1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
db400f44 | 4 | import { Subscription } from 'rxjs' |
8094a898 | 5 | import { NotificationsService } from 'angular2-notifications' |
8094a898 | 6 | import { UserService } from '../shared' |
db400f44 | 7 | import { User, USER_EMAIL, USER_ROLE, USER_VIDEO_QUOTA } from '../../../shared' |
6a84aafd | 8 | import { ServerService } from '../../../core' |
8094a898 | 9 | import { UserEdit } from './user-edit' |
db400f44 | 10 | import { UserUpdate } from '../../../../../../shared' |
b1d40cff | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
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 | ||
23 | form: FormGroup | |
24 | formErrors = { | |
25 | 'email': '', | |
954605a8 | 26 | 'role': '', |
8094a898 C |
27 | 'videoQuota': '' |
28 | } | |
29 | validationMessages = { | |
30 | 'email': USER_EMAIL.MESSAGES, | |
954605a8 | 31 | 'role': USER_ROLE.MESSAGES, |
8094a898 C |
32 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES |
33 | } | |
34 | ||
35 | private paramsSub: Subscription | |
36 | ||
37 | constructor ( | |
6a84aafd | 38 | protected serverService: ServerService, |
8094a898 C |
39 | private route: ActivatedRoute, |
40 | private router: Router, | |
41 | private notificationsService: NotificationsService, | |
6a84aafd | 42 | private formBuilder: FormBuilder, |
b1d40cff C |
43 | private userService: UserService, |
44 | private i18n: I18n | |
8094a898 C |
45 | ) { |
46 | super() | |
47 | } | |
48 | ||
49 | buildForm () { | |
50 | this.form = this.formBuilder.group({ | |
51 | email: [ '', USER_EMAIL.VALIDATORS ], | |
954605a8 | 52 | role: [ '', USER_ROLE.VALIDATORS ], |
8094a898 C |
53 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] |
54 | }) | |
55 | ||
56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
57 | } | |
58 | ||
59 | ngOnInit () { | |
60 | this.buildForm() | |
61 | ||
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
63 | const userId = routeParams['id'] | |
64 | this.userService.getUser(userId).subscribe( | |
65 | user => this.onUserFetched(user), | |
66 | ||
f7354483 | 67 | err => this.error = err.message |
8094a898 C |
68 | ) |
69 | }) | |
70 | } | |
71 | ||
72 | ngOnDestroy () { | |
73 | this.paramsSub.unsubscribe() | |
74 | } | |
75 | ||
76 | formValidated () { | |
77 | this.error = undefined | |
78 | ||
79 | const userUpdate: UserUpdate = this.form.value | |
80 | ||
81 | // A select in HTML is always mapped as a string, we convert it to number | |
82 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
83 | ||
84 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
85 | () => { | |
b1d40cff C |
86 | this.notificationsService.success( |
87 | this.i18n('Success'), | |
88 | this.i18n('User {{ username }} updated.', { username: this.username }) | |
89 | ) | |
8094a898 C |
90 | this.router.navigate([ '/admin/users/list' ]) |
91 | }, | |
92 | ||
f7354483 | 93 | err => this.error = err.message |
8094a898 C |
94 | ) |
95 | } | |
96 | ||
97 | isCreation () { | |
98 | return false | |
99 | } | |
100 | ||
101 | getFormButtonTitle () { | |
b1d40cff | 102 | return this.i18n('Update user') |
8094a898 C |
103 | } |
104 | ||
105 | private onUserFetched (userJson: User) { | |
106 | this.userId = userJson.id | |
107 | this.username = userJson.username | |
108 | ||
109 | this.form.patchValue({ | |
110 | email: userJson.email, | |
954605a8 | 111 | role: userJson.role, |
8094a898 C |
112 | videoQuota: userJson.videoQuota |
113 | }) | |
114 | } | |
115 | } |