From: Kai Moritz Date: Sun, 25 Dec 2022 15:13:13 +0000 (+0100) Subject: feat: Added a user-service, that asserts, that a user is known X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=db7f6b1498d71f47c219dac3247884fd159ad944;p=demos%2Fkafka%2Fchat feat: Added a user-service, that asserts, that a user is known --- diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..57fcedcc 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,7 +1,13 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; +import { UserComponent } from "./user/user.component"; +import { ChatroomsComponent } from "./chatrooms/chatrooms.component"; -const routes: Routes = []; +const routes: Routes = [ + { path: 'user', component: UserComponent }, + { path: 'chatrooms', component: ChatroomsComponent }, + { path: '', redirectTo: '/user', pathMatch: 'full' }, +]; @NgModule({ imports: [RouterModule.forRoot(routes)], diff --git a/src/app/app.component.html b/src/app/app.component.html index 0af4fab5..014c6b01 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,2 +1,2 @@

{{title}}

- + diff --git a/src/app/app.module.ts b/src/app/app.module.ts index d947470c..2c193a99 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,20 +1,22 @@ import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from "@angular/forms"; +import { ReactiveFormsModule } from "@angular/forms"; import { HttpClientModule } from '@angular/common/http'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { ChatroomsComponent } from './chatrooms/chatrooms.component'; +import { UserComponent } from './user/user.component'; @NgModule({ declarations: [ AppComponent, - ChatroomsComponent + ChatroomsComponent, + UserComponent ], imports: [ BrowserModule, - FormsModule, + ReactiveFormsModule, HttpClientModule, AppRoutingModule ], diff --git a/src/app/chatrooms/chatrooms.component.ts b/src/app/chatrooms/chatrooms.component.ts index c965cf98..99eb15ac 100644 --- a/src/app/chatrooms/chatrooms.component.ts +++ b/src/app/chatrooms/chatrooms.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; -import { FormsModule } from '@angular/forms'; import { Chatroom } from '../chatroom'; +import { UserService } from "../user.service"; import { ChatroomService } from "../chatroom.service"; @Component({ @@ -12,11 +12,13 @@ export class ChatroomsComponent implements OnInit { chatrooms: Chatroom[] = []; - constructor(private chatroomsService: ChatroomService) { } + constructor( + private chatroomsService: ChatroomService, + private userService: UserService) {} ngOnInit(): void { - this.getChatrooms(); + this.userService.assertUserisKnown(() => this.getChatrooms()); } getChatrooms(): void { diff --git a/src/app/user.service.spec.ts b/src/app/user.service.spec.ts new file mode 100644 index 00000000..3f804c9f --- /dev/null +++ b/src/app/user.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { UserService } from './user.service'; + +describe('UserService', () => { + let service: UserService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(UserService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/user.service.ts b/src/app/user.service.ts new file mode 100644 index 00000000..bc38fef2 --- /dev/null +++ b/src/app/user.service.ts @@ -0,0 +1,32 @@ +import { Injectable } from '@angular/core'; +import { Router } from "@angular/router"; + +@Injectable({ + providedIn: 'root' +}) +export class UserService { + + private unknown: boolean = true; + private name = ''; + + constructor(private router: Router) { } + + assertUserisKnown(callback: Function): void { + if(this.unknown) { + this.router.navigate(['user']); + } + else { + callback(); + } + } + + setUser(name: string): void { + console.log("New user: " + name); + this.name = name; + this.unknown = false; + } + + getUser(): string { + return this.name; + } +} diff --git a/src/app/user/user.component.html b/src/app/user/user.component.html new file mode 100644 index 00000000..de14e7bb --- /dev/null +++ b/src/app/user/user.component.html @@ -0,0 +1,6 @@ +
+ + + +
+

Value: {{ name.value }}

diff --git a/src/app/user/user.component.less b/src/app/user/user.component.less new file mode 100644 index 00000000..e69de29b diff --git a/src/app/user/user.component.spec.ts b/src/app/user/user.component.spec.ts new file mode 100644 index 00000000..aaf6c19a --- /dev/null +++ b/src/app/user/user.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { UserComponent } from './user.component'; + +describe('UserComponent', () => { + let component: UserComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ UserComponent ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(UserComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/user/user.component.ts b/src/app/user/user.component.ts new file mode 100644 index 00000000..7e3caeb4 --- /dev/null +++ b/src/app/user/user.component.ts @@ -0,0 +1,26 @@ +import { Component } from '@angular/core'; +import { FormControl } from '@angular/forms'; +import { Router } from "@angular/router"; +import { UserService } from "../user.service"; + +@Component({ + selector: 'app-user', + templateUrl: './user.component.html', + styleUrls: ['./user.component.less'] +}) +export class UserComponent { + + name = new FormControl(''); + + updateName(): void { + var input = this.name.getRawValue(); + if (input !== null) { + this.userService.setUser(input); + this.router.navigate(['chatrooms']) + } + } + + constructor( + private userService: UserService, + private router: Router) {} +}