Skip to content

Commit

Permalink
feat: display recipient's name
Browse files Browse the repository at this point in the history
  • Loading branch information
rasulov1337 committed Dec 17, 2024
1 parent 137b6e2 commit 9659cca
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/components/ChatWindow/ChatWindow.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
</template>

<div class='chat-window' id='{{id}}'>
<div class='chat-window__upper-part'>
<p class='chat-window__recipient-name'>{{recipientName}}</p>
</div>

<div class='chat-window__messages' id='js-messages'>
<!-- Messages will be here -->
</div>
Expand Down
13 changes: 13 additions & 0 deletions src/components/ChatWindow/ChatWindow.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@
border-radius: 50%;
cursor: pointer;
}
&__upper-part {
width: 100%;
height: 50px;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
}

&__recipient-name {
font-size: 16px;
font-weight: 500;
}
}

.message {
Expand Down
32 changes: 19 additions & 13 deletions src/components/ChatWindow/ChatWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,24 @@ export default class ChatWindow extends BaseComponent {
private messagesContainer: HTMLDivElement;
private recipientId: string;

constructor(parent: HTMLElement, recipientId: string, messages: Message[]) {
constructor(
parent: HTMLElement,
recipientId: string,
recipientName: string,
messages: Message[]
) {
super({
parent: parent,
id: '0',
templateData: {},
templateData: {
recipientName,
},
templateName: 'ChatWindow',
});

this.recipientId = recipientId;
this.messages = messages;

requestAnimationFrame(() => {
this.messagesContainer = document.getElementById(
'js-messages'
) as HTMLElement;

this.displayMessageHistory();

this.messagesContainer.scrollTop =
this.messagesContainer.scrollHeight;
});

if (globalStore.chat.socket) {
globalStore.chat.socket.close();
}
Expand All @@ -44,6 +40,16 @@ export default class ChatWindow extends BaseComponent {
);
}

protected afterRender() {
this.messagesContainer = document.getElementById(
'js-messages'
) as HTMLDivElement;

this.displayMessageHistory();

this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
}

private async displayMessageHistory() {
for (const message of this.messages) {
this.addNewMessageElement(message);
Expand Down
1 change: 1 addition & 0 deletions src/pages/ChatPage/ChatPage.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div
class='recipient-card'
data-id='{{this.authorUuid}}'
data-name='{{this.authorName}}'
id='recipient-{{this.authorUuid}}'
>
<img
Expand Down
66 changes: 34 additions & 32 deletions src/pages/ChatPage/ChatPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ export default class ChatPage extends BaseComponent {
if (!startChatWithRecipientId) return;

requestAnimationFrame(async () => {
const data = await ChatRepository.get(startChatWithRecipientId);

const chatWindow = new ChatWindow(
this.thisElement,
startChatWithRecipientId,
data
);

// const data = await ChatRepository.get(startChatWithRecipientId);
// const chatWindow = new ChatWindow(
// this.thisElement,
// startChatWithRecipientId,
// data
// );
// chatWindow.on('new-message', (message) => {
// if (typeof message === 'string')
// (
Expand All @@ -34,8 +32,7 @@ export default class ChatPage extends BaseComponent {
// ) as HTMLElement
// ).textContent = message;
// });

chatWindow.render();
// chatWindow.render();
});
}

Expand All @@ -45,31 +42,36 @@ export default class ChatPage extends BaseComponent {
) as HTMLCollectionOf<HTMLElement>;

for (const el of cards) {
(el as HTMLElement).onclick = async () => {
if (!el.dataset.id) {
throw new Error('recipient id is not defined');
}
(el as HTMLElement).onclick = async (e) => {
this.handleCardClick(e, el);
};
}
}

document.getElementById('ChatWindow-0')?.remove();
const data = await ChatRepository.get(el.dataset.id);
private async handleCardClick(e: Event, el: HTMLElement) {
if (!el.dataset.id) {
throw new Error('recipient id is not defined');
}

const chatWindow = new ChatWindow(
this.thisElement,
el.dataset.id!,
data
);
document.getElementById('ChatWindow-0')?.remove();
const data = await ChatRepository.get(el.dataset.id);

chatWindow.on('new-message', (message) => {
if (typeof message === 'string')
(
document.getElementById(
`recipient-${el.dataset.id}-last-message`
) as HTMLElement
).textContent = message;
});
const chatWindow = new ChatWindow(
this.thisElement,
el.dataset.id!,
el.dataset.name!,
data
);

chatWindow.render();
};
}
chatWindow.on('new-message', (message) => {
if (typeof message !== 'string') return;
(
document.getElementById(
`recipient-${el.dataset.id}-last-message`
) as HTMLElement
).textContent = message;
});

chatWindow.render();
}
}

0 comments on commit 9659cca

Please sign in to comment.