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); } } ); export const fetchConversationSummary = createAsyncThunk( 'chat/fetchConversationSummary', async (conversation_id, { rejectWithValue, dispatch }) => { try { const response = await api.get(`/gmail/conversations/summary/${conversation_id}/`); console.log(response); if (response.code === 200) { return response.data; } console.log(response); throw new Error(response.message); } catch (error) { console.log(error); dispatch(setNotificationBarMessage({ message: error.message, type: 'error' })); return rejectWithValue(error.message); } } ); export const getRecommendedReply = createAsyncThunk( 'chat/getRecommendedReply', async (query, { rejectWithValue, dispatch }) => { try { const response = await api.post('/gmail/recommended-reply/', 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); } } ); const initialState = { currentChat: null, status: 'idle', error: null, }; const chatSlice = createSlice({ name: 'chat', initialState, reducers: { selectChat: (state, action) => { state.currentChat = action.payload; console.log(action.payload); }, resetSelectedChat: (state) => { state.currentChat = null; }, }, extraReducers: (builder) => { builder.addCase(fetchChatDetails.pending, (state) => { state.status = 'loading'; }); builder.addCase(fetchChatDetails.fulfilled, (state, action) => { state.status = 'succeeded'; const { messages, negotiation } = action.payload; state.currentChat.messages = messages; state.currentChat.negotiation = negotiation; }); builder.addCase(fetchChatDetails.rejected, (state, action) => { state.status = 'failed'; state.error = action.payload; }); builder.addCase(fetchConversationSummary.pending, (state) => { state.status = 'loading'; }); builder.addCase(fetchConversationSummary.fulfilled, (state, action) => { state.status = 'succeeded'; state.currentChat.summary = action.payload; }); builder.addCase(fetchConversationSummary.rejected, (state, action) => { state.currentChat.summary_error = action.payload; }); builder.addCase(getRecommendedReply.pending, (state) => { state.status = 'loading'; }); builder.addCase(getRecommendedReply.fulfilled, (state, action) => { state.status = 'succeeded'; state.currentChat.recommended_reply = action.payload; }); builder.addCase(getRecommendedReply.rejected, (state, action) => { state.currentChat.recommended_reply_error = action.payload; }); }, }); export const { selectChat, resetSelectedChat } = chatSlice.actions; export default chatSlice.reducer;