11import { ChatMessage } from "@/types/chatmessage.type" ;
2- import { ChatRoom } from "@/types/chatroom.type" ;
3- import { unique } from "@/utils/unique_array " ;
2+ import { ChatRoom , ChatRoomWithNotifications } from "@/types/chatroom.type" ;
3+ import { createUniqueMessages } from "@/utils/unreadMessages " ;
44import { createAsyncThunk , createSlice } from "@reduxjs/toolkit" ;
55export interface ChatState {
6- chats : ChatRoom [ ] ;
6+ chats : ChatRoomWithNotifications [ ] ;
77 chatsAreLoading : boolean ;
88 chatIsLoading : boolean ;
99 messageSending : boolean ;
@@ -17,6 +17,21 @@ const initialState: ChatState = {
1717 messageSending : false ,
1818} ;
1919
20+ const setMessagesForChat = (
21+ chat : ChatRoomWithNotifications ,
22+ activeChatId ?: string ,
23+ messagesToAdd ?: ChatMessage [ ]
24+ ) => {
25+ const { messages, noNewMessages } = createUniqueMessages (
26+ chat . messages ,
27+ messagesToAdd
28+ ) ;
29+ chat . messages = messages ;
30+ if ( activeChatId != chat . id ) {
31+ chat . unreadMessages = noNewMessages ;
32+ }
33+ } ;
34+
2035export const getAllChats = createAsyncThunk ( "chats/getAll" , async ( _ , s ) => {
2136 let token = window . sessionStorage . getItem ( "token" ) ;
2237 const response = await fetch (
@@ -30,8 +45,10 @@ export const getAllChats = createAsyncThunk("chats/getAll", async (_, s) => {
3045 if ( response . status > 299 ) {
3146 throw new Error ( "Request failed with " + response . status ) ;
3247 }
33- const data = await response . json ( ) ;
34- console . log ( data ) ;
48+ const data = ( await response . json ( ) ) as ChatRoomWithNotifications [ ] ;
49+ data . forEach ( ( chat ) => {
50+ chat . unreadMessages = 0 ;
51+ } ) ;
3552 if ( data . length > 0 ) {
3653 s . dispatch ( getChatById ( { chatId : data [ 0 ] . id } ) ) ;
3754 }
@@ -103,16 +120,21 @@ export const chatSlice = createSlice({
103120 reducers : {
104121 setActiveChatId : ( state , action : { payload : string } ) => {
105122 state . activeChatId = action . payload ;
123+ const chat = state . chats . find ( ( x ) => x . id == action . payload ) ;
124+ if ( chat != null ) {
125+ chat . unreadMessages = 0 ;
126+ }
106127 } ,
107128 updateChatRoomMessagesSync : ( state , action : { payload : ChatRoom } ) => {
108129 const chatRoom = state . chats . find ( ( x ) => x . id == action . payload . id ) ;
109130 if ( chatRoom != null ) {
110- chatRoom . messages = unique (
111- [ ...( chatRoom . messages ?? [ ] ) , ...( action . payload . messages ?? [ ] ) ] ,
112- ( x ) => x . id
131+ setMessagesForChat (
132+ chatRoom ,
133+ state . activeChatId ,
134+ action . payload . messages
113135 ) ;
114136 } else {
115- state . chats . push ( action . payload ) ;
137+ state . chats . push ( { ... action . payload , unreadMessages : 1 } ) ;
116138 }
117139 } ,
118140 } ,
@@ -142,11 +164,7 @@ export const chatSlice = createSlice({
142164 state . chatIsLoading = false ;
143165 const chat = state . chats . find ( ( x ) => x . id == action . payload . chatId ) ;
144166 if ( chat != null ) {
145- const prevMessages = chat . messages ?? [ ] ;
146- chat . messages = unique (
147- [ ...prevMessages , ...action . payload . messages ] ,
148- ( x ) => x . id
149- ) ;
167+ setMessagesForChat ( chat , state . activeChatId , action . payload . messages ) ;
150168 }
151169 }
152170 ) ;
0 commit comments