|
| 1 | +import { ChatRoom } from "@/types/chatroom.type"; |
| 2 | +import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; |
| 3 | +export interface ChatState { |
| 4 | + chats: ChatRoom[]; |
| 5 | + chatsLoading: boolean; |
| 6 | + messageSending: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +const initialState: ChatState = { |
| 10 | + chats: [], |
| 11 | + chatsLoading: false, |
| 12 | + messageSending: false, |
| 13 | +}; |
| 14 | + |
| 15 | +export const getAllChats = createAsyncThunk("chats/getAll", async () => { |
| 16 | + let token = window.sessionStorage.getItem("token"); |
| 17 | + |
| 18 | + const response = await fetch( |
| 19 | + "https://" + window.envUrl + "/api/v1/chats/query/chats/", |
| 20 | + { |
| 21 | + headers: { |
| 22 | + Authorization: `Bearer ${token}`, |
| 23 | + }, |
| 24 | + } |
| 25 | + ); |
| 26 | + if (response.status > 299) { |
| 27 | + throw new Error("Request failed with " + response.status); |
| 28 | + } |
| 29 | + const data = await response.json(); |
| 30 | + console.log(data); |
| 31 | + return data; |
| 32 | +}); |
| 33 | + |
| 34 | +export const addMessage = createAsyncThunk( |
| 35 | + "chats/add", |
| 36 | + async (payload: { text?: string; imageBase64?: string }) => { |
| 37 | + /*let token = window.sessionStorage.getItem("token"); |
| 38 | + const tokenDec = jwtDecode(token!); |
| 39 | + const chat = { |
| 40 | + id: uuidv4(), |
| 41 | + text: payload.text, |
| 42 | + image_base64: payload.imageBase64, |
| 43 | + user_id: tokenDec.sub, |
| 44 | + change_date: new Date().toISOString(), |
| 45 | + } as ChatRoom; |
| 46 | + const response = await fetch( |
| 47 | + "https://" + window.envUrl + "/api/v1/chats/command/chats/", |
| 48 | + { |
| 49 | + headers: { |
| 50 | + Authorization: `Bearer ${token}`, |
| 51 | + }, |
| 52 | + body: JSON.stringify(chat), |
| 53 | + method: "Chat", |
| 54 | + } |
| 55 | + ); |
| 56 | + if (response.status > 299) { |
| 57 | + throw new Error("Request failed with " + response.status); |
| 58 | + } |
| 59 | + return chat;*/ |
| 60 | + } |
| 61 | +); |
| 62 | + |
| 63 | +export const chatSlice = createSlice({ |
| 64 | + name: "chats", |
| 65 | + initialState, |
| 66 | + reducers: { |
| 67 | + addChatSync: (state, action) => { |
| 68 | + state.chats.unshift(action.payload); |
| 69 | + }, |
| 70 | + }, |
| 71 | + extraReducers: (builder) => { |
| 72 | + builder.addCase(getAllChats.pending, (state) => { |
| 73 | + state.chatsLoading = true; |
| 74 | + }); |
| 75 | + builder.addCase(getAllChats.rejected, (state) => { |
| 76 | + state.chatsLoading = false; |
| 77 | + }); |
| 78 | + builder.addCase(getAllChats.fulfilled, (state, action) => { |
| 79 | + state.chatsLoading = false; |
| 80 | + state.chats = action.payload; |
| 81 | + }); |
| 82 | + builder.addCase(addMessage.pending, (state) => { |
| 83 | + state.messageSending = true; |
| 84 | + }); |
| 85 | + builder.addCase(addMessage.rejected, (state, err) => { |
| 86 | + state.messageSending = false; |
| 87 | + console.log(err); |
| 88 | + }); |
| 89 | + builder.addCase(addMessage.fulfilled, (state) => { |
| 90 | + state.messageSending = false; |
| 91 | + }); |
| 92 | + }, |
| 93 | +}); |
| 94 | + |
| 95 | +export const { addChatSync } = chatSlice.actions; |
| 96 | + |
| 97 | +export default chatSlice.reducer; |
0 commit comments