diff --git a/src/backend/redux/api_slice.js b/src/backend/redux/api_slice.js index 14bfe0a..df00273 100644 --- a/src/backend/redux/api_slice.js +++ b/src/backend/redux/api_slice.js @@ -25,13 +25,11 @@ export const apiSlice = createApi({ query: (username) => `/chat/list/${username}`, }), postChat: builder.mutation({ - query: (body) => ({ - url: '/chat/post', + query: ({ id1, id2 }) => ({ + url: `/chat/item/${id1}/${id2}`, method: 'POST', - body, }), }), - // Add more endpoints as needed }), }); diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index 8a3e610..10942bc 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -28,22 +28,29 @@ const Home = () => { if (getError) { displayMessage(getError.message, MessageType.ERROR); } - }, [getError]); + if (getError) { + displayMessage(getError.message, MessageType.ERROR); + } + }, [getError, postError]); useEffect(() => { if (chatsData) { - setChats(chatsData.chats); + // setChats(chatsData.chats); + + let data = chatsData.chats; try { - // const sortedChats = chatsData.chats.sort((a, b) => { - // const lastMessageA = new Date(a.timestamp); - // const lastMessageB = new Date(b.timestamp); - // return lastMessageB - lastMessageA; - // }); - // // - // // console.log('sorted:', sortedChats); - // - // setChats(sortedChats); + const sortedChats = [...data].sort((a, b) => { + const lastMessageA = a.messages[a.messages.length - 1]; + const lastMessageB = b.messages[b.messages.length - 1]; + + const dateA = new Date(lastMessageA.timestamp); + const dateB = new Date(lastMessageB.timestamp); + + return dateB - dateA; + }); + + setChats(sortedChats); } catch (e) { console.error(e); } @@ -60,7 +67,7 @@ const Home = () => { displayMessage("Sent", MessageType.INFO); try { - const data = await createChat({ alias, username }).unwrap(); // Using unwrap to handle promise rejection + const data = await createChat({ id1: alias, id2: username }).unwrap(); // Using unwrap to handle promise rejection localStorage.setItem("message", "Successfully opened chat!"); localStorage.setItem("interlocutorId", alias); window.location.href = URLs.chat.url; @@ -69,8 +76,6 @@ const Home = () => { } }; - if (isGetting) return
Your chats
+Your chats
-