Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Discussion page chat toggle #233

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 78 additions & 34 deletions client/src/component/Discussion.jsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,67 @@
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import './css/Discussion.css';
import { io } from 'socket.io-client';
import { useEffect, useState } from "react";
import PropTypes from "prop-types";
import "./css/Discussion.css";
import { io } from "socket.io-client";
// AUDIO
import recieveMsg from '../assets/music/recieveMsg.mp3';
import userJoin from '../assets/music/userJoin.mp3';
import userLeft from '../assets/music/userLeft.mp3';
import InputModal from './InputModal';
import recieveMsg from "../assets/music/recieveMsg.mp3";
import userJoin from "../assets/music/userJoin.mp3";
import userLeft from "../assets/music/userLeft.mp3";
import InputModal from "./InputModal";

// CHAT-PATTERN-IMAGE
import chatPatternLight from "../assets/images/Discussion/chat-pattern.png";
import chatPatternDark from "../assets/images/Discussion/chat-pattern-dark.png";

// Create a Socket
const socket = io("http://localhost:5000", { transports: ["websocket"] });

const Discussion = (props) => {
const [messages, setMessages] = useState([]); // State to store chat messages
const [messageInput, setMessageInput] = useState(''); // State to store user input message
const [userName, setUserName] = useState(''); // State to store user's name
const [messageInput, setMessageInput] = useState(""); // State to store user input message
const [userName, setUserName] = useState(""); // State to store user's name
const [isModalOpen, setIsModalOpen] = useState(false);

useEffect(() => {
setIsModalOpen(true);

socket.on('user-joined', name => {
socket.on("user-joined", (name) => {
// Update messages state with the new user's joining message
setMessages(prevMessages => [...prevMessages, { content: `${name} joined the chat`, position: 'center1' }]);
setMessages((prevMessages) => [
...prevMessages,
{ content: `${name} joined the chat`, position: "center1" },
]);
// Play audio when a user joins
playAudio(userJoin);
});

// Event listener for receiving a message from the server
socket.on('receive', data => {
socket.on("receive", (data) => {
// Update messages state with the received message
setMessages(prevMessages => [...prevMessages, { content: `${data.name}: ${data.message}`, position: 'left' }]);
setMessages((prevMessages) => [
...prevMessages,
{ content: `${data.name}: ${data.message}`, position: "left" },
]);
// Play audio when receiving a message
playAudio(recieveMsg);
});

// Event listener for a user leaving the chat
socket.on('left', name => {
socket.on("left", (name) => {
// Update messages state with the user's leaving message
setMessages(prevMessages => [...prevMessages, { content: `${name} left the chat`, position: 'center2' }]);
setMessages((prevMessages) => [
...prevMessages,
{ content: `${name} left the chat`, position: "center2" },
]);
// Play audio when a user leaves
playAudio(userLeft);
});

// Clean up socket listeners when component unmounts
return () => {
socket.off('user-joined');
socket.off('receive');
socket.off('left');
socket.off("user-joined");
socket.off("receive");
socket.off("left");
};

}, []);

// Function to handle audio playback
Expand All @@ -62,47 +74,79 @@ const Discussion = (props) => {
const handleSubmit = (e) => {
e.preventDefault();
const message = messageInput.trim();
if (message !== '') {
if (message !== "") {
// Append your own message immediately to avoid waiting for the server
setMessages(prevMessages => [...prevMessages, { content: `You: ${message}`, position: 'right' }]);
socket.emit('send', message); // Emit 'send' event to the server
setMessageInput(''); // Clear the message input field
setMessages((prevMessages) => [
...prevMessages,
{ content: `You: ${message}`, position: "right" },
]);
socket.emit("send", message); // Emit 'send' event to the server
setMessageInput(""); // Clear the message input field
}
};
const handleJoin = (name) => {
setUserName(name);
socket.emit('join', name); // Notify the server that a user has joined
socket.emit("join", name); // Notify the server that a user has joined
setIsModalOpen(false); // Close the modal after a successful join
};
};

return (
<div className='discussion-main-container mt-20 mb-[50px]'>
<div className="discussion-main-container mt-20 mb-[50px]">
<InputModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onSubmit={handleJoin}
/>

<div className='discussion-container-section'>
<div className="discussion-container-section">
{/* Container for displaying chat messages */}
<div className="discussion-container" style={{ color: props.mode === 'dark' ? 'black' : '' }}>
<div
className="discussion-container"
style={{
color: props.mode === "dark" ? "black" : "",
backgroundImage: // method to change chat image based on theme mode
props.mode === "dark"
? `url(${chatPatternDark})`
: `url(${chatPatternLight})`,
}}
>
<div className="welcome-center fs-3 mt-3">{`Welcome ${userName} to the Bitbox Community`}</div>
{messages.map((message, index) => (
<div key={index} className={`message ${message.position}`}>{message.content}</div>
<div
key={index}
className={`message ${message.position}`}
>
{message.content}
</div>
))}
</div>

{/* Form for sending messages */}
<div className="discussion-send pb-[20px]">
<form id="discussion-send-container pb-[20px]" onSubmit={handleSubmit}>
<input style={{ color: props.mode === 'dark' ? 'black' : '' }}
<form
id="discussion-send-container pb-[20px]"
onSubmit={handleSubmit}
>
<input
style={{
color: props.mode === "dark" ? "black" : "",
}}
type="text"
name="messageImp"
id="messageInp"
placeholder="Type a message"
value={messageInput}
onChange={e => setMessageInput(e.target.value)}
onChange={(e) => setMessageInput(e.target.value)}
/>
<button className="discussion-btn" type="submit" style={{ color: props.mode === 'dark' ? 'black' : '' }}>Send</button>
<button
className="discussion-btn"
type="submit"
style={{
color: props.mode === "dark" ? "black" : "",
}}
>
Send
</button>
</form>
</div>
</div>
Expand Down
3 changes: 1 addition & 2 deletions client/src/component/css/Discussion.css
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
}

/* Third Container */

#discussion-send-container {
position: relative;
top: 0.7rem;
Expand Down Expand Up @@ -157,4 +156,4 @@
width: 80px;
font-weight: bolder;
font-family: cursive;
}
}