aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/admin
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-09-09 22:16:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-09-09 22:16:51 +0200
commit4b2f33f3c6d109365090b08244d7f99ad4e69025 (patch)
tree700d3e8e14efc4172f754d75c041ec507100e897 /client/src/app/admin
parentab32b0fc805b92c5a1d7ac5901cb1a38e94622ca (diff)
downloadPeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.tar.gz
PeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.tar.zst
PeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.zip
Client: reactive forms
Diffstat (limited to 'client/src/app/admin')
-rw-r--r--client/src/app/admin/friends/friend-add/friend-add.component.html6
-rw-r--r--client/src/app/admin/friends/friend-add/friend-add.component.ts15
-rw-r--r--client/src/app/admin/users/user-add/user-add.component.html20
-rw-r--r--client/src/app/admin/users/user-add/user-add.component.ts44
4 files changed, 54 insertions, 31 deletions
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.html b/client/src/app/admin/friends/friend-add/friend-add.component.html
index 5b8dc8d87..788f3b44d 100644
--- a/client/src/app/admin/friends/friend-add/friend-add.component.html
+++ b/client/src/app/admin/friends/friend-add/friend-add.component.html
@@ -2,14 +2,14 @@
2 2
3<div *ngIf="error" class="alert alert-danger">{{ error }}</div> 3<div *ngIf="error" class="alert alert-danger">{{ error }}</div>
4 4
5<form (ngSubmit)="makeFriends()" [formGroup]="friendAddForm"> 5<form (ngSubmit)="makeFriends()" [formGroup]="form">
6 <div class="form-group" *ngFor="let url of urls; let id = index; trackBy:customTrackBy"> 6 <div class="form-group" *ngFor="let url of urls; let id = index; trackBy:customTrackBy">
7 <label for="username">Url</label> 7 <label for="username">Url</label>
8 8
9 <div class="input-group"> 9 <div class="input-group">
10 <input 10 <input
11 type="text" class="form-control" placeholder="http://domain.com" 11 type="text" class="form-control" placeholder="http://domain.com"
12 [name]="'url-' + id" [id]="'url-' + id" [formControlName]="'url-' + id" [(ngModel)]="urls[id]" 12 [id]="'url-' + id" [formControlName]="'url-' + id"
13 /> 13 />
14 <span class="input-group-btn"> 14 <span class="input-group-btn">
15 <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button> 15 <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button>
@@ -17,7 +17,7 @@
17 </span> 17 </span>
18 </div> 18 </div>
19 19
20 <div [hidden]="friendAddForm.controls['url-' + id].valid || friendAddForm.controls['url-' + id].pristine" class="alert alert-warning"> 20 <div [hidden]="form.controls['url-' + id].valid || form.controls['url-' + id].pristine" class="alert alert-warning">
21 It should be a valid url. 21 It should be a valid url.
22 </div> 22 </div>
23 </div> 23 </div>
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts
index 55aed9156..68363b482 100644
--- a/client/src/app/admin/friends/friend-add/friend-add.component.ts
+++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts
@@ -11,19 +11,19 @@ import { FriendService } from '../shared';
11 styles: [ require('./friend-add.component.scss') ] 11 styles: [ require('./friend-add.component.scss') ]
12}) 12})
13export class FriendAddComponent implements OnInit { 13export class FriendAddComponent implements OnInit {
14 friendAddForm: FormGroup; 14 form: FormGroup;
15 urls = [ ]; 15 urls = [ ];
16 error: string = null; 16 error: string = null;
17 17
18 constructor(private router: Router, private friendService: FriendService) {} 18 constructor(private router: Router, private friendService: FriendService) {}
19 19
20 ngOnInit() { 20 ngOnInit() {
21 this.friendAddForm = new FormGroup({}); 21 this.form = new FormGroup({});
22 this.addField(); 22 this.addField();
23 } 23 }
24 24
25 addField() { 25 addField() {
26 this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); 26 this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
27 this.urls.push(''); 27 this.urls.push('');
28 } 28 }
29 29
@@ -42,7 +42,7 @@ export class FriendAddComponent implements OnInit {
42 isFormValid() { 42 isFormValid() {
43 // Do not check the last input 43 // Do not check the last input
44 for (let i = 0; i < this.urls.length - 1; i++) { 44 for (let i = 0; i < this.urls.length - 1; i++) {
45 if (!this.friendAddForm.controls[`url-${i}`].valid) return false; 45 if (!this.form.controls[`url-${i}`].valid) return false;
46 } 46 }
47 47
48 const lastIndex = this.urls.length - 1; 48 const lastIndex = this.urls.length - 1;
@@ -50,13 +50,13 @@ export class FriendAddComponent implements OnInit {
50 if (this.urls[lastIndex] === '' && lastIndex !== 0) { 50 if (this.urls[lastIndex] === '' && lastIndex !== 0) {
51 return true; 51 return true;
52 } else { 52 } else {
53 return this.friendAddForm.controls[`url-${lastIndex}`].valid; 53 return this.form.controls[`url-${lastIndex}`].valid;
54 } 54 }
55 } 55 }
56 56
57 removeField(index: number) { 57 removeField(index: number) {
58 // Remove the last control 58 // Remove the last control
59 this.friendAddForm.removeControl(`url-${this.urls.length - 1}`); 59 this.form.removeControl(`url-${this.urls.length - 1}`);
60 this.urls.splice(index, 1); 60 this.urls.splice(index, 1);
61 } 61 }
62 62
@@ -94,7 +94,8 @@ export class FriendAddComponent implements OnInit {
94 private getNotEmptyUrls() { 94 private getNotEmptyUrls() {
95 const notEmptyUrls = []; 95 const notEmptyUrls = [];
96 96
97 this.urls.forEach((url) => { 97 Object.keys(this.form.value).forEach((urlKey) => {
98 const url = this.form.value[urlKey];
98 if (url !== '') notEmptyUrls.push(url); 99 if (url !== '') notEmptyUrls.push(url);
99 }); 100 });
100 101
diff --git a/client/src/app/admin/users/user-add/user-add.component.html b/client/src/app/admin/users/user-add/user-add.component.html
index 09219893b..9b76c7c1b 100644
--- a/client/src/app/admin/users/user-add/user-add.component.html
+++ b/client/src/app/admin/users/user-add/user-add.component.html
@@ -2,28 +2,28 @@
2 2
3<div *ngIf="error" class="alert alert-danger">{{ error }}</div> 3<div *ngIf="error" class="alert alert-danger">{{ error }}</div>
4 4
5<form role="form" (ngSubmit)="addUser()" [formGroup]="userAddForm"> 5<form role="form" (ngSubmit)="addUser()" [formGroup]="form">
6 <div class="form-group"> 6 <div class="form-group">
7 <label for="username">Username</label> 7 <label for="username">Username</label>
8 <input 8 <input
9 type="text" class="form-control" name="username" id="username" placeholder="Username" 9 type="text" class="form-control" id="username" placeholder="Username"
10 [(ngModel)]="username" 10 formControlName="username"
11 > 11 >
12 <div [hidden]="userAddForm.controls.username.valid || userAddForm.controls.username.pristine" class="alert alert-danger"> 12 <div *ngIf="formErrors.username" class="alert alert-danger">
13 Username is required with a length >= 3 and <= 20 13 {{ formErrors.username }}
14 </div> 14 </div>
15 </div> 15 </div>
16 16
17 <div class="form-group"> 17 <div class="form-group">
18 <label for="password">Password</label> 18 <label for="password">Password</label>
19 <input 19 <input
20 type="password" class="form-control" name="password" id="password" placeholder="Password" 20 type="password" class="form-control" id="password" placeholder="Password"
21 [(ngModel)]="password" 21 formControlName="password"
22 > 22 >
23 <div [hidden]="userAddForm.controls.password.valid || userAddForm.controls.password.pristine" class="alert alert-danger"> 23 <div *ngIf="formErrors.password" class="alert alert-danger">
24 Password is required with a length >= 6 24 {{ formErrors.password }}
25 </div> 25 </div>
26 </div> 26 </div>
27 27
28 <input type="submit" value="Add user" class="btn btn-default" [disabled]="!userAddForm.valid"> 28 <input type="submit" value="Add user" class="btn btn-default" [disabled]="!form.valid">
29</form> 29</form>
diff --git a/client/src/app/admin/users/user-add/user-add.component.ts b/client/src/app/admin/users/user-add/user-add.component.ts
index e3f4b2e1a..b79437795 100644
--- a/client/src/app/admin/users/user-add/user-add.component.ts
+++ b/client/src/app/admin/users/user-add/user-add.component.ts
@@ -1,32 +1,54 @@
1import { Component, OnInit } from '@angular/core'; 1import { Component, OnInit } from '@angular/core';
2import { FormGroup, FormControl, Validators } from '@angular/forms'; 2import { FormBuilder, FormGroup } from '@angular/forms';
3import { Router } from '@angular/router'; 3import { Router } from '@angular/router';
4 4
5import { UserService } from '../shared'; 5import { UserService } from '../shared';
6import { FormReactive, USER_USERNAME, USER_PASSWORD } from '../../../shared';
6 7
7@Component({ 8@Component({
8 selector: 'my-user-add', 9 selector: 'my-user-add',
9 template: require('./user-add.component.html') 10 template: require('./user-add.component.html')
10}) 11})
11export class UserAddComponent implements OnInit { 12export class UserAddComponent extends FormReactive implements OnInit {
12 userAddForm: FormGroup;
13 error: string = null; 13 error: string = null;
14 username = '';
15 password = '';
16 14
17 constructor(private router: Router, private userService: UserService) {} 15 form: FormGroup;
16 formErrors = {
17 'username': '',
18 'password': ''
19 };
20 validationMessages = {
21 'username': USER_USERNAME.MESSAGES,
22 'password': USER_PASSWORD.MESSAGES,
23 };
18 24
19 ngOnInit() { 25 constructor(
20 this.userAddForm = new FormGroup({ 26 private formBuilder: FormBuilder,
21 username: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(20) ]), 27 private router: Router,
22 password: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]), 28 private userService: UserService
29 ) {
30 super();
31 }
32
33 buildForm() {
34 this.form = this.formBuilder.group({
35 username: [ '', USER_USERNAME.VALIDATORS ],
36 password: [ '', USER_PASSWORD.VALIDATORS ],
23 }); 37 });
38
39 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
40 }
41
42 ngOnInit() {
43 this.buildForm();
24 } 44 }
25 45
26 addUser() { 46 addUser() {
27 this.error = null; 47 this.error = null;
28 48
29 this.userService.addUser(this.username, this.password).subscribe( 49 const { username, password } = this.form.value;
50
51 this.userService.addUser(username, password).subscribe(
30 ok => this.router.navigate([ '/admin/users/list' ]), 52 ok => this.router.navigate([ '/admin/users/list' ]),
31 53
32 err => this.error = err.text 54 err => this.error = err.text