diff --git a/front-end/src/ChatPage.js b/front-end/src/ChatPage.js index 45bdaa9..2df8c16 100644 --- a/front-end/src/ChatPage.js +++ b/front-end/src/ChatPage.js @@ -4,24 +4,22 @@ import './ChatPage.css'; import axios from 'axios' import Header from "./Header"; import { socket } from './sockets/ReactSocket'; -import { ConnectionState } from './sockets/ConnectionState'; import { ChatBoxSender, ChatBoxReceiver } from './sockets/ChatBox'; import InputTxt from './sockets/InputTxt'; function ChatPage() { const [messagesarray, setMessages2] = useState([]); - const [socketConnected, setIsConnected] = useState(socket.connected); const [user, setUser] = useState('') const [chats, setChats] = useState([]); useEffect(() => { function onConnect() { - setIsConnected(true); + //setIsConnected(true); } function onDisconnect() { - setIsConnected(false); + //setIsConnected(false); } socket.on('connect', onConnect); @@ -31,7 +29,19 @@ function ChatPage() { socket.off('connect', onConnect); socket.off('disconnect', onDisconnect); } - }, []) + }, []); + + useEffect(() => { + getUser(); + + axios.get('http://localhost:3001/chatpage') + .then(response => { + setMessages2(response.data); + }) + .catch(error => { + console.error('Error fetching messages:', error); + }); + }, []); //listen for chat_message event from the socket useEffect(() => { @@ -62,16 +72,16 @@ function ChatPage() { //called everytime message is sent/received //the post request to the backend with the new message should probably go here function sendMessage(msg) { - //get the current time here - const newMsg = { ...msg, user}; //this will eventually also have info about the user that send the message - console.log(`Sending message as ${user}`); //this is how you extract the message out of newMsg + const msgTime = new Date().toLocaleTimeString(); + const newMsg = { ...msg, user, msgTime}; + console.log(`Sending message as ${user}`); setChats([...chats, newMsg]); sendToSocket([...chats, newMsg]); let messagestring = newMsg.message; const currentTime = new Date().toISOString(); //This should be formatted eventually (go see what it looks like in the database messages collection) const msg_post = { - sender: "current_user_test", //CHNAGE THIS LATER TO ACTUAL CURRENT USERNAME!!!! + sender: newMsg.user, //CHNAGE THIS LATER TO ACTUAL CURRENT USERNAME!!!! recipient: "recipient_test", //CHANGE THIS LATER TO ACTUAL RECIPIENT!!! timestamp: currentTime, messagetext: messagestring @@ -90,26 +100,13 @@ function ChatPage() { } //displays the chat messages to the user - //this will ultimately need to be updated when we get login working function ChatExchange() { return chats.map((chat, index) => { - if(chat.user === user) return - return + if(chat.user === user) return + return }); } - useEffect(() => { - getUser(); - - axios.get('http://localhost:3001/chatpage') - .then(response => { - setMessages2(response.data); - }) - .catch(error => { - console.error('Error fetching messages:', error); - }); - }, []); - return (
@@ -117,24 +114,6 @@ function ChatPage() {
); - - /* - return ( -
- -
- {messagesarray.map((msg, index) => ( -
-

{msg.sender} [{msg.timestamp}]: {msg.messagetext}

-
- ))} -
- setValue(e.target.value)} /> - -
-
- ); - */ } export default ChatPage; \ No newline at end of file diff --git a/front-end/src/sockets/ChatBox.js b/front-end/src/sockets/ChatBox.js index b31f166..c365099 100644 --- a/front-end/src/sockets/ChatBox.js +++ b/front-end/src/sockets/ChatBox.js @@ -1,12 +1,12 @@ import React from "react"; import './ChatBox.css' -export function ChatBoxSender({user, message}) { +export function ChatBoxSender({user, time, message}) { return (

- {user} [10:00 AM] + {user} [{time}]
{message} @@ -15,12 +15,12 @@ export function ChatBoxSender({user, message}) { ); } -export function ChatBoxReceiver({user, message}) { +export function ChatBoxReceiver({user, time, message}) { return (

- {user} [10:00 AM] + {user} [{time}]
{message}