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