mirror of
https://github.com/Funkoala14/CreatorCenter_OOIN.git
synced 2025-06-08 05:28:14 +08:00
Update chatSlice.js
This commit is contained in:
parent
8ee06cceaa
commit
099d11ed9f
@ -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(
|
||||||
|
'chat/fetchChatDetails',
|
||||||
|
async (query, { rejectWithValue, dispatch }) => {
|
||||||
try {
|
try {
|
||||||
const response = await api.get(`/chat-history/search/`);
|
const response = await api.get('/chat-history/conversation_detail/', { params: query });
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
throw new Error(response.message);
|
throw new Error(response.message);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
dispatch(setNotificationBarMessage({ message: error.message, type: 'error' }));
|
||||||
return rejectWithValue(error.message);
|
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;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user