diff --git a/client/src/components/Negotiate/Chat.jsx b/client/src/components/Negotiate/Chat.jsx index 4783b58..30c1c9e 100644 --- a/client/src/components/Negotiate/Chat.jsx +++ b/client/src/components/Negotiate/Chat.jsx @@ -1,7 +1,7 @@ import styles from './chat.module.css'; import io from 'socket.io-client' import { useState, useRef, useEffect, useContext } from 'react'; -import { addDoc, collection, doc, getDoc } from 'firebase/firestore'; +import { addDoc, collection, doc, getDoc, orderBy, query, getDocs } from 'firebase/firestore'; import { db } from '../../../firebase'; import { AuthContext } from '../context/Authcontext'; import { useNavigate } from 'react-router-dom'; @@ -19,13 +19,15 @@ export default function Chat() { const { currentUser } = useContext(AuthContext); const [socket, setSocket] = useState(null); - const [currentRoomID, setRoomID] = useState(1); + const [currentRoomID, setRoomID] = useState(null); const [prevRoomID, setPrevRoomID] = useState(null); const [userType, setUserType] = useState('buyers'); const [contacts, setContacts] = useState(null); const [message,setmessage]=useState(); + const[messages, setMessages] = useState(); const navigate = useNavigate(); const [currperson,setperson]=useState(); + const [loading, setLoading] = useState(true); useEffect(()=>{ if(!currentUser){ @@ -37,6 +39,7 @@ export default function Chat() { loadContacts(currentUser.uid, userType).then((result)=>{ setContacts(result); + setLoading(false); }) console.log("current user id ", currentUser.uid); @@ -49,7 +52,7 @@ export default function Chat() { useEffect(()=>{ if(contacts!=null){ - console.log("contacts ", contacts); + // console.log("contacts ", contacts); } },[contacts]) @@ -82,14 +85,17 @@ export default function Chat() { }, [socket]) useEffect(()=>{ - //switch rooms/contacts - if(socket){ - switchRooms(socket, prevRoomID, currentRoomID); - loadMessage(currentRoomID); - } + if(socket){ + switchRooms(socket, prevRoomID, currentRoomID); + loadMessage(currentRoomID).then((result)=>{setMessages(result)}); + } }, [currentRoomID]) + useEffect(()=>{ + console.log(messages); + }, [messages]) + const handleClick = async ()=>{ //broadcast message @@ -101,6 +107,15 @@ export default function Chat() { const handleChange = (event) => { setmessage(event.target.value); }; + + if (loading) { + return