feat: Added a user-service, that asserts, that a user is known
authorKai Moritz <kai@juplo.de>
Sun, 25 Dec 2022 15:13:13 +0000 (16:13 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 25 Dec 2022 18:19:36 +0000 (19:19 +0100)
src/app/app-routing.module.ts
src/app/app.component.html
src/app/app.module.ts
src/app/chatrooms/chatrooms.component.ts
src/app/user.service.spec.ts [new file with mode: 0644]
src/app/user.service.ts [new file with mode: 0644]
src/app/user/user.component.html [new file with mode: 0644]
src/app/user/user.component.less [new file with mode: 0644]
src/app/user/user.component.spec.ts [new file with mode: 0644]
src/app/user/user.component.ts [new file with mode: 0644]

index 0297262..57fcedc 100644 (file)
@@ -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)],
index 0af4fab..014c6b0 100644 (file)
@@ -1,2 +1,2 @@
 <h1>{{title}}</h1>
-<app-chatrooms></app-chatrooms>
+<router-outlet></router-outlet>
index d947470..2c193a9 100644 (file)
@@ -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
   ],
index c965cf9..99eb15a 100644 (file)
@@ -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 (file)
index 0000000..3f804c9
--- /dev/null
@@ -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 (file)
index 0000000..bc38fef
--- /dev/null
@@ -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 (file)
index 0000000..de14e7b
--- /dev/null
@@ -0,0 +1,6 @@
+<div>
+  <label for="name">Name: </label>
+  <input id="name" type="text" [formControl]="name">
+  <button type="button" (click)="updateName()">Update Name</button>
+</div>
+<p>Value: {{ name.value }}</p>
diff --git a/src/app/user/user.component.less b/src/app/user/user.component.less
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/app/user/user.component.spec.ts b/src/app/user/user.component.spec.ts
new file mode 100644 (file)
index 0000000..aaf6c19
--- /dev/null
@@ -0,0 +1,23 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { UserComponent } from './user.component';
+
+describe('UserComponent', () => {
+  let component: UserComponent;
+  let fixture: ComponentFixture<UserComponent>;
+
+  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 (file)
index 0000000..7e3caeb
--- /dev/null
@@ -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) {}
+}