From 72d6d2d76c0a55faaa16c5e7761326f51317e5a1 Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 26 Nov 2024 00:48:14 +0700 Subject: [PATCH] add more logics --- frontend/src/message.jsx | 62 ++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/frontend/src/message.jsx b/frontend/src/message.jsx index aa7612e..622f2b8 100644 --- a/frontend/src/message.jsx +++ b/frontend/src/message.jsx @@ -1,7 +1,7 @@ -import { useState, useEffect } from 'react'; -import ChatList from './components/ChatList'; -import ChatBox from './components/ChatBox'; -import axios from 'axios'; +import { useState, useEffect } from "react"; +import ChatList from "./components/ChatList"; +import ChatBox from "./components/ChatBox"; +import axios from "axios"; // Schemas for Inbox // MessageID: int @@ -11,63 +11,77 @@ import axios from 'axios'; // otherUser: { // UserID: int, UserName, ProfileImage +// Example of chats +// const chats = [ +// { UserID: 1, UserName: 'Alice', ProfileImage: '/profile1.jpg', }, ] + const Message = () => { - const [activeChat, setActiveChat] = useState(null); + const [activeUser, setActiveUser] = useState(null); const [userID, setUserID] = useState(null); const [chats, setChats] = useState([]); - // Example of chats - // const chats = [ - // { UserID: 1, UserName: 'Alice', ProfileImage: '/profile1.jpg', }, ] - const fetchChats = async () => { try { - const response = await axios.get(`http://localhost:3000/message/inbox/${userID}`); + const response = await axios.get( + `http://localhost:3000/message/inbox/${userID}` + ); console.log(response.data); - const chats = response.data.map(chat => ({ + const chats = response.data.map((chat) => ({ UserID: chat.otherUser.UserID, UserName: chat.otherUser.UserName, ProfileImage: chat.otherUser.ProfileImage, - MessageContent: chat.MessageContent + MessageContent: chat.MessageContent, })); setChats(chats); - console.log(chats); - // setChats(response.data); } catch (error) { - console.error('Error fetching chats:', error); + console.error("Error fetching chats:", error); } }; + const fetchMessages = async (receiverID) => { + try { + const response = await axios.get( + `http://localhost:3000/message/${userID}/${receiverID}` + ); + console.log(response.data); + } catch (error) { + console.error("Error fetching messages:", error); + } + } + useEffect(() => { - const user = localStorage.getItem('UserID'); + const user = localStorage.getItem("UserID"); if (user) { setUserID(user); } - }, []) + }, []); useEffect(() => { fetchChats(); }, [userID]); - const handleChatSelect = (contact) => { - setActiveChat(contact); + const handleChatSelect = (receiverData) => { + setActiveUser(receiverData); + fetchMessages(receiverData.UserID); }; return (
- {chats ? ( + {chats.length > 0 ? (
- +
) : (
Friends -

Try to make new friends by Matching Feature

+

+ Try to make new friends by Matching Feature +

@@ -77,4 +91,4 @@ const Message = () => { ); }; -export default Message; \ No newline at end of file +export default Message;