1+ import { ChatMessage } from "@/types/chatmessage.type" ;
12import { ChatRoom } from "@/types/chatroom.type" ;
23import { createAsyncThunk , createSlice } from "@reduxjs/toolkit" ;
34export interface ChatState {
45 chats : ChatRoom [ ] ;
5- chatsLoading : boolean ;
6+ chatsAreLoading : boolean ;
7+ chatIsLoading : boolean ;
68 messageSending : boolean ;
79 activeChatId ?: string ;
810}
911
1012const initialState : ChatState = {
1113 chats : [ ] ,
12- chatsLoading : false ,
14+ chatsAreLoading : false ,
15+ chatIsLoading : false ,
1316 messageSending : false ,
1417} ;
1518
16- export const getAllChats = createAsyncThunk ( "chats/getAll" , async ( ) => {
19+ export const getAllChats = createAsyncThunk ( "chats/getAll" , async ( _ , s ) => {
1720 let token = window . sessionStorage . getItem ( "token" ) ;
18-
1921 const response = await fetch (
2022 "https://" + window . envUrl + "/api/v1/chats/query/chats/" ,
2123 {
@@ -29,9 +31,42 @@ export const getAllChats = createAsyncThunk("chats/getAll", async () => {
2931 }
3032 const data = await response . json ( ) ;
3133 console . log ( data ) ;
34+ if ( data . length > 0 ) {
35+ s . dispatch ( getChatById ( { chatId : data [ 0 ] . id } ) ) ;
36+ }
37+
3238 return data ;
3339} ) ;
3440
41+ export const getChatById = createAsyncThunk (
42+ "chats/getById" ,
43+ async ( { chatId } : { chatId : string } , t ) => {
44+ let token = window . sessionStorage . getItem ( "token" ) ;
45+
46+ const activeChatId = ( t . getState ( ) as { chats : ChatState } ) . chats
47+ . activeChatId ;
48+ if ( activeChatId == chatId ) {
49+ return { messages : [ ] , chatId } ;
50+ }
51+ t . dispatch ( setActiveChatId ( chatId ) ) ;
52+
53+ const response = await fetch (
54+ "https://" + window . envUrl + `/api/v1/chats/query/chats/${ chatId } ` ,
55+ {
56+ headers : {
57+ Authorization : `Bearer ${ token } ` ,
58+ } ,
59+ }
60+ ) ;
61+ if ( response . status > 299 ) {
62+ throw new Error ( "Request failed with " + response . status ) ;
63+ }
64+ const data = await response . json ( ) ;
65+ console . log ( data ) ;
66+ return { messages : data . messages , chatId } ;
67+ }
68+ ) ;
69+
3570export const addMessage = createAsyncThunk (
3671 "chats/add" ,
3772 async ( payload : { text ?: string ; imageBase64 ?: string } ) => {
@@ -68,18 +103,41 @@ export const chatSlice = createSlice({
68103 addChatSync : ( state , action ) => {
69104 state . chats . unshift ( action . payload ) ;
70105 } ,
106+ setActiveChatId : ( state , action : { payload : string } ) => {
107+ state . activeChatId = action . payload ;
108+ } ,
71109 } ,
72110 extraReducers : ( builder ) => {
73111 builder . addCase ( getAllChats . pending , ( state ) => {
74- state . chatsLoading = true ;
112+ state . chatsAreLoading = true ;
75113 } ) ;
76114 builder . addCase ( getAllChats . rejected , ( state ) => {
77- state . chatsLoading = false ;
115+ state . chatsAreLoading = false ;
78116 } ) ;
79117 builder . addCase ( getAllChats . fulfilled , ( state , action ) => {
80- state . chatsLoading = false ;
118+ state . chatsAreLoading = false ;
81119 state . chats = action . payload ;
82120 } ) ;
121+ builder . addCase ( getChatById . pending , ( state ) => {
122+ state . chatIsLoading = true ;
123+ } ) ;
124+ builder . addCase ( getChatById . rejected , ( state ) => {
125+ state . chatIsLoading = false ;
126+ } ) ;
127+ builder . addCase (
128+ getChatById . fulfilled ,
129+ (
130+ state ,
131+ action : { payload : { chatId : string ; messages : ChatMessage [ ] } }
132+ ) => {
133+ state . chatIsLoading = false ;
134+ const chat = state . chats . find ( ( x ) => x . id == action . payload . chatId ) ;
135+ if ( chat != null ) {
136+ const prevMessages = chat . messages ?? [ ] ;
137+ chat . messages = [ ...prevMessages , ...action . payload . messages ] ;
138+ }
139+ }
140+ ) ;
83141 builder . addCase ( addMessage . pending , ( state ) => {
84142 state . messageSending = true ;
85143 } ) ;
@@ -93,6 +151,6 @@ export const chatSlice = createSlice({
93151 } ,
94152} ) ;
95153
96- export const { addChatSync } = chatSlice . actions ;
154+ export const { addChatSync, setActiveChatId } = chatSlice . actions ;
97155
98156export default chatSlice . reducer ;
0 commit comments