]>
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' |
f8b2c1b4 | 4 | import { Notifier } from '@app/core' |
6a84aafd | 5 | import { ServerService } from '../../../core' |
8094a898 | 6 | import { UserEdit } from './user-edit' |
3827c3b3 | 7 | import { User, UserUpdate } from '../../../../../../shared' |
b1d40cff | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 9 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 10 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' |
3827c3b3 | 11 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
e724fa93 | 12 | import { UserService } from '@app/shared' |
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, |
3827c3b3 | 29 | protected configService: ConfigService, |
e309822b | 30 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
31 | private route: ActivatedRoute, |
32 | private router: Router, | |
f8b2c1b4 | 33 | private notifier: Notifier, |
b1d40cff C |
34 | private userService: UserService, |
35 | private i18n: I18n | |
8094a898 C |
36 | ) { |
37 | super() | |
3827c3b3 C |
38 | |
39 | this.buildQuotaOptions() | |
8094a898 C |
40 | } |
41 | ||
8094a898 | 42 | ngOnInit () { |
bee0abff | 43 | const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' } |
d18d6478 | 44 | this.buildForm({ |
e309822b C |
45 | email: this.userValidatorsService.USER_EMAIL, |
46 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff FA |
47 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
48 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY | |
d18d6478 | 49 | }, defaultValues) |
8094a898 C |
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 | ||
f7354483 | 56 | err => this.error = err.message |
8094a898 C |
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) | |
bee0abff | 72 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 C |
73 | |
74 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
75 | () => { | |
f8b2c1b4 | 76 | this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username })) |
8094a898 C |
77 | this.router.navigate([ '/admin/users/list' ]) |
78 | }, | |
79 | ||
f7354483 | 80 | err => this.error = err.message |
8094a898 C |
81 | ) |
82 | } | |
83 | ||
84 | isCreation () { | |
85 | return false | |
86 | } | |
87 | ||
88 | getFormButtonTitle () { | |
b1d40cff | 89 | return this.i18n('Update user') |
8094a898 C |
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, | |
954605a8 | 98 | role: userJson.role, |
bee0abff FA |
99 | videoQuota: userJson.videoQuota, |
100 | videoQuotaDaily: userJson.videoQuotaDaily | |
8094a898 C |
101 | }) |
102 | } | |
103 | } |