From 543dc2b9fb8ef1b980fb7b63e5885b1a417a2b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Thu, 5 Jun 2025 14:34:53 +0200 Subject: [PATCH] list channels --- .../channel-entry.component.scss | 4 +- .../channel-list/channel-list.component.ts | 4 +- src/app/services/chat.service.ts | 49 ++++++++++--------- src/app/services/responses/chat.ts | 6 +++ 4 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 src/app/services/responses/chat.ts diff --git a/src/app/chat/chat/channel-list/channel-entry/channel-entry.component.scss b/src/app/chat/chat/channel-list/channel-entry/channel-entry.component.scss index addffd9..4fa2a70 100644 --- a/src/app/chat/chat/channel-list/channel-entry/channel-entry.component.scss +++ b/src/app/chat/chat/channel-list/channel-entry/channel-entry.component.scss @@ -1,5 +1,5 @@ button { - width: 70%; + width: 95%; display: block; - margin: 10px auto; + margin: 5px auto; } \ No newline at end of file diff --git a/src/app/chat/chat/channel-list/channel-list.component.ts b/src/app/chat/chat/channel-list/channel-list.component.ts index 81658ab..39059a9 100644 --- a/src/app/chat/chat/channel-list/channel-list.component.ts +++ b/src/app/chat/chat/channel-list/channel-list.component.ts @@ -1,4 +1,4 @@ -import { Component, EventEmitter, OnChanges, OnDestroy, OnInit, Output } from '@angular/core'; +import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core'; import { Channel } from '../../../models/channel'; import { ChatService } from '../../../services/chat.service'; import { Subject, takeUntil } from 'rxjs'; @@ -20,7 +20,7 @@ export class ChannelListComponent implements OnInit, OnDestroy { constructor(private chatService: ChatService) { } ngOnInit() { - this.chatService.ListChannels() + this.chatService.ListAvailableChannels() .pipe(takeUntil(this.destroy)) .subscribe(channels => { this.channels = channels; diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index 8e82b80..6a37247 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -1,37 +1,38 @@ import { Injectable } from '@angular/core'; -import { from, Observable } from 'rxjs'; +import { catchError, from, map, Observable, throwError } from 'rxjs'; import { Channel } from '../models/channel'; import { Message } from '../models/message'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../../environment/environment'; +import { ListAvailableChannelsResponse } from './responses/chat'; @Injectable({ providedIn: 'root' }) export class ChatService { - constructor() { } + constructor(private http: HttpClient) { } - // TODO: implement - public ListChannels(): Observable { - return new Observable(subscriber => { - subscriber.next([ - { - id: 0, - name: 'default', - description: 'this is the default channel' - }, - { - id: 1, - name: 'XIV. Leo', - description: 'this is another channel' - }, - ]); - subscriber.complete(); - }); + public ListAvailableChannels(): Observable { + let url = `${environment.apiBase}/chat/channels` + return this.http.get(url, { withCredentials: true }) + .pipe( + map(response => { + if (response.error) { + throw new Error(response.error); + } + + if (response.channels) { + return response.channels; + } + + throw new Error("bad API response, missing channels with no error"); + }), + catchError(error => throwError(() => new Error(error.error.message))) + ); } // TODO: implement - // TODO: refactor this so it first returns the n last messages, - // then listens for incoming messages and forwards them as they come public GetMessages(channelID: number): Observable { return from([ @@ -44,21 +45,21 @@ export class ChatService { }, { id: 1, - sender: 'admin', + sender: 'alice', channel: 1, time: new Date(), content: 'this is my first message' }, { id: 1, - sender: 'admin', + sender: 'bob', channel: 1, time: new Date(), content: 'this is my first message' }, { id: 1, - sender: 'admin', + sender: 'charlie', channel: 1, time: new Date(), content: 'this is my first message' diff --git a/src/app/services/responses/chat.ts b/src/app/services/responses/chat.ts new file mode 100644 index 0000000..f980204 --- /dev/null +++ b/src/app/services/responses/chat.ts @@ -0,0 +1,6 @@ +import { Channel } from "../../models/channel"; +import { APIResponse } from "./basic"; + +export class ListAvailableChannelsResponse extends APIResponse { + public channels?: Channel[]; +} \ No newline at end of file