feat: Added a chatroom component, that is navigatable by the chatroom's ID
authorKai Moritz <kai@juplo.de>
Sun, 25 Dec 2022 22:03:47 +0000 (23:03 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 25 Dec 2022 22:33:20 +0000 (23:33 +0100)
src/app/app-routing.module.ts
src/app/app.module.ts
src/app/chatroom.service.ts
src/app/chatroom/chatroom.component.html [new file with mode: 0644]
src/app/chatroom/chatroom.component.less [new file with mode: 0644]
src/app/chatroom/chatroom.component.spec.ts [new file with mode: 0644]
src/app/chatroom/chatroom.component.ts [new file with mode: 0644]
src/app/chatrooms/chatrooms.component.html
src/app/chatrooms/chatrooms.component.ts

index 57fcedc..4809bdd 100644 (file)
@@ -2,10 +2,12 @@ import { NgModule } from '@angular/core';
 import { RouterModule, Routes } from '@angular/router';
 import { UserComponent } from "./user/user.component";
 import { ChatroomsComponent } from "./chatrooms/chatrooms.component";
+import { ChatroomComponent } from "./chatroom/chatroom.component";
 
 const routes: Routes = [
   { path: 'user', component: UserComponent },
   { path: 'chatrooms', component: ChatroomsComponent },
+  { path: 'chatroom/:id', component: ChatroomComponent },
   { path: '', redirectTo: '/user', pathMatch: 'full' },
 ];
 
index 2c193a9..b2b66d6 100644 (file)
@@ -7,12 +7,14 @@ import { AppRoutingModule } from './app-routing.module';
 import { AppComponent } from './app.component';
 import { ChatroomsComponent } from './chatrooms/chatrooms.component';
 import { UserComponent } from './user/user.component';
+import { ChatroomComponent } from './chatroom/chatroom.component';
 
 @NgModule({
   declarations: [
     AppComponent,
     ChatroomsComponent,
-    UserComponent
+    UserComponent,
+    ChatroomComponent
   ],
   imports: [
     BrowserModule,
index 77b0818..ab92206 100644 (file)
@@ -9,10 +9,15 @@ import { Chatroom } from "./chatroom";
 export class ChatroomService {
 
   private uriList = 'http://localhost:8080/list';
+  private uriGet = 'http://localhost:8080/get/';
 
   constructor(private http: HttpClient) { }
 
   getChatrooms(): Observable<Chatroom[]> {
     return this.http.get<Chatroom[]>(this.uriList);
   }
+
+  getChatroom(id: String): Observable<Chatroom> {
+    return this.http.get<Chatroom>(this.uriGet + id);
+  }
 }
diff --git a/src/app/chatroom/chatroom.component.html b/src/app/chatroom/chatroom.component.html
new file mode 100644 (file)
index 0000000..a45ee7b
--- /dev/null
@@ -0,0 +1,7 @@
+<div class="panel panel-primary">
+  <div class="panel-heading">
+    <h3 class="panel-title">{{chatroom.name}}</h3>
+  </div>
+  <div class="panel-body">
+  </div>
+</div>
diff --git a/src/app/chatroom/chatroom.component.less b/src/app/chatroom/chatroom.component.less
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/app/chatroom/chatroom.component.spec.ts b/src/app/chatroom/chatroom.component.spec.ts
new file mode 100644 (file)
index 0000000..6ebaacf
--- /dev/null
@@ -0,0 +1,23 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { ChatroomComponent } from './chatroom.component';
+
+describe('ChatroomComponent', () => {
+  let component: ChatroomComponent;
+  let fixture: ComponentFixture<ChatroomComponent>;
+
+  beforeEach(async () => {
+    await TestBed.configureTestingModule({
+      declarations: [ ChatroomComponent ]
+    })
+    .compileComponents();
+
+    fixture = TestBed.createComponent(ChatroomComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  });
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});
diff --git a/src/app/chatroom/chatroom.component.ts b/src/app/chatroom/chatroom.component.ts
new file mode 100644 (file)
index 0000000..51aff66
--- /dev/null
@@ -0,0 +1,37 @@
+import { Component, OnInit } from '@angular/core';
+import { ActivatedRoute } from '@angular/router';
+import { ChatroomService } from "../chatroom.service";
+import { UserService } from "../user.service";
+import { Chatroom } from "../chatroom";
+
+@Component({
+  selector: 'app-chatroom',
+  templateUrl: './chatroom.component.html',
+  styleUrls: ['./chatroom.component.less']
+})
+export class ChatroomComponent implements OnInit {
+
+  chatroom: Chatroom = { id: 'FOO', name: 'BAR'};
+
+  constructor(
+    private chatroomsService: ChatroomService,
+    private userService: UserService,
+    private route: ActivatedRoute) {}
+
+  ngOnInit(): void
+  {
+    this.userService.assertUserisKnown(() => this.getChatroom());
+  }
+
+  getChatroom(): void {
+    const id: string | null = this.route.snapshot.paramMap.get('id');
+    if (id === null) {
+      console.log("ID for chatroom is missing in URI");
+    }
+    else {
+      this.chatroomsService
+        .getChatroom(id)
+        .subscribe(chatroom => this.chatroom = chatroom);
+    }
+  }
+}
index c265675..4df38de 100644 (file)
@@ -3,6 +3,6 @@
     <h3 class="panel-title">Please Choose a Chat-Room</h3>
   </div>
   <div class="panel-body">
-    <button *ngFor="let chatroom of chatrooms" type="button" class="btn btn-default btn-lg btn-block">{{chatroom.name}}</button>
+    <button *ngFor="let chatroom of chatrooms" routerLink="/chatroom/{{chatroom.id}}" type="button" class="btn btn-default btn-lg btn-block">{{chatroom.name}}</button>
   </div>
 </div>
index 99eb15a..26665da 100644 (file)
@@ -8,7 +8,7 @@ import { ChatroomService } from "../chatroom.service";
   templateUrl: './chatrooms.component.html',
   styleUrls: ['./chatrooms.component.less']
 })
-export class ChatroomsComponent  implements OnInit
+export class ChatroomsComponent implements OnInit
 {
   chatrooms: Chatroom[] = [];