CreatorCenter_OOIN/src/store/slices/chatSlice.js

145 lines
5.1 KiB
JavaScript
Raw Normal View History

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
2025-06-06 05:47:51 +08:00
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);
}
}
);
2025-05-27 00:55:28 +08:00
const initialState = {
currentChat: null,
status: 'idle',
error: null,
};
const chatSlice = createSlice({
name: 'chat',
initialState,
2025-06-06 05:47:51 +08:00
reducers: {
selectChat: (state, action) => {
state.currentChat = action.payload;
console.log(action.payload);
},
resetSelectedChat: (state) => {
state.currentChat = null;
},
},
2025-05-27 00:55:28 +08:00
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-06 05:47:51 +08:00
const { messages, negotiation } = action.payload;
state.currentChat.messages = messages;
state.currentChat.negotiation = negotiation;
2025-06-03 21:56:22 +08:00
});
builder.addCase(fetchChatDetails.rejected, (state, action) => {
2025-05-27 00:55:28 +08:00
state.status = 'failed';
state.error = action.payload;
});
2025-06-06 05:47:51 +08:00
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;
});
2025-05-27 00:55:28 +08:00
},
});
2025-06-06 05:47:51 +08:00
export const { selectChat, resetSelectedChat } = chatSlice.actions;
2025-05-27 00:55:28 +08:00
export default chatSlice.reducer;