diff --git a/src/app/join-room/join-room.component.html b/src/app/join-room/join-room.component.html index 581a85db333b6020686cc90d8630499bafc8d663..cbda834a7263ea007243689f431efded1a6ccd4f 100644 --- a/src/app/join-room/join-room.component.html +++ b/src/app/join-room/join-room.component.html @@ -1,12 +1,11 @@ <form (ngSubmit)="joinRoom(roomId.value)"> - <div fxLayout="row" fxLayoutAlign="center" fxLayoutGap="10px"> - <mat-form-field class="input-block"> - <input matInput #roomId placeholder="Room-Id" (keypress)="reset()"/> - </mat-form-field> - <button mat-fab color="primary" (click)="joinRoom(roomId.value)"> - <mat-icon>send</mat-icon> - </button> - </div> - <mat-error *ngIf="!isExisting" class="errorForm">No room was found with this id.</mat-error> - <mat-error *ngIf="noInput" class="errorForm">Please enter a room-id.</mat-error> + <div fxLayout="row" fxLayoutAlign="center" fxLayoutGap="10px"> + <mat-form-field class="input-block"> + <input matInput #roomId placeholder="Room-Id" [formControl]="roomFormControl" [errorStateMatcher]="matcher"/> + <mat-error *ngIf="roomFormControl.hasError('required')">Please enter a room-id.</mat-error> + </mat-form-field> + <button mat-fab color="primary" (click)="joinRoom(roomId.value)"> + <mat-icon>send</mat-icon> + </button> + </div> </form> diff --git a/src/app/join-room/join-room.component.scss b/src/app/join-room/join-room.component.scss index b192678ebec201a29778a83843687c4d36a12e86..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/src/app/join-room/join-room.component.scss +++ b/src/app/join-room/join-room.component.scss @@ -1,3 +0,0 @@ -.errorForm { - font-size: 80%; -} diff --git a/src/app/join-room/join-room.component.ts b/src/app/join-room/join-room.component.ts index 4bf6bebad765cbceeb719fe7fe18537963c7114c..52f29894b841840bafd4bccc89f10c5122426bf5 100644 --- a/src/app/join-room/join-room.component.ts +++ b/src/app/join-room/join-room.component.ts @@ -2,6 +2,17 @@ import { Component, OnInit } from '@angular/core'; import { Room } from '../room'; import { RoomService } from '../room.service'; import { Router } from '@angular/router'; +import { RegisterErrorStateMatcher } from '../register/register.component'; +import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms'; +import { ErrorStateMatcher } from '@angular/material'; +import { NotificationService } from '../notification.service'; + +export class JoinErrorStateMatcher 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)); + } +} @Component({ selector: 'app-join-room', @@ -12,11 +23,15 @@ export class JoinRoomComponent implements OnInit { room: Room; isExisting = true; - noInput = false; + + roomFormControl = new FormControl('', [Validators.required]); + + matcher = new RegisterErrorStateMatcher(); constructor(private roomService: RoomService, - private router: Router - ) { + private router: Router, + public notificationService: NotificationService + ) { } ngOnInit() { @@ -24,25 +39,17 @@ export class JoinRoomComponent implements OnInit { } joinRoom(id: string): void { - if (id) { - this.roomService.getRoom(id) - .subscribe( room => { - this.room = room; - if (!room) { - this.isExisting = false; - this.noInput = false; - } else { - this.router.navigate([`/participant/room/${this.room.id}`]); - } - }); - } else { - this.noInput = true; - this.isExisting = true; + if (!this.roomFormControl.hasError('required')) { + this.roomService.getRoom(id) + .subscribe(room => { + this.room = room; + if (!room) { + this.notificationService.show(`No room was found with id: ${id}`); + } else { + this.router.navigate([`/participant/room/${this.room.id}`]); + } + }); } } - reset(): void { - this.isExisting = true; - this.noInput = false; - } }