import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import api from '@/services/api'; import { setNotificationBarMessage } from './notificationBarSlice'; 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); } } ); 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 = { currentChat: null, status: 'idle', error: null, }; const chatSlice = createSlice({ name: 'chat', initialState, reducers: {}, extraReducers: (builder) => { builder.addCase(fetchChatDetails.pending, (state) => { state.status = 'loading'; }); builder.addCase(fetchChatDetails.fulfilled, (state, action) => { state.status = 'succeeded'; state.currentChat = action.payload; }); builder.addCase(fetchChatDetails.rejected, (state, action) => { state.status = 'failed'; state.error = action.payload; }); }, }); export default chatSlice.reducer;