Skip to content

Commit

Permalink
Fix loading of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Teeesa7 committed Nov 13, 2024
1 parent 798acde commit 7e3b006
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions peer-prep-fe/src/chat-feature/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export class ChatComponent implements OnInit, OnDestroy {

this.cdr.detectChanges();
this.scrollToBottom();

console.log(sessionStorage)

}
}
Expand All @@ -105,6 +107,7 @@ export class ChatComponent implements OnInit, OnDestroy {

ngOnDestroy(): void {
if (this.messageSubscription) {
this.messageService.clearMessages()
this.messageSubscription.unsubscribe();
}
this.webSocketService.disconnect();
Expand All @@ -127,4 +130,5 @@ export class ChatComponent implements OnInit, OnDestroy {
this.myUsername = JSON.parse(userData).data.username
}
}

}
6 changes: 3 additions & 3 deletions peer-prep-fe/src/services/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export class MessageService {
private messages: { userID: string; content: string; username: string }[] = [];

constructor() {
const storedMessages = localStorage.getItem('chatMessages');
const storedMessages = sessionStorage.getItem('chatMessages');
this.messages = storedMessages ? JSON.parse(storedMessages) : [];
}
addMessage(message: { userID: string; content: string; username: string}) {
this.messages.push(message);
localStorage.setItem('chatMessages', JSON.stringify(this.messages));
sessionStorage.setItem('chatMessages', JSON.stringify(this.messages));
}

getMessages() {
Expand All @@ -21,6 +21,6 @@ export class MessageService {

clearMessages() {
this.messages = [];
localStorage.removeItem('chatMessages');
sessionStorage.removeItem('chatMessages');
}
}

0 comments on commit 7e3b006

Please sign in to comment.