Skip to content

Commit

Permalink
fixed glitching issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshPlays committed Oct 23, 2024
1 parent c331a92 commit 7f26782
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 126 deletions.
14 changes: 7 additions & 7 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ const Layout = ({ children, mode, setProgress, toggleMode, showAlert }) => {
const hideFooterRoutes = ["/login", "/signup", "/forgot-password"];

return (
<>
<div className="h-full w-full">
{/* Conditionally render the Navbar */}
{!hideNavbarRoutes.includes(location.pathname) && (
{/* {!hideNavbarRoutes.includes(location.pathname) && ( */}
<Navbar
title='BITBOX'
home='Home'
Expand All @@ -60,16 +60,16 @@ const Layout = ({ children, mode, setProgress, toggleMode, showAlert }) => {
mode={mode}
toggleMode={toggleMode}
/>
)}
{/* )} */}

{/* Main content */}
{children}

{/* Conditionally render the Footer */}
{!hideFooterRoutes.includes(location.pathname) && (
{/* {!hideFooterRoutes.includes(location.pathname) && ( */}
<Footer mode={mode} setProgress={setProgress} setAlert={showAlert} />
)}
</>
{/* )} */}
</div>
);
};

Expand Down Expand Up @@ -123,7 +123,7 @@ function App() {
};

return (
<div>
<div className="h-full w-screen">
<ProjectState>
<ProfileState>
<Router>
Expand Down
234 changes: 131 additions & 103 deletions client/src/component/Discussion.jsx
Original file line number Diff line number Diff line change
@@ -1,127 +1,155 @@
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";

// 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 [isModalOpen, setIsModalOpen] = useState(false);
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 [isModalOpen, setIsModalOpen] = useState(false);

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

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' }]);
// Play audio when a user joins
playAudio(userJoin);
});
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" },
]);
// Play audio when a user joins
playAudio(userJoin);
});

// Event listener for receiving a message from the server
socket.on('receive', data => {
// Update messages state with the received message
setMessages(prevMessages => [...prevMessages, { content: `${data.name}: ${data.message}`, position: 'left' }]);
// Play audio when receiving a message
playAudio(recieveMsg);
});
// Event listener for receiving a message from the server
socket.on("receive", (data) => {
// Update messages state with the received message
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 => {
// Update messages state with the user's leaving message
setMessages(prevMessages => [...prevMessages, { content: `${name} left the chat`, position: 'center2' }]);
// Play audio when a user leaves
playAudio(userLeft);
});
// Event listener for a user leaving the chat
socket.on("left", (name) => {
// Update messages state with the user's leaving message
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');
};

}, []);

// Function to handle audio playback
const playAudio = (audio) => {
const audioElement = new Audio(audio); // Create new Audio object
audioElement.play(); // Play the audio
// Clean up socket listeners when component unmounts
return () => {
socket.off("user-joined");
socket.off("receive");
socket.off("left");
};
}, []);

// Function to handle form submission
const handleSubmit = (e) => {
e.preventDefault();
const message = messageInput.trim();
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
}
};
const handleJoin = (name) => {
setUserName(name);
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]'>
<InputModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onSubmit={handleJoin}
/>
// Function to handle audio playback
const playAudio = (audio) => {
const audioElement = new Audio(audio); // Create new Audio object
audioElement.play(); // Play the audio
};

<div className='discussion-container-section'>
{/* Container for displaying chat messages */}
<div className="discussion-container" style={{ color: props.mode === 'dark' ? 'black' : '' }}>
<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>
// Function to handle form submission
const handleSubmit = (e) => {
e.preventDefault();
const message = messageInput.trim();
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
}
};
const handleJoin = (name) => {
setUserName(name);
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]">
<InputModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onSubmit={handleJoin}
/>

{/* 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' : '' }}
type="text"
name="messageImp"
id="messageInp"
placeholder="Type a message"
value={messageInput}
onChange={e => setMessageInput(e.target.value)}
/>
<button className="discussion-btn" type="submit" style={{ color: props.mode === 'dark' ? 'black' : '' }}>Send</button>
</form>
</div>
<div className="discussion-container-section flex items-center h-full justify-center flex-col w-full">
{/* Container for displaying chat messages */}
<div
className="discussion-container"
style={{ color: props.mode === "dark" ? "black" : "" }}
>
<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>

