Update chatSlice.js

This commit is contained in:
susie-laptop 2025-06-03 09:56:22 -04:00
parent 8ee06cceaa
commit 099d11ed9f

View File

@ -1,19 +1,56 @@
import { createSlice } from '@reduxjs/toolkit';
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import api from '@/services/api';
import { setNotificationBarMessage } from './notificationBarSlice';
export const fetchChats = createAsyncThunk('chat/fetchChats', async (_, { rejectWithValue }) => {
try {
const response = await api.get(`/chat-history/search/`);
if (response.code === 200) {
return response.data;
export const fetchChatDetails = createAsyncThunk(
'chat/fetchChatDetails',
async (query, { rejectWithValue, dispatch }) => {
try {
const response = await api.get('/chat-history/conversation_detail/', { params: query });
if (response.code === 200) {
return response.data;
}
throw new Error(response.message);
} catch (error) {
dispatch(setNotificationBarMessage({ message: error.message, type: 'error' }));
return rejectWithValue(error.message);
}
throw new Error(response.message);
} catch (error) {
return rejectWithValue(error.message);
}
});
);
export const sendEMailToCreator = createAsyncThunk(
'chat/sendEMailToCreator',
async (query, { rejectWithValue, dispatch }) => {
try {
const response = await api.post('/chat-history/send_email_to_creator/', query);
if (response.code === 200) {
return response.data;
}
throw new Error(response.message);
} catch (error) {
dispatch(setNotificationBarMessage({ message: error.message, type: 'error' }));
return rejectWithValue(error.message);
}
}
);
export const createConversation = createAsyncThunk(
'chat/createConversation',
async (id, { rejectWithValue, dispatch }) => {
try {
const response = await api.post('/chat-history/create_conversation/', { negotiation_id: id });
if (response.code === 200) {
return response.data;
}
throw new Error(response.message);
} catch (error) {
dispatch(setNotificationBarMessage({ message: error.message, type: 'error' }));
return rejectWithValue(error.message);
}
}
);
const initialState = {
chats: [],
currentChat: null,
status: 'idle',
error: null,
@ -24,14 +61,14 @@ const chatSlice = createSlice({
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(fetchChats.pending, (state) => {
builder.addCase(fetchChatDetails.pending, (state) => {
state.status = 'loading';
})
builder.addCase(fetchChats.fulfilled, (state, action) => {
});
builder.addCase(fetchChatDetails.fulfilled, (state, action) => {
state.status = 'succeeded';
state.chats = action.payload;
})
builder.addCase(fetchChats.rejected, (state, action) => {
state.currentChat = action.payload;
});
builder.addCase(fetchChatDetails.rejected, (state, action) => {
state.status = 'failed';
state.error = action.payload;
});