Client: update to new form api

This commit is contained in:
Chocobozzz 2016-08-23 14:37:49 +02:00
parent 39f87cb216
commit 0f6da32b14
13 changed files with 90 additions and 61 deletions

View File

@ -22,6 +22,7 @@
"@angular/common": "2.0.0-rc.4", "@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4", "@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4", "@angular/core": "2.0.0-rc.4",
"@angular/forms": "^0.2.0",
"@angular/http": "2.0.0-rc.4", "@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4", "@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4", "@angular/platform-browser-dynamic": "2.0.0-rc.4",
@ -42,7 +43,7 @@
"ie-shim": "^0.1.0", "ie-shim": "^0.1.0",
"intl": "^1.2.4", "intl": "^1.2.4",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
"ng2-bootstrap": "1.0.16", "ng2-bootstrap": "1.0.24",
"ng2-file-upload": "^1.0.3", "ng2-file-upload": "^1.0.3",
"node-sass": "^3.7.0", "node-sass": "^3.7.0",
"normalize.css": "^4.1.1", "normalize.css": "^4.1.1",

View File

@ -3,14 +3,14 @@
<div *ngIf="information" class="alert alert-success">{{ information }}</div> <div *ngIf="information" class="alert alert-success">{{ information }}</div>
<div *ngIf="error" class="alert alert-danger">{{ error }}</div> <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
<form role="form" (ngSubmit)="changePassword(newPassword.value, newConfirmedPassword.value)" [ngFormModel]="changePasswordForm"> <form role="form" (ngSubmit)="changePassword()" [formGroup]="changePasswordForm">
<div class="form-group"> <div class="form-group">
<label for="new-password">New password</label> <label for="new-password">New password</label>
<input <input
type="password" class="form-control" name="new-password" id="new-password" type="password" class="form-control" name="new-password" id="new-password"
ngControl="newPassword" #newPassword="ngForm" [(ngModel)]="newPassword" #newPasswordInput="ngModel"
> >
<div [hidden]="newPassword.valid || newPassword.pristine" class="alert alert-warning"> <div [hidden]="changePasswordForm.controls['new-password'].valid || changePasswordForm.controls['new-password'].pristine" class="alert alert-warning">
The password should have more than 5 characters The password should have more than 5 characters
</div> </div>
</div> </div>
@ -19,7 +19,7 @@
<label for="name">Confirm new password</label> <label for="name">Confirm new password</label>
<input <input
type="password" class="form-control" name="new-confirmed-password" id="new-confirmed-password" type="password" class="form-control" name="new-confirmed-password" id="new-confirmed-password"
ngControl="newConfirmedPassword" #newConfirmedPassword="ngForm" [(ngModel)]="newConfirmedPassword" #newConfirmedPasswordInput="ngModel"
> >
</div> </div>

View File

