]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subscription } from 'rxjs' |
b426edd4 | 2 | import { Component, OnDestroy, OnInit } from '@angular/core' |
8094a898 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
3827c3b3 | 4 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
67ed6552 C |
5 | import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core' |
6 | import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' | |
67ed6552 C |
7 | import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models' |
8 | import { UserEdit } from './user-edit' | |
8094a898 C |
9 | |
10 | @Component({ | |
11 | selector: 'my-user-update', | |
6a84aafd C |
12 | templateUrl: './user-edit.component.html', |
13 | styleUrls: [ './user-edit.component.scss' ] | |
8094a898 C |
14 | }) |
15 | export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |
16 | error: string | |
8094a898 | 17 | |
8094a898 C |
18 | private paramsSub: Subscription |
19 | ||
20 | constructor ( | |
d18d6478 | 21 | protected formValidatorService: FormValidatorService, |
6a84aafd | 22 | protected serverService: ServerService, |
3827c3b3 | 23 | protected configService: ConfigService, |
76314386 | 24 | protected screenService: ScreenService, |
a95a4cc8 | 25 | protected auth: AuthService, |
e309822b | 26 | private userValidatorsService: UserValidatorsService, |
8094a898 C |
27 | private route: ActivatedRoute, |
28 | private router: Router, | |
f8b2c1b4 | 29 | private notifier: Notifier, |
66357162 C |
30 | private userService: UserService |
31 | ) { | |
8094a898 | 32 | super() |
3827c3b3 C |
33 | |
34 | this.buildQuotaOptions() | |
8094a898 C |
35 | } |
36 | ||
8094a898 | 37 | ngOnInit () { |
ba430d75 C |
38 | super.ngOnInit() |
39 | ||
76314386 RK |
40 | const defaultValues = { |
41 | role: UserRole.USER.toString(), | |
42 | videoQuota: '-1', | |
43 | videoQuotaDaily: '-1' | |
44 | } | |
45 | ||
d18d6478 | 46 | this.buildForm({ |
e309822b C |
47 | email: this.userValidatorsService.USER_EMAIL, |
48 | role: this.userValidatorsService.USER_ROLE, | |
bee0abff | 49 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 | 50 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
3487330d | 51 | byPassAutoBlock: null |
d18d6478 | 52 | }, defaultValues) |
8094a898 C |
53 | |
54 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
55 | const userId = routeParams['id'] | |
76314386 | 56 | this.userService.getUser(userId, true).subscribe( |
8094a898 C |
57 | user => this.onUserFetched(user), |
58 | ||
f7354483 | 59 | err => this.error = err.message |
8094a898 C |
60 | ) |
61 | }) | |
62 | } | |
63 | ||
64 | ngOnDestroy () { | |
65 | this.paramsSub.unsubscribe() | |
66 | } | |
67 | ||
68 | formValidated () { | |
69 | this.error = undefined | |
70 | ||
71 | const userUpdate: UserUpdate = this.form.value | |
1eddc9a7 | 72 | userUpdate.adminFlags = this.buildAdminFlags(this.form.value) |
8094a898 C |
73 | |
74 | // A select in HTML is always mapped as a string, we convert it to number | |
75 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
bee0abff | 76 | userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
8094a898 | 77 | |
76314386 | 78 | this.userService.updateUser(this.user.id, userUpdate).subscribe( |
8094a898 | 79 | () => { |
66357162 | 80 | this.notifier.success($localize`User ${this.user.username} updated.`) |
8094a898 C |
81 | this.router.navigate([ '/admin/users/list' ]) |
82 | }, | |
83 | ||
f7354483 | 84 | err => this.error = err.message |
8094a898 C |
85 | ) |
86 | } | |
87 | ||
88 | isCreation () { | |
89 | return false | |
90 | } | |
91 | ||
45f1bd72 JL |
92 | isPasswordOptional () { |
93 | return false | |
94 | } | |
95 | ||
8094a898 | 96 | getFormButtonTitle () { |
66357162 | 97 | return $localize`Update user` |
8094a898 C |
98 | } |
99 | ||
328c78bc | 100 | resetPassword () { |
76314386 | 101 | this.userService.askResetPassword(this.user.email).subscribe( |
328c78bc | 102 | () => { |
66357162 | 103 | this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`) |
328c78bc RK |
104 | }, |
105 | ||
106 | err => this.error = err.message | |
107 | ) | |
108 | } | |
109 | ||
76314386 RK |
110 | private onUserFetched (userJson: UserType) { |
111 | this.user = new User(userJson) | |
8094a898 C |
112 | |
113 | this.form.patchValue({ | |
114 | email: userJson.email, | |
76314386 | 115 | role: userJson.role.toString(), |
bee0abff | 116 | videoQuota: userJson.videoQuota, |
1eddc9a7 | 117 | videoQuotaDaily: userJson.videoQuotaDaily, |
3487330d | 118 | byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST |
8094a898 C |
119 | }) |
120 | } | |
121 | } |