Skip to content

Commit

Permalink
improve chat (pls test there seem to be some bugs...)
Browse files Browse the repository at this point in the history
  • Loading branch information
halbekanne committed Dec 28, 2023
1 parent a4fd83f commit 3b39b1f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@angular/router": "^16.1.4",
"@ngneat/until-destroy": "^9.2.2",
"@ngrx/component-store": "^16.1.0",
"@state-adapt/angular": "^1.2.1",
"@state-adapt/core": "^1.2.1",
"@state-adapt/rxjs": "^1.2.1",
"@state-adapt/angular": "^2.0.5",
"@state-adapt/core": "^2.0.5",
"@state-adapt/rxjs": "^2.0.5",
"bootstrap-icons": "^1.11.2",
"date-fns": "^2.29.3",
"hammerjs": "^2.0.8",
Expand Down
30 changes: 16 additions & 14 deletions src/app/chat/data-access/chat-state.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,23 @@ export const chatStateAdapter = createAdapter<ChatAdaptState>()({
...state,
}),

setMessagesAsRead: (state, _chatId: string) => ({
setMessagesAsRead: (state, { chatId, profileId }) => ({
...state,
chats: state.chats.map((chat) => {
if (chat.id !== chatId) return chat;
return {
...chat,
lastMessage: chat.lastMessage
? {
...(chat.lastMessage || {}),
readByParticipants: [
...(chat.lastMessage?.readByParticipants || []),
profileId,
],
}
: null,
};
}),
}),

selectors: {
Expand Down Expand Up @@ -186,18 +201,5 @@ function updateChat(
}
return oldChat;

// Object.keys(newChat).forEach((key: string) => {
// const keyOfChat = key as keyof SingleChatState;
// if (keyOfChat !== 'messages' && newChat[keyOfChat] !== oldChat[keyOfChat]) {
// console.log(
// 'updating',
// keyOfChat,
// newChat[keyOfChat],
// oldChat[keyOfChat]
// );
// const newValue = newChat[keyOfChat];
// oldChat[keyOfChat] = newValue as any;
// }
// });
return oldChat;
}
18 changes: 11 additions & 7 deletions src/app/chat/data-access/chat-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ export class ChatStateService {
newMessageSent: false,
};

private chatStore = adapt(
[this.storePath, this.initialState, chatStateAdapter],
(store) => {
private chatStore = adapt(this.initialState, {
path: this.storePath,
adapter: chatStateAdapter,
sources: (store) => {
const timerService = inject(TimerService);
const chatHttpService = inject(ChatHttpService);

const fetchChatsSources = getRequestSources(
'[Chat] fetchChats',
merge(
timerService.interval('chatFetchTrigger', 10000),
timerService.interval('chatFetchTrigger', 20000),
store.chatCreated$.pipe(filter((hasCreated) => !!hasCreated))
).pipe(
startWith(-1),
Expand Down Expand Up @@ -180,7 +181,10 @@ export class ChatStateService {
for (const message of unreadMessages) {
chatHttpService.setMessageAsRead(message.id).subscribe();
}
return of(chatId);
return of({
chatId: chatId,
profileId: this.profileService.getProfile()!.id!,
});
}),
toSource('[Chat] setMessagesAsRead')
);
Expand All @@ -202,8 +206,8 @@ export class ChatStateService {
setMessagesAsRead: setMessagesAsReadSource,
reset: this.userLoggedOut$,
};
}
);
},
});

// Selectors
// public signals for components to consume
Expand Down

0 comments on commit 3b39b1f

Please sign in to comment.