@ -1,5 +1,6 @@
import { Control, ControlGroup, Validators } from '@angular/common'; import { Validators } from '@angular/common';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AccountService } from './account.service'; import { AccountService } from './account.service';
@ -7,11 +8,14 @@ import { AccountService } from './account.service';
@Component({ @Component({
selector: 'my-account', selector: 'my-account',
template: require('./account.component.html'), template: require('./account.component.html'),
providers: [ AccountService ] providers: [ AccountService ],
directives: [ REACTIVE_FORM_DIRECTIVES ]
}) })
export class AccountComponent implements OnInit { export class AccountComponent implements OnInit {
changePasswordForm: ControlGroup; newPassword = '';
newConfirmedPassword = '';
changePasswordForm: FormGroup;
information: string = null; information: string = null;
error: string = null; error: string = null;
@ -21,22 +25,22 @@ export class AccountComponent implements OnInit {
) {} ) {}
ngOnInit() { ngOnInit() {
this.changePasswordForm = new ControlGroup({ this.changePasswordForm = new FormGroup({
newPassword: new Control('', Validators.compose([ Validators.required, Validators.minLength(6) ])), 'new-password': new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]),
newConfirmedPassword: new Control('', Validators.compose([ Validators.required, Validators.minLength(6) ])), 'new-confirmed-password': new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]),
}); });
} }
changePassword(newPassword: string, newConfirmedPassword: string) { changePassword() {
this.information = null; this.information = null;
this.error = null; this.error = null;
if (newPassword !== newConfirmedPassword) { if (this.newPassword !== this.newConfirmedPassword) {
this.error = 'The new password and the confirmed password do not correspond.'; this.error = 'The new password and the confirmed password do not correspond.';
return; return;
} }
this.accountService.changePassword(newPassword).subscribe( this.accountService.changePassword(this.newPassword).subscribe(
ok => this.information = 'Password updated.', ok => this.information = 'Password updated.',
err => this.error = err err => this.error = err

View File

@ -2,11 +2,11 @@
<div *ngIf="error" class="alert alert-danger">{{ error }}</div> <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
<form role="form" (ngSubmit)="makeFriends()"> <form (ngSubmit)="makeFriends()">
<div class="form-group" *ngFor="let url of urls; let id = index; trackBy:customTrackBy"> <div class="form-group" *ngFor="let url of urls; let id = index; trackBy:customTrackBy">
<label for="username">Url</label> <label for="username">Url</label>
<div class="input-group"> <div class="input-group">
<input type="text" class="form-control" name="url" id="url" placeholder="http://domain.com" [(ngModel)]="urls[id]" /> <input type="text" class="form-control" [name]="'url-' + id" [id]="'url-' + id" placeholder="http://domain.com" [(ngModel)]="urls[id]" />
<span class="input-group-btn"> <span class="input-group-btn">
<button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button> <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button>
<button *ngIf="displayRemoveField(id)" (click)="removeField(index)" class="btn btn-default" type="button">-</button> <button *ngIf="displayRemoveField(id)" (click)="removeField(index)" class="btn btn-default" type="button">-</button>

View File

@ -53,7 +53,7 @@ export class FriendAddComponent {
return; return;
} }
const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - '); const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - ');
if (!confirm(confirmMessage)) return; if (!confirm(confirmMessage)) return;
this.friendService.makeFriends(notEmptyUrls).subscribe( this.friendService.makeFriends(notEmptyUrls).subscribe(

View File

@ -2,14 +2,14 @@
<div *ngIf="error" class="alert alert-danger">{{ error }}</div> <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
<form role="form" (ngSubmit)="addUser(username.value, password.value)" #addUserForm="ngForm"> <form role="form" (ngSubmit)="addUser()" [formGroup]="userAddForm">
<div class="form-group"> <div class="form-group">
<label for="username">Username</label> <label for="username">Username</label>
<input <input
type="text" class="form-control" name="username" id="username" placeholder="Username" required type="text" class="form-control" name="username" id="username" placeholder="Username"
ngControl="username" #username="ngForm" [(ngModel)]="username"
> >
<div [hidden]="username.valid || username.pristine" class="alert alert-danger"> <div [hidden]="userAddForm.controls.username.valid || userAddForm.controls.username.pristine" class="alert alert-danger">
Username is required with a length >= 3 and <= 20 Username is required with a length >= 3 and <= 20
</div> </div>
</div> </div>
@ -17,13 +17,13 @@
<div class="form-group"> <div class="form-group">
<label for="password">Password</label> <label for="password">Password</label>
<input <input
type="password" class="form-control" name="password" id="password" placeholder="Password" required type="password" class="form-control" name="password" id="password" placeholder="Password"
ngControl="password" #password="ngForm" [(ngModel)]="password"
> >
<div [hidden]="password.valid || password.pristine" class="alert alert-danger"> <div [hidden]="userAddForm.controls.password.valid || userAddForm.controls.password.pristine" class="alert alert-danger">
Password is required with a length >= 6 Password is required with a length >= 6
</div> </div>
</div> </div>
<input type="submit" value="Add user" class="btn btn-default" [disabled]="!addUserForm.form.valid"> <input type="submit" value="Add user" class="btn btn-default" [disabled]="!userAddForm.valid">
</form> </form>

View File

@ -1,5 +1,6 @@
import { Control, ControlGroup, Validators } from '@angular/common'; import { Validators } from '@angular/common';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { UserService } from '../shared'; import { UserService } from '../shared';
@ -7,24 +8,27 @@ import { UserService } from '../shared';
@Component({ @Component({
selector: 'my-user-add', selector: 'my-user-add',
template: require('./user-add.component.html'), template: require('./user-add.component.html'),
directives: [ REACTIVE_FORM_DIRECTIVES ]
}) })
export class UserAddComponent implements OnInit { export class UserAddComponent implements OnInit {
userAddForm: ControlGroup; userAddForm: FormGroup;
error: string = null; error: string = null;
username = '';
password = '';
constructor(private router: Router, private userService: UserService) {} constructor(private router: Router, private userService: UserService) {}
ngOnInit() { ngOnInit() {
this.userAddForm = new ControlGroup({ this.userAddForm = new FormGroup({
username: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(20) ])), username: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(20) ]),
password: new Control('', Validators.compose([ Validators.required, Validators.minLength(6) ])), password: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]),
}); });
} }
addUser(username: string, password: string) { addUser() {
this.error = null; this.error = null;
this.userService.addUser(username, password).subscribe( this.userService.addUser(this.username, this.password).subscribe(
ok => this.router.navigate([ '/admin/users/list' ]), ok => this.router.navigate([ '/admin/users/list' ]),
err => this.error = err err => this.error = err

View File

@ -1,16 +1,15 @@
<h3>Login</h3> <h3>Login</h3>
<div *ngIf="error" class="alert alert-danger">{{ error }}</div> <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
<form role="form" (ngSubmit)="login(username.value, password.value)" #loginForm="ngForm"> <form role="form" (ngSubmit)="login()" [formGroup]="loginForm">
<div class="form-group"> <div class="form-group">
<label for="username">Username</label> <label for="username">Username</label>
<input <input
type="text" class="form-control" name="username" id="username" placeholder="Username" required type="text" class="form-control" name="username" id="username" placeholder="Username"
ngControl="username" #username="ngForm" [(ngModel)]="username"
> >
<div [hidden]="username.valid || username.pristine" class="alert alert-danger"> <div [hidden]="loginForm.controls.username.valid || loginForm.controls.username.pristine" class="alert alert-danger">
Username is required Username is required
</div> </div>
</div> </div>
@ -18,13 +17,13 @@
<div class="form-group"> <div class="form-group">
<label for="password">Password</label> <label for="password">Password</label>
<input <input
type="password" class="form-control" name="password" id="password" placeholder="Password" required type="password" class="form-control" name="password" id="password" placeholder="Password"
ngControl="password" #password="ngForm" [(ngModel)]="password"
> >
<div [hidden]="password.valid || password.pristine" class="alert alert-danger"> <div [hidden]="loginForm.controls.password.valid || loginForm.controls.password.pristine" class="alert alert-danger">
Password is required Password is required
</div> </div>
</div> </div>
<input type="submit" value="Login" class="btn btn-default" [disabled]="!loginForm.form.valid"> <input type="submit" value="Login" class="btn btn-default" [disabled]="!loginForm.valid">
</form> </form>

View File

@ -1,23 +1,36 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Validators } from '@angular/common';
import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthService } from '../shared'; import { AuthService } from '../shared';
@Component({ @Component({
selector: 'my-login', selector: 'my-login',
template: require('./login.component.html') template: require('./login.component.html'),
directives: [ REACTIVE_FORM_DIRECTIVES ]
}) })
export class LoginComponent { export class LoginComponent implements OnInit {
error: string = null; error: string = null;
username = '';
password: '';
loginForm: FormGroup;
constructor( constructor(
private authService: AuthService, private authService: AuthService,
private router: Router private router: Router
) {} ) {}
login(username: string, password: string) { ngOnInit() {
this.authService.login(username, password).subscribe( this.loginForm = new FormGroup({
username: new FormControl('', [ <any>Validators.required ]),
password: new FormControl('', [ <any>Validators.required ]),
});
}
login() {
this.authService.login(this.username, this.password).subscribe(
result => { result => {
this.error = null; this.error = null;

View File

@ -2,14 +2,14 @@
<div *ngIf="error" class="alert alert-danger">{{ error }}</div> <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
<form novalidate (ngSubmit)="upload()" [ngFormModel]="videoForm"> <form novalidate (ngSubmit)="upload()" [formGroup]="videoForm">
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
<input <input
type="text" class="form-control" name="name" id="name" type="text" class="form-control" name="name" id="name"
ngControl="name" #name="ngForm" [(ngModel)]="video.name" [(ngModel)]="video.name"
> >
<div [hidden]="name.valid || name.pristine" class="alert alert-warning"> <div [hidden]="videoForm.controls.name.valid || videoForm.controls.name.pristine" class="alert alert-warning">
A name is required and should be between 3 and 50 characters long A name is required and should be between 3 and 50 characters long
</div> </div>
</div> </div>
@ -18,9 +18,9 @@
<label for="tags">Tags</label> <label for="tags">Tags</label>
<input <input
type="text" class="form-control" name="tags" id="tags" type="text" class="form-control" name="tags" id="tags"
ngControl="tags" #tags="ngForm" [disabled]="isTagsInputDisabled" (keyup)="onTagKeyPress($event)" [(ngModel)]="currentTag" [disabled]="isTagsInputDisabled" (keyup)="onTagKeyPress($event)" [(ngModel)]="currentTag"
> >
<div [hidden]="tags.valid || tags.pristine" class="alert alert-warning"> <div [hidden]="videoForm.controls.tags.valid || videoForm.controls.tags.pristine" class="alert alert-warning">
A tag should be between 2 and 10 characters (alphanumeric) long A tag should be between 2 and 10 characters (alphanumeric) long
</div> </div>
</div> </div>
@ -54,10 +54,10 @@
<label for="description">Description</label> <label for="description">Description</label>
<textarea <textarea
name="description" id="description" class="form-control" placeholder="Description..." name="description" id="description" class="form-control" placeholder="Description..."
ngControl="description" #description="ngForm" [(ngModel)]="video.description" [(ngModel)]="video.description"
> >
</textarea> </textarea>
<div [hidden]="description.valid || description.pristine" class="alert alert-warning"> <div [hidden]="videoForm.controls.description.valid || videoForm.controls.description.pristine" class="alert alert-warning">
A description is required and should be between 3 and 250 characters long A description is required and should be between 3 and 250 characters long
</div> </div>
</div> </div>

View File

@ -1,5 +1,6 @@
import { Control, ControlGroup, Validators } from '@angular/common'; import { Validators } from '@angular/common';
import { Component, ElementRef, OnInit } from '@angular/core'; import { Component, ElementRef, OnInit } from '@angular/core';
import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
@ -12,14 +13,14 @@ import { AuthService } from '../../shared';
selector: 'my-videos-add', selector: 'my-videos-add',
styles: [ require('./video-add.component.scss') ], styles: [ require('./video-add.component.scss') ],
template: require('./video-add.component.html'), template: require('./video-add.component.html'),
directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES ], directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES, REACTIVE_FORM_DIRECTIVES ],
pipes: [ BytesPipe ] pipes: [ BytesPipe ]
}) })
export class VideoAddComponent implements OnInit { export class VideoAddComponent implements OnInit {
currentTag: string; // Tag the user is writing in the input currentTag: string; // Tag the user is writing in the input
error: string = null; error: string = null;
videoForm: ControlGroup; videoForm: FormGroup;
uploader: FileUploader; uploader: FileUploader;
video = { video = {
name: '', name: '',
@ -70,10 +71,10 @@ export class VideoAddComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.videoForm = new ControlGroup({ this.videoForm = new FormGroup({
name: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(50) ])), name: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(50) ]),
description: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(250) ])), description: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(250) ]),
tags: new Control('', Validators.pattern('^[a-zA-Z0-9]{2,10}$')) tags: new FormControl('', <any>Validators.pattern('^[a-zA-Z0-9]{2,10}$'))
}); });

View File

@ -1,4 +1,5 @@
import { enableProdMode, provide } from '@angular/core'; import { enableProdMode, provide } from '@angular/core';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { import {
HTTP_PROVIDERS, HTTP_PROVIDERS,
RequestOptions, RequestOptions,
@ -23,6 +24,11 @@ bootstrap(AppComponent, [
}, },
deps: [ XHRBackend, RequestOptions, AuthService ] deps: [ XHRBackend, RequestOptions, AuthService ]
}), }),
AuthService, AuthService,
provideRouter(routes)
provideRouter(routes),
disableDeprecatedForms(),
provideForms()
]); ]);

View File

@ -8,6 +8,7 @@ import '@angular/platform-browser';
import '@angular/platform-browser-dynamic'; import '@angular/platform-browser-dynamic';
import '@angular/core'; import '@angular/core';
import '@angular/common'; import '@angular/common';
import '@angular/forms';
import '@angular/http'; import '@angular/http';
import '@angular/router'; import '@angular/router';