feat: Added a chatroom component, that is navigatable by the chatroom's ID
[demos/kafka/chat] / src / app / user / user.component.ts
index 7e3caeb..23817da 100644 (file)
@@ -1,5 +1,6 @@
 import { Component } from '@angular/core';
 import { FormControl } from '@angular/forms';
+import { Validators } from '@angular/forms';
 import { Router } from "@angular/router";
 import { UserService } from "../user.service";
 
@@ -10,16 +11,22 @@ import { UserService } from "../user.service";
 })
 export class UserComponent {
 
-  name = new FormControl('');
+  usernameForm = new FormControl('', [ Validators.required, this.noWhitespaceValidator ]);
 
   updateName(): void {
-    var input = this.name.getRawValue();
+    var input = this.usernameForm.getRawValue();
     if (input !== null) {
-      this.userService.setUser(input);
+      this.userService.setUser(input.trim());
       this.router.navigate(['chatrooms'])
     }
   }
 
+  noWhitespaceValidator(control: FormControl) {
+    const isWhitespace = (control.value || '').trim().length === 0;
+    const isValid = !isWhitespace;
+    return isValid ? null : { 'whitespace': true };
+  }
+
   constructor(
     private userService: UserService,
     private router: Router) {}