diff options
Diffstat (limited to 'client/src/app/+admin/users/user-edit')
5 files changed, 266 insertions, 0 deletions
diff --git a/client/src/app/+admin/users/user-edit/index.ts b/client/src/app/+admin/users/user-edit/index.ts new file mode 100644 index 000000000..edec02fbb --- /dev/null +++ b/client/src/app/+admin/users/user-edit/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './user-add.component' | ||
2 | export * from './user-update.component' | ||
diff --git a/client/src/app/+admin/users/user-edit/user-add.component.ts b/client/src/app/+admin/users/user-edit/user-add.component.ts new file mode 100644 index 000000000..40f649cff --- /dev/null +++ b/client/src/app/+admin/users/user-edit/user-add.component.ts | |||
@@ -0,0 +1,87 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | ||
2 | import { FormBuilder, FormGroup } from '@angular/forms' | ||
3 | import { Router } from '@angular/router' | ||
4 | |||
5 | import { NotificationsService } from 'angular2-notifications' | ||
6 | |||
7 | import { UserService } from '../shared' | ||
8 | import { | ||
9 | USER_USERNAME, | ||
10 | USER_EMAIL, | ||
11 | USER_PASSWORD, | ||
12 | USER_VIDEO_QUOTA | ||
13 | } from '../../../shared' | ||
14 | import { UserCreate } from '../../../../../../shared' | ||
15 | import { UserEdit } from './user-edit' | ||
16 | |||
17 | @Component({ | ||
18 | selector: 'my-user-add', | ||
19 | templateUrl: './user-edit.component.html' | ||
20 | }) | ||
21 | export class UserAddComponent extends UserEdit implements OnInit { | ||
22 | error: string | ||
23 | |||
24 | form: FormGroup | ||
25 | formErrors = { | ||
26 | 'username': '', | ||
27 | 'email': '', | ||
28 | 'password': '', | ||
29 | 'videoQuota': '' | ||
30 | } | ||
31 | validationMessages = { | ||
32 | 'username': USER_USERNAME.MESSAGES, | ||
33 | 'email': USER_EMAIL.MESSAGES, | ||
34 | 'password': USER_PASSWORD.MESSAGES, | ||
35 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | ||
36 | } | ||
37 | |||
38 | constructor ( | ||
39 | private formBuilder: FormBuilder, | ||
40 | private router: Router, | ||
41 | private notificationsService: NotificationsService, | ||
42 | private userService: UserService | ||
43 | ) { | ||
44 | super() | ||
45 | } | ||
46 | |||
47 | buildForm () { | ||
48 | this.form = this.formBuilder.group({ | ||
49 | username: [ '', USER_USERNAME.VALIDATORS ], | ||
50 | email: [ '', USER_EMAIL.VALIDATORS ], | ||
51 | password: [ '', USER_PASSWORD.VALIDATORS ], | ||
52 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | ||
53 | }) | ||
54 | |||
55 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
56 | } | ||
57 | |||
58 | ngOnInit () { | ||
59 | this.buildForm() | ||
60 | } | ||
61 | |||
62 | formValidated () { | ||
63 | this.error = undefined | ||
64 | |||
65 | const userCreate: UserCreate = this.form.value | ||
66 | |||
67 | // A select in HTML is always mapped as a string, we convert it to number | ||
68 | userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | ||
69 | |||
70 | this.userService.addUser(userCreate).subscribe( | ||
71 | () => { | ||
72 | this.notificationsService.success('Success', `User ${userCreate.username} created.`) | ||
73 | this.router.navigate([ '/admin/users/list' ]) | ||
74 | }, | ||
75 | |||
76 | err => this.error = err.text | ||
77 | ) | ||
78 | } | ||
79 | |||
80 | isCreation () { | ||
81 | return true | ||
82 | } | ||
83 | |||
84 | getFormButtonTitle () { | ||
85 | return 'Add user' | ||
86 | } | ||
87 | } | ||
diff --git a/client/src/app/+admin/users/user-edit/user-edit.component.html b/client/src/app/+admin/users/user-edit/user-edit.component.html new file mode 100644 index 000000000..0e23cb731 --- /dev/null +++ b/client/src/app/+admin/users/user-edit/user-edit.component.html | |||
@@ -0,0 +1,55 @@ | |||
1 | <div class="row"> | ||
2 | <div class="content-padding"> | ||
3 | |||
4 | <h3 *ngIf="isCreation() === true">Add user</h3> | ||
5 | <h3 *ngIf="isCreation() === false">Edit user {{ username }}</h3> | ||
6 | |||
7 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | ||
8 | |||
9 | <form role="form" (ngSubmit)="formValidated()" [formGroup]="form"> | ||
10 | <div class="form-group" *ngIf="isCreation()"> | ||
11 | <label for="username">Username</label> | ||
12 | <input | ||
13 | type="text" class="form-control" id="username" placeholder="john" | ||
14 | formControlName="username" | ||
15 | > | ||
16 | <div *ngIf="formErrors.username" class="alert alert-danger"> | ||
17 | {{ formErrors.username }} | ||
18 | </div> | ||
19 | </div> | ||
20 | |||
21 | <div class="form-group"> | ||
22 | <label for="email">Email</label> | ||
23 | <input | ||
24 | type="text" class="form-control" id="email" placeholder="mail@example.com" | ||
25 | formControlName="email" | ||
26 | > | ||
27 | <div *ngIf="formErrors.email" class="alert alert-danger"> | ||
28 | {{ formErrors.email }} | ||
29 | </div> | ||
30 | </div> | ||
31 | |||
32 | <div class="form-group" *ngIf="isCreation()"> | ||
33 | <label for="password">Password</label> | ||
34 | <input | ||
35 | type="password" class="form-control" id="password" | ||
36 | formControlName="password" | ||
37 | > | ||
38 | <div *ngIf="formErrors.password" class="alert alert-danger"> | ||
39 | {{ formErrors.password }} | ||
40 | </div> | ||
41 | </div> | ||
42 | |||
43 | <div class="form-group"> | ||
44 | <label for="videoQuota">Video quota</label> | ||
45 | <select class="form-control" id="videoQuota" formControlName="videoQuota"> | ||
46 | <option *ngFor="let videoQuotaOption of videoQuotaOptions" [value]="videoQuotaOption.value"> | ||
47 | {{ videoQuotaOption.label }} | ||
48 | </option> | ||
49 | </select> | ||
50 | </div> | ||
51 | |||
52 | <input type="submit" value="{{ getFormButtonTitle() }}" class="btn btn-default" [disabled]="!form.valid"> | ||
53 | </form> | ||
54 | </div> | ||
55 | </div> | ||
diff --git a/client/src/app/+admin/users/user-edit/user-edit.ts b/client/src/app/+admin/users/user-edit/user-edit.ts new file mode 100644 index 000000000..61db8a906 --- /dev/null +++ b/client/src/app/+admin/users/user-edit/user-edit.ts | |||
@@ -0,0 +1,16 @@ | |||
1 | import { FormReactive } from '../../../shared' | ||
2 | |||
3 | export abstract class UserEdit extends FormReactive { | ||
4 | videoQuotaOptions = [ | ||
5 | { value: -1, label: 'Unlimited' }, | ||
6 | { value: 100 * 1024 * 1024, label: '100MB' }, | ||
7 | { value: 5 * 1024 * 1024, label: '500MB' }, | ||
8 | { value: 1024 * 1024 * 1024, label: '1GB' }, | ||
9 | { value: 5 * 1024 * 1024 * 1024, label: '5GB' }, | ||
10 | { value: 20 * 1024 * 1024 * 1024, label: '20GB' }, | ||
11 | { value: 50 * 1024 * 1024 * 1024, label: '50GB' } | ||
12 | ] | ||
13 | |||
14 | abstract isCreation (): boolean | ||
15 | abstract getFormButtonTitle (): string | ||
16 | } | ||
diff --git a/client/src/app/+admin/users/user-edit/user-update.component.ts b/client/src/app/+admin/users/user-edit/user-update.component.ts new file mode 100644 index 000000000..dbac5f974 --- /dev/null +++ b/client/src/app/+admin/users/user-edit/user-update.component.ts | |||
@@ -0,0 +1,106 @@ | |||
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | ||
2 | import { FormBuilder, FormGroup } from '@angular/forms' | ||
3 | import { ActivatedRoute, Router } from '@angular/router' | ||
4 | import { Subscription } from 'rxjs/Subscription' | ||
5 | |||
6 | import { NotificationsService } from 'angular2-notifications' | ||
7 | |||
8 | import { UserService } from '../shared' | ||
9 | import { USER_EMAIL, USER_VIDEO_QUOTA } from '../../../shared' | ||
10 | import { UserUpdate } from '../../../../../../shared/models/users/user-update.model' | ||
11 | import { User } from '../../../shared/users/user.model' | ||
12 | import { UserEdit } from './user-edit' | ||
13 | |||
14 | @Component({ | ||
15 | selector: 'my-user-update', | ||
16 | templateUrl: './user-edit.component.html' | ||
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': '', | ||
26 | 'videoQuota': '' | ||
27 | } | ||
28 | validationMessages = { | ||
29 | 'email': USER_EMAIL.MESSAGES, | ||
30 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | ||
31 | } | ||
32 | |||
33 | private paramsSub: Subscription | ||
34 | |||
35 | constructor ( | ||
36 | private formBuilder: FormBuilder, | ||
37 | private route: ActivatedRoute, | ||
38 | private router: Router, | ||
39 | private notificationsService: NotificationsService, | ||
40 | private userService: UserService | ||
41 | ) { | ||
42 | super() | ||
43 | } | ||
44 | |||
45 | buildForm () { | ||
46 | this.form = this.formBuilder.group({ | ||
47 | email: [ '', USER_EMAIL.VALIDATORS ], | ||
48 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | ||
49 | }) | ||
50 | |||
51 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
52 | } | ||
53 | |||
54 | ngOnInit () { | ||
55 | this.buildForm() | ||
56 | |||
57 | this.paramsSub = this.route.params.subscribe(routeParams => { | ||
58 | const userId = routeParams['id'] | ||
59 | this.userService.getUser(userId).subscribe( | ||
60 | user => this.onUserFetched(user), | ||
61 | |||
62 | err => this.error = err.text | ||
63 | ) | ||
64 | }) | ||
65 | } | ||
66 | |||
67 | ngOnDestroy () { | ||
68 | this.paramsSub.unsubscribe() | ||
69 | } | ||
70 | |||
71 | formValidated () { | ||
72 | this.error = undefined | ||
73 | |||
74 | const userUpdate: UserUpdate = this.form.value | ||
75 | |||
76 | // A select in HTML is always mapped as a string, we convert it to number | ||
77 | userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | ||
78 | |||
79 | this.userService.updateUser(this.userId, userUpdate).subscribe( | ||
80 | () => { | ||
81 | this.notificationsService.success('Success', `User ${this.username} updated.`) | ||
82 | this.router.navigate([ '/admin/users/list' ]) | ||
83 | }, | ||
84 | |||
85 | err => this.error = err.text | ||
86 | ) | ||
87 | } | ||
88 | |||
89 | isCreation () { | ||
90 | return false | ||
91 | } | ||
92 | |||
93 | getFormButtonTitle () { | ||
94 | return 'Update user' | ||
95 | } | ||
96 | |||
97 | private onUserFetched (userJson: User) { | ||
98 | this.userId = userJson.id | ||
99 | this.username = userJson.username | ||
100 | |||
101 | this.form.patchValue({ | ||
102 | email: userJson.email, | ||
103 | videoQuota: userJson.videoQuota | ||
104 | }) | ||
105 | } | ||
106 | } | ||