mirror of
https://github.com/Funkoala14/CreatorCenter_OOIN.git
synced 2025-06-08 09:28:15 +08:00
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
|
import { createSlice } from '@reduxjs/toolkit';
|
||
|
|
||
|
export const fetchChats = createAsyncThunk('chat/fetchChats', async (_, { rejectWithValue }) => {
|
||
|
try {
|
||
|
const response = await api.get(`/chat-history/search/`);
|
||
|
if (response.code === 200) {
|
||
|
return response.data;
|
||
|
}
|
||
|
throw new Error(response.message);
|
||
|
} catch (error) {
|
||
|
return rejectWithValue(error.message);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const initialState = {
|
||
|
chats: [],
|
||
|
currentChat: null,
|
||
|
status: 'idle',
|
||
|
error: null,
|
||
|
};
|
||
|
|
||
|
const chatSlice = createSlice({
|
||
|
name: 'chat',
|
||
|
initialState,
|
||
|
reducers: {},
|
||
|
extraReducers: (builder) => {
|
||
|
builder.addCase(fetchChats.pending, (state) => {
|
||
|
state.status = 'loading';
|
||
|
})
|
||
|
builder.addCase(fetchChats.fulfilled, (state, action) => {
|
||
|
state.status = 'succeeded';
|
||
|
state.chats = action.payload;
|
||
|
})
|
||
|
builder.addCase(fetchChats.rejected, (state, action) => {
|
||
|
state.status = 'failed';
|
||
|
state.error = action.payload;
|
||
|
});
|
||
|
},
|
||
|
});
|
||
|
|
||
|
export default chatSlice.reducer;
|