diff --git a/src/app/register/register.component.html b/src/app/register/register.component.html index 9312e35a9a9e62de695097d3957363629cfe575e..36774c0c1f8149fbec5b724c9464f3ec6467e814 100644 --- a/src/app/register/register.component.html +++ b/src/app/register/register.component.html @@ -1,26 +1,28 @@ -<form> +<form (ngSubmit)="register(userName.value, userPassword1.value, userPassword2.value)" fxLayout="column" + fxLayoutAlign="space-around" fxLayoutGap="10px"> <mat-form-field class="input-block"> - <input matInput #email maxlength="256" placeholder="E-mail"> - <mat-hint align="start"><strong></strong></mat-hint> + <input matInput #userName placeholder="E-mail" [formControl]="usernameFormControl" [errorStateMatcher]="matcher"> + <mat-error *ngIf="usernameFormControl.hasError('email') && !usernameFormControl.hasError('required')">Please enter a + <strong>valid</strong> email address. + </mat-error> + <mat-error *ngIf="usernameFormControl.hasError('required')">Email address is <strong>required</strong>.</mat-error> </mat-form-field> <mat-form-field class="input-block"> - <input matInput type="password" #passwd maxlength="256" placeholder="Password"> - <mat-hint align="start"><strong></strong></mat-hint> - <mat-hint align="end">{{passwd.value.length}} / 256</mat-hint> + <input matInput type="password" #userPassword1 placeholder="Password" [formControl]="password1FormControl" + [errorStateMatcher]="matcher"> + <mat-error *ngIf="password1FormControl.hasError('required')">Password is <strong>required</strong>.</mat-error> </mat-form-field> <mat-form-field class="input-block"> - <input matInput type="password" #passwd2 maxlength="256" placeholder="Verify password"> - <mat-hint align="start"><strong></strong></mat-hint> - <mat-hint align="end">{{passwd2.value.length}} / 256</mat-hint> + <input matInput type="password" #userPassword2 placeholder="Verify password" [formControl]="password2FormControl" + [errorStateMatcher]="matcher"> + <mat-error *ngIf="password2FormControl.hasError('required')">Password is <strong>required</strong>.</mat-error> + <mat-error *ngIf="password2FormControl.hasError('passwordIsEqual') && !password2FormControl.hasError('required')"> + Passwords do <strong>not</strong> match. + </mat-error> </mat-form-field> - <div *ngIf="passwd.value === passwd2.value && passwd.value.length !== 0"> - <mat-icon>check_circle</mat-icon> - </div> - <div *ngIf="passwd.value !== passwd2.value && passwd.value.length !== 0"> - <mat-icon>clear</mat-icon> - </div> - <button mat-raised-button color="primary" (click)="save(email.value, passwd.value, passwd2.value)">Register</button> + <button mat-raised-button color="primary" type="submit">Register</button> + </form> diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts index b7cbd50d6582cf2cb92f3fdc1fef0470e08f9c4c..85fd4fe2fc437f8913a98d6bf528128175e53dc4 100644 --- a/src/app/register/register.component.ts +++ b/src/app/register/register.component.ts @@ -1,5 +1,33 @@ import { Component, Inject, OnInit } from '@angular/core'; -import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; +import { ErrorStateMatcher, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; +import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms'; +import { AuthenticationService } from '../authentication.service'; +import { NotificationService } from '../notification.service'; +import { Router } from '@angular/router'; + +export class RegisterErrorStateMatcher implements ErrorStateMatcher { + isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { + const isSubmitted = form && form.submitted; + return (control && control.invalid && (control.dirty || control.touched || isSubmitted)); + } +} + +function validatePassword(password1: FormControl) { + return (formControl: FormControl) => { + const password1Value = password1.value; + const password2Value = formControl.value; + + if (password1Value !== password2Value) { + return { + passwordIsEqual: { + isEqual: false + } + }; + } else { + return null; + } + }; +} @Component({ selector: 'app-register', @@ -7,9 +35,16 @@ import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; styleUrls: ['./register.component.scss'] }) export class RegisterComponent implements OnInit { - check = 0; - constructor(public dialogRef: MatDialogRef<RegisterComponent>, + usernameFormControl = new FormControl('', [Validators.required, Validators.email]); + password1FormControl = new FormControl('', [Validators.required]); + password2FormControl = new FormControl('', [Validators.required, validatePassword(this.password1FormControl)]); + + matcher = new RegisterErrorStateMatcher(); + + constructor(public authenticationService: AuthenticationService, + public notificationService: NotificationService, + public dialogRef: MatDialogRef<RegisterComponent>, @Inject(MAT_DIALOG_DATA) public data: any) { } @@ -20,12 +55,20 @@ export class RegisterComponent implements OnInit { ngOnInit() { } - save(email: string, password1: string, password2: string): void { - // console.log(email, password1, password2); - if (password1 === password2) { - console.log(true); + register(username: string, password1: string, password2: string): void { + if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') && + !this.password1FormControl.hasError('required') && + !this.password2FormControl.hasError('required') && !this.password2FormControl.hasError('passwordIsEqual')) { + this.authenticationService.register(username, password1).subscribe(result => { + if (result) { + this.notificationService.show('Successfully registered. Please check your mail!'); + this.dialogRef.close(); + } else { + this.notificationService.show('Oops! Something went wrong on our side...'); + } + }); } else { - console.log(false); + this.notificationService.show('Please fit the requirements shown above.'); } } }