diff --git a/frontend/src/components/ChatBox.jsx b/frontend/src/components/ChatBox.jsx index 66abf85..73c006b 100644 --- a/frontend/src/components/ChatBox.jsx +++ b/frontend/src/components/ChatBox.jsx @@ -1,5 +1,5 @@ import PropTypes from 'prop-types'; -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import axios from 'axios'; const ChatBox = ({ activeUser, messages, handleSendMessage }) => { @@ -18,7 +18,11 @@ const ChatBox = ({ activeUser, messages, handleSendMessage }) => { } }; - fetchProfileImage(UserID); + useEffect(() => { + if (UserID) { + fetchProfileImage(UserID); + } + }, [UserID]); const handleSubmit = (e) => { e.preventDefault(); diff --git a/frontend/src/message.jsx b/frontend/src/message.jsx index becdccd..8065644 100644 --- a/frontend/src/message.jsx +++ b/frontend/src/message.jsx @@ -20,59 +20,22 @@ const Message = () => { const [userID, setUserID] = useState(null); const [chats, setChats] = useState([]); const [messages, setMessages] = useState([]); - const [isLoading, setIsLoading] = useState(false); - const [error, setError] = useState(null); - - // First add this loading spinner component at the top of your file - const LoadingSpinner = () => ( -
-
-
- ); const fetchChats = async () => { - setIsLoading(true); - setError(null); - try { - // Configure axios with timeout and retry - const instance = axios.create({ - timeout: 5000, - baseURL: 'http://localhost:3000' - }); - - const response = await instance.get(`/message/inbox/${userID}`); - - if (!response.data) { - throw new Error('No data received from server'); - } - + const response = await axios.get( + `http://localhost:3000/message/inbox/${userID}` + ); const chats = response.data.map((chat) => ({ - UserID: chat.otherUser?.UserID, - UserName: chat.otherUser?.UserName, - ProfileImage: chat.otherUser?.ProfileImage || '/default-profile.jpg', + UserID: chat.otherUser.UserID, + UserName: chat.otherUser.UserName, + ProfileImage: chat.otherUser.ProfileImage, MessageContent: chat.MessageContent, })); - setChats(chats); } catch (error) { - let errorMessage = 'An error occurred while fetching chats'; - - if (axios.isAxiosError(error)) { - if (error.code === 'ECONNABORTED') { - errorMessage = 'Request timed out'; - } else if (error.response) { - errorMessage = `Server error: ${error.response.status}`; - } else if (error.request) { - errorMessage = 'No response from server'; - } - } - - setError(errorMessage); - console.error("Error fetching chats:", errorMessage); - } finally { - setIsLoading(false); + console.error("Error fetching chats:", error); } }; @@ -94,7 +57,11 @@ const Message = () => { } }, []); - fetchChats(); + useEffect(() => { + if (userID) { + fetchChats(); + } + }, [userID]); const handleChatSelect = (receiverData) => { setActiveUser(receiverData); @@ -114,7 +81,6 @@ const Message = () => { }}; return ( -
{chats.length > 0 ? (