{/* Form for sending messages */}
<div className="discussion-send pb-[20px] pt-10 w-full">
<form
id="discussion-send-container pb-[20px]"
className="w-full items-center flex justify-center "
onSubmit={handleSubmit}
>
<input
style={{ color: props.mode === "dark" ? "black" : "" }}
type="text"
name="messageImp"
id="messageInp"
placeholder="Type a message"
className="h-"
value={messageInput}
onChange={(e) => setMessageInput(e.target.value)}
/>
<button
className="discussion-btn"
type="submit"
style={{ color: props.mode === "dark" ? "black" : "" }}
>
Send
</button>
</form>
</div>
);
</div>
</div>
);
};

// Props Validation
Discussion.propTypes = {
title: PropTypes.string,
home: PropTypes.string,
community: PropTypes.string,
discussion: PropTypes.string,
myProjects: PropTypes.string,
about: PropTypes.string,
mode: PropTypes.string,
toggleMode: PropTypes.func,
showAlert: PropTypes.func,
isAuthenticated: PropTypes.bool,
title: PropTypes.string,
home: PropTypes.string,
community: PropTypes.string,
discussion: PropTypes.string,
myProjects: PropTypes.string,
about: PropTypes.string,
mode: PropTypes.string,
toggleMode: PropTypes.func,
showAlert: PropTypes.func,
isAuthenticated: PropTypes.bool,
};

export default Discussion;
4 changes: 2 additions & 2 deletions client/src/component/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Login = ({ mode, showAlert }) => {
};

return (
<div className="h-screen">
<div className="h-screen flex items-center justify-center">
<div
className="wrapper h-3/4 mt-10"
style={{
Expand Down Expand Up @@ -119,7 +119,7 @@ const Login = ({ mode, showAlert }) => {
color: mode === "dark" ? "white" : "black",
}}>
Don&apos;t have an account?
<Link className="link text-xl" to="/Signup">
<Link className="link text-xl" to="/signup">
{" "}
Sign Up
</Link>
Expand Down
21 changes: 7 additions & 14 deletions client/src/component/Signup.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@

import { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { Input } from "antd";
import PropTypes from "prop-types";
import "./css/Signup.css";
import { registerValidation } from "../validations/validation";
import toast from "react-hot-toast";
import {
EyeInvisibleOutlined,
EyeTwoTone,
} from "@ant-design/icons";
import { EyeInvisibleOutlined, EyeTwoTone } from "@ant-design/icons";

const host = "http://localhost:5000";

const Signup = ({ mode }) => {
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const navigate = useNavigate();

const [name, setName] = useState("");
Expand Down Expand Up @@ -88,9 +82,9 @@ const Signup = ({ mode }) => {
Create An Account
</h2>

<form onSubmit={handleSubmit}>
<form className="w-3/4" onSubmit={handleSubmit}>
<div className="space-y-4 w-full">
<div className="signup-form-group">
<div className="signup-form-group items-start flex flex-col gap-2">
<label
htmlFor="name"
className="text-md leading-none font-medium"
Expand Down Expand Up @@ -120,7 +114,7 @@ const Signup = ({ mode }) => {
)}
</div>

<div className="signup-form-group">
<div className="signup-form-group items-start flex flex-col gap-2">
<label
htmlFor="email"
className="text-md leading-none font-medium"
Expand Down Expand Up @@ -150,7 +144,7 @@ const Signup = ({ mode }) => {
)}
</div>

<div className="signup-form-group relative">
<div className="signup-form-group relative items-start flex flex-col gap-2">
<label
htmlFor="password"
className="text-md font-medium"
Expand All @@ -177,10 +171,9 @@ const Signup = ({ mode }) => {
visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />
}
/>

</div>

<div className="signup-form-group relative">
<div className="signup-form-group items-start flex flex-col gap-2 relative">
<label
htmlFor="cpassword"
className="text-md font-medium"
Expand All @@ -207,7 +200,7 @@ const Signup = ({ mode }) => {
visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />
}
/>

{errors.cpassword && (
<div className="text-danger">{errors.cpassword}</div>
)}
Expand Down

0 comments on commit 7f26782

Please sign in to comment.