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 }) => { export const fetchChatDetails = createAsyncThunk(
try { 'chat/fetchChatDetails',
const response = await api.get(`/chat-history/search/`); async (query, { rejectWithValue, dispatch }) => {
if (response.code === 200) { try {
return response.data; 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 = { const initialState = {
chats: [],
currentChat: null, currentChat: null,
status: 'idle', status: 'idle',
error: null, error: null,
@ -24,14 +61,14 @@ const chatSlice = createSlice({
initialState, initialState,
reducers: {}, reducers: {},
extraReducers: (builder) => { extraReducers: (builder) => {
builder.addCase(fetchChats.pending, (state) => { builder.addCase(fetchChatDetails.pending, (state) => {
state.status = 'loading'; state.status = 'loading';
}) });
builder.addCase(fetchChats.fulfilled, (state, action) => { builder.addCase(fetchChatDetails.fulfilled, (state, action) => {
state.status = 'succeeded'; state.status = 'succeeded';
state.chats = action.payload; state.currentChat = action.payload;
}) });
builder.addCase(fetchChats.rejected, (state, action) => { builder.addCase(fetchChatDetails.rejected, (state, action) => {
state.status = 'failed'; state.status = 'failed';
state.error = action.payload; state.error = action.payload;
}); });