feat: Added a chatroom component, that is navigatable by the chatroom's ID
[demos/kafka/chat] / src / app / user / user.component.ts
1 import { Component } from '@angular/core';
2 import { FormControl } from '@angular/forms';
3 import { Validators } from '@angular/forms';
4 import { Router } from "@angular/router";
5 import { UserService } from "../user.service";
6
7 @Component({
8   selector: 'app-user',
9   templateUrl: './user.component.html',
10   styleUrls: ['./user.component.less']
11 })
12 export class UserComponent {
13
14   usernameForm = new FormControl('', [ Validators.required, this.noWhitespaceValidator ]);
15
16   updateName(): void {
17     var input = this.usernameForm.getRawValue();
18     if (input !== null) {
19       this.userService.setUser(input.trim());
20       this.router.navigate(['chatrooms'])
21     }
22   }
23
24   noWhitespaceValidator(control: FormControl) {
25     const isWhitespace = (control.value || '').trim().length === 0;
26     const isValid = !isWhitespace;
27     return isValid ? null : { 'whitespace': true };
28   }
29
30   constructor(
31     private userService: UserService,
32     private router: Router) {}
33 }