Skip to content

Commit

Permalink
i think chat is working
Browse files Browse the repository at this point in the history
  • Loading branch information
Samyak-SH committed Sep 3, 2024
1 parent 0a22129 commit 51e4be7
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 16 deletions.
84 changes: 81 additions & 3 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"react-icons": "^5.3.0",
"react-phone-number-input": "^3.4.5",
"react-router-dom": "^6.26.1",
"react-toastify": "^10.0.5"
"react-toastify": "^10.0.5",
"socket.io-client": "^4.7.5"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
Expand Down
77 changes: 75 additions & 2 deletions client/src/components/Negotiate/Chat.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import './chat.css';
import { useState, useRef, useEffect } from 'react';
import io from 'socket.io-client'
import { useState, useRef, useEffect, useContext } from 'react';
import { addDoc, collection, doc, getDoc } from 'firebase/firestore';
import {db} from '../../../firebase'
import { AuthContext } from '../context/Authcontext';

export default function Chat() {
//current user context
const { currentUser } = useContext(AuthContext);
const [userType, setUserType] = useState("buyers"); //change depending upon the type of user(buyer or farmer)
const [contacts, setContacts] = useState({});
// console.log(currentUser);
console.log(contacts);
//testing values, to be removed later
const roomID = 'room1';
const SERVER_PORT = import.meta.env.VITE_SERVER_PORT;

const [socket, setSocket]= useState(null);

useEffect(()=>{
//establish new socket connection
const newSocket = io.connect(`http://localhost:${SERVER_PORT}`);
setSocket(newSocket);

//monitor messages
newSocket.on("recieve-message", (recieved_message)=>{
setMessages(prevMessages => [...prevMessages, recieved_message]);
})

//get all contacts when u load the page
getContacts(currentUser.uid, userType).then((result)=>{
setContacts(result);
});
return () => {
newSocket.disconnect();
};
},[])

Check warning on line 38 in client/src/components/Negotiate/Chat.jsx

View workflow job for this annotation

GitHub Actions / Lint Client Code

React Hook useEffect has missing dependencies: 'SERVER_PORT', 'currentUser.uid', and 'userType'. Either include them or remove the dependency array

// State for the input text and messages
const [text, setText] = useState('');
const [messages, setMessages] = useState([]);
Expand All @@ -15,8 +50,25 @@ export default function Chat() {
}, [messages]);

// Handle the click event to add a new message
const handleClick = () => {
const handleClick = async () => {
//send message to server through websocket
if (text.trim()) {

if (socket) {
socket.emit("send-message", { message: text});
}

//add message to database
try {
await addDoc(collection(db, 'chats', roomID, 'messages'), {
message: text,
timeStamp: new Date(),
user: currentUser.uid,
});
} catch (error) {
console.error("Error uploading message:", error);
}

setMessages([...messages, text]);
setText('');
}
Expand Down Expand Up @@ -56,3 +108,24 @@ export default function Chat() {
</div>
);
}

const getContacts = async (userID, userType)=>{
try {
const userDocRef = doc(db, userType, userID);
const userDoc = await getDoc(userDocRef);
if (userDoc.exists()) {
const userData = userDoc.data();
return userData.Contacts
// const contacts = userData.contacts || [];

// console.log("Contacts:", contacts);
// return contacts;
} else {
console.log("No such user!");
return [];
}
} catch (error) {
console.error("Error fetching contacts:", error);
return [];
}
}
2 changes: 0 additions & 2 deletions client/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import FDashboard from './components/Farmer/FDashboard';
import Profilesetup from './components/ProfileSetup/Profilesetup';

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<AuthProvider >
<BrowserRouter>
<Routes>
Expand All @@ -32,5 +31,4 @@ ReactDOM.createRoot(document.getElementById('root')).render(
</BrowserRouter>
</AuthProvider>

</React.StrictMode>
);
Loading

0 comments on commit 51e4be7

Please sign in to comment.