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 @@ +
+ +
+

{{author.name}}

+

{{lastMessage}}

+
+
\ No newline at end of file diff --git a/src/components/ChatRecipientCard/ChatRecipientCard.ts b/src/components/ChatRecipientCard/ChatRecipientCard.ts new file mode 100644 index 00000000..ab9f46b3 --- /dev/null +++ b/src/components/ChatRecipientCard/ChatRecipientCard.ts @@ -0,0 +1,70 @@ +import ChatRepository from '../../repositories/ChatRepository'; +import BaseComponent from '../BaseComponent/BaseComponent'; +import ChatWindow from '../ChatWindow/ChatWindow'; + +export default class ChatRecipientCard extends BaseComponent { + private authorInfo: { + name: string; + id: string; + avatar: string; + }; + private lastMessage: string; + + constructor( + parent: HTMLElement, + id: string, + authorInfo: { + name: string; + id: string; + avatar: string; + }, + lastMessage: string + ) { + super({ + parent: parent, + id: id, + templateName: 'ChatRecipientCard', + templateData: { + author: authorInfo, + lastMessage: lastMessage, + }, + }); + + this.authorInfo = authorInfo; + this.lastMessage = lastMessage; + } + + protected addEventListeners(): void { + this.thisElement.onclick = async () => { + // Remove active class from already selected chat list item + document + .querySelector('.recipient-card--active') + ?.classList.remove('recipient-card--active'); + + this.thisElement.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(this.authorInfo.id); + const chatWindow = new ChatWindow( + document.getElementById('ChatPage-')!, + this.authorInfo.id, + this.authorInfo.name, + data + ); + + chatWindow.on('new-message', (message) => { + if (typeof message !== 'string') return; + ( + document.getElementById( + `recipient-${this.authorInfo.id}-last-message` + ) as HTMLElement + ).textContent = message; + }); + + chatWindow.render(); + }; + } +} diff --git a/src/index.ts b/src/index.ts index 9b6a49ce..5efcf2cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,10 +43,6 @@ function renderMapPage(adId?: string) { mapPage.render(pageContainer); } -const renderArticlesPage = () => {}; - -const renderMessagesPage = () => {}; - const renderFavoritesPage = () => { const favouritePage = new FavouritePage(); favouritePage.render(pageContainer); @@ -199,7 +195,7 @@ router.addRoute('/chats', async (params: URLSearchParams) => { const recipientId = params.get('recipientId') as string; const data = await ChatRepository.getAll(); - const chatPage = new ChatPage(pageContainer, data, recipientId); + const chatPage = new ChatPage(pageContainer, data.chats, recipientId); chatPage.render(); }); diff --git a/src/pages/ChatPage/ChatPage.hbs b/src/pages/ChatPage/ChatPage.hbs index a9bed4ea..e9017b0b 100644 --- a/src/pages/ChatPage/ChatPage.hbs +++ b/src/pages/ChatPage/ChatPage.hbs @@ -1,24 +1,5 @@
-
- {{#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(); + } } }