From aaf21a345dfbbb6a7bb23a0ff36d5d2a17efde08 Mon Sep 17 00:00:00 2001
From: rasulov1337 <117843890+rasulov1337@users.noreply.github.com>
Date: Wed, 18 Dec 2024 02:50:43 +0300
Subject: [PATCH] feat: enable shortpolling for updating chat list
---
.../ChatRecipientCard/ChatRecipientCard.hbs | 15 ++++
.../ChatRecipientCard/ChatRecipientCard.ts | 70 +++++++++++++++
src/index.ts | 6 +-
src/pages/ChatPage/ChatPage.hbs | 23 +----
src/pages/ChatPage/ChatPage.ts | 85 ++++++++++---------
5 files changed, 134 insertions(+), 65 deletions(-)
create mode 100644 src/components/ChatRecipientCard/ChatRecipientCard.hbs
create mode 100644 src/components/ChatRecipientCard/ChatRecipientCard.ts
diff --git a/src/components/ChatRecipientCard/ChatRecipientCard.hbs b/src/components/ChatRecipientCard/ChatRecipientCard.hbs
new file mode 100644
index 00000000..8256b082
--- /dev/null
+++ b/src/components/ChatRecipientCard/ChatRecipientCard.hbs
@@ -0,0 +1,15 @@
+
-
- {{#each chats}}
-
-
-
-
{{this.authorName}}
-
{{this.lastMessage}}
-
-
- {{/each}}
+
+ {{#each chats}}{{/each}}
\ No newline at end of file
diff --git a/src/pages/ChatPage/ChatPage.ts b/src/pages/ChatPage/ChatPage.ts
index bdc80a53..f64f3658 100644
--- a/src/pages/ChatPage/ChatPage.ts
+++ b/src/pages/ChatPage/ChatPage.ts
@@ -1,21 +1,26 @@
import BaseComponent from '../../components/BaseComponent/BaseComponent';
+import ChatRecipientCard from '../../components/ChatRecipientCard/ChatRecipientCard';
import ChatWindow from '../../components/ChatWindow/ChatWindow';
import ApiClient from '../../modules/ApiClient';
import ChatRepository, { Chat } from '../../repositories/ChatRepository';
export default class ChatPage extends BaseComponent {
+ private chats: Chat[];
+
constructor(
parent: HTMLElement,
- data: { chats: Chat[] },
+ chats: Chat[],
startChatWithRecipientId?: string
) {
super({
parent: parent,
id: '',
- templateData: data,
+ templateData: { chats: chats },
templateName: 'ChatPage',
});
+ this.chats = chats;
+
if (!startChatWithRecipientId) return;
requestAnimationFrame(async () => {
@@ -47,46 +52,48 @@ export default class ChatPage extends BaseComponent {
});
}
- protected addEventListeners(): void {
- const cards = document.getElementsByClassName(
- 'recipient-card'
- ) as HTMLCollectionOf
;
+ protected addEventListeners(): void {}
- for (const el of cards) {
- (el as HTMLElement).onclick = async (e) => {
- this.handleCardClick(e, el);
- };
- }
+ protected afterRender(): void {
+ this.renderItems();
+
+ const id = setInterval(async () => {
+ const chats = (await ChatRepository.getAll()).chats;
+
+ // That means that we are not on ChatPage
+ if (!document.getElementById('recipients-list')) {
+ clearInterval(id);
+ return;
+ }
+
+ if (chats === this.chats) {
+ return;
+ }
+
+ this.chats = chats;
+ this.renderItems();
+ }, 5_000);
}
- private async handleCardClick(e: Event, el: HTMLElement) {
- // Remove active class from already selected chat list item
- document
- .querySelector('.recipient-card--active')
- ?.classList.remove('recipient-card--active');
- el.classList.add('recipient-card--active');
-
- // Remove old Chat Window if present
- document.getElementById('ChatWindow-0')?.remove();
-
- // Create new Chat Window
- const data = await ChatRepository.get(el.dataset.id!);
- const chatWindow = new ChatWindow(
- this.thisElement,
- el.dataset.id!,
- el.dataset.name!,
- data
- );
-
- chatWindow.on('new-message', (message) => {
- if (typeof message !== 'string') return;
- (
- document.getElementById(
- `recipient-${el.dataset.id}-last-message`
- ) as HTMLElement
- ).textContent = message;
- });
+ private renderItems() {
+ const listEl = document.getElementById(
+ 'recipients-list'
+ ) as HTMLElement;
+
+ listEl.replaceChildren();
- chatWindow.render();
+ for (const card of this.chats) {
+ const el = new ChatRecipientCard(
+ listEl,
+ card.authorUuid,
+ {
+ name: card.authorName,
+ id: card.authorUuid,
+ avatar: card.authorAvatar,
+ },
+ card.lastMessage
+ );
+ el.render();
+ }
}
}