Skip to content
Open
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
18,604 changes: 18,558 additions & 46 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
"@material-ui/icons": "^4.11.2",
"axios": "^0.21.1",
"bootstrap": "^4.5.3",
"cloudinary": "^1.28.1",
"form-data": "^4.0.0",
"idb-keyval": "^4.0.1",
"jquery": "^3.5.1",
"materialize-css": "^1.0.0-rc.2",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-avatar": "^3.9.7",
"react-bootstrap": "^2.1.1",
"react-dom": "^16.8.6",
"react-loader-spinner": "^3.1.14",
"react-loading-overlay": "^1.0.1",
"react-markdown": "^5.0.3",
"react-multi-select-component": "^3.0.8",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-script-tag": "^1.1.2",
"react-scripts": "3.0.1",
"react-spinners": "^0.10.4",
"react-tilt": "^0.1.4",
Expand Down
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Signup from "./components/Signup";
import { debounce } from "lodash";
import $ from "jquery";
import { Switch, Route, HashRouter } from "react-router-dom";
import Admin from "./components/adminPages/admin";

function App() {
useEffect(() => {
Expand Down Expand Up @@ -36,6 +37,7 @@ function App() {
<Route exact path="/" component={Login} />
<Route path="/signup" component={Signup} />
<Route path="/chat" component={Chat} />
<Route path="/admin" component={Admin} />
</Switch>
</HashRouter>
</div>
Expand Down
140 changes: 102 additions & 38 deletions src/components/ChatRoom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useState, useEffect, useReducer } from "react";
import React, { useState, useEffect, useRef } from "react";
import FormData from "form-data";
import { ProgressBar } from "react-bootstrap";
import "./css/ChatRoom.css";
import $ from "jquery";
import Message from "./Message";
Expand All @@ -10,26 +12,21 @@ import BounceLoader from "react-spinners/BounceLoader";
import Navbar from "./navbar";
import axios from "axios";
import config from "../config.json";
import { flushSync } from "react-dom";

const socket = getSocket();

// $(function () {
// if ($("#ms-menu-trigger")[0]) {
// $("body").on("clixck", "#ms-menu-trigger", function () {
// $(".ms-menu").toggleClass("toggled");
// });
// }
// });

const ChatRoom = ({ token, user }) => {
const [isActive, setActive] = useState(false);
const [clickedRoomName, setClickedRoomName] = useState("");
const [clickedRoomMessages, setClickedRoomMessages] = useState([]);
const [message, setMessage] = useState("");
const [clickedRoomMembers, setClickedRoomMembers] = useState([]);
const [clickedMemberIdForDm, setClickedMemberIdForDm] = useState("");
const [clickedMemberNameForDm, setClickedMemberNameForDm] = useState("");
const [rooms, setRooms] = useState([]);
const [clickedRoomId, setClickedRoomId] = useState("");
const [percentage, setPercentage] = useState(0);
const acknowledgement = (ack) => {
if (ack) {
alert(ack);
Expand Down Expand Up @@ -80,12 +77,24 @@ const ChatRoom = ({ token, user }) => {
}
};

const messagesEndRef = useRef(null);
const scrollToBottom = () => {
if(messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({ block: 'nearest', inline: 'start' })
}
};

useEffect(() => {
scrollToBottom()
}, [clickedRoomMessages]);

useEffect(() => {
if (!clickedRoomId) return;
getMessages(clickedRoomId);
}, [clickedRoomId]);

const getMessages = async (clickedRoomId) => {

const res = await axios.post(
`${config.server}/messages/`,
{
Expand All @@ -105,21 +114,86 @@ const ChatRoom = ({ token, user }) => {

useEffect(() => {
getDmRoom(clickedMemberIdForDm, clickedMemberNameForDm);
console.log('Clicked member id for dm');
}, [clickedMemberIdForDm, setClickedMemberNameForDm]);

const renderMessages = ({ senderId, content, type, createdAt, senderName}, index ) => {

return (
<Message
senderId={senderId}
content={content}
createdAt={createdAt}
user={user}
senderName={senderName}
key={index}
/>
)
}


// Socket io implementation.
const sendMessage = () => {
const content = document.getElementById("box").value;
if (/\S/.test(content)) {
socket.emit("message", {
senderId: user._id,
content,
createdAt: Date().toLocaleString(),
senderName: user.fullName,
});
document.getElementById("box").value = "";
}
var content = document.getElementById("box").value;

socket.emit("message", {
senderId: user._id,
content,
type: "text",
createdAt: Date().toLocaleString(),
senderName: user.fullName,
});
document.getElementById("box").value = "";
setMessage("");
document.getElementById("file-image").value = '';
content=""
};

const selectFile = async (e) => {
const files = Array.from(e.target.files)
setMessage(files[0].name)
const file = files[0]

var bodyFormData = new FormData()

bodyFormData.append('photo', file)

const options = {
headers: {
Authorization: token ? `Bearer ${token}` : "",
"Content-type": "multipart/form-data",
},
onUploadProgress: (progressEvent) => {
const { loaded, total } = progressEvent
let percent = Math.floor(loaded * 100 / total)
console.log(`${percent} %`)

if(percent < 100) {
setPercentage(percent)
}
}
}

const res = await axios.post(
`${config.server}/sendPhoto/`,
bodyFormData,
options
);
var content = res.data.url;
socket.emit("message", {
senderId: user._id,
content,
type: "file",
createdAt: Date().toLocaleString(),
senderName: user.fullName,
})
setPercentage(0)
document.getElementById("box").value = "";
setMessage("");
document.getElementById("file-image").value = '';
content=""
}

useEffect(() => {
socket.on("message", (data) => {
const { senderId, content, createdAt, senderName } = data;
Expand Down Expand Up @@ -177,7 +251,6 @@ const ChatRoom = ({ token, user }) => {
) : (
<span></span>
)}
{console.log(clickedRoomMembers)}
{clickedRoomMembers.length > 4 &&
clickedRoomMembers.map((clickedRoomMember, index) =>
clickedRoomMember !== user._id &&
Expand Down Expand Up @@ -218,35 +291,26 @@ const ChatRoom = ({ token, user }) => {
<div className="messages">
<div className="reverse" id="messages">
{clickedRoomMessages &&
clickedRoomMessages.map(
(
{ senderId, content, createdAt, senderName },
index
) => (
<Message
senderId={senderId}
content={content}
createdAt={createdAt}
user={user}
senderName={senderName}
key={index}
/>
)
)}
clickedRoomMessages.map(renderMessages)}
<div ref={messagesEndRef} />
{/* The above line for reference has been added for messagesEnd. */}
</div>
</div>
{/* MESSAGES END */}
<div>
{percentage > 0 && <ProgressBar animated now={percentage} label={`${percentage}%`} />}
</div>

<div className="msb-reply">
<textarea
placeholder="What's on your mind..."
defaultValue={""}
id="box"
onKeyDown={(event) => onEnterPress(event)}
/>
<input id="file-image" onChange={selectFile} type="file" name="avatar"></input>
<button
onClick={() => {
sendMessage();
}}
onClick={() => sendMessage() }
>
<i className="fa fa-paper-plane-o" />
</button>
Expand Down
22 changes: 13 additions & 9 deletions src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ const Login = () => {
email,
password,
});
if (res.status === 200) {
if (res.status === 200) {
const { token, user } = res.data;
console.log("Successfully Signed in");
console.log("Successfully Signed in",res.data.user.role);
localStorage.setItem("token", token);
localStorage.setItem("user", JSON.stringify(user));
history.push("/chat");
if (res.data.user.role === "admin") {
history.push("/admin")
}else{
history.push("/chat");}
} else {
console.log(res);
}
} catch (error) {
if(error.response.status === 401) {
alert("Wrong Password");
} else {
console.log(error.response);
}
// if(error.response.status === 401) {
// alert("Wrong Password");
// } else {
// console.log(error.response);
// }
console.log(error)
}
};
return (
Expand Down Expand Up @@ -122,4 +126,4 @@ const Login = () => {
);
};

export default Login;
export default Login;
77 changes: 56 additions & 21 deletions src/components/Message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import ReactMarkdown from "react-markdown";
import { Link } from "react-router-dom";
import "./css/ChatRoom.css";
import gfm from "remark-gfm";

Expand Down Expand Up @@ -35,32 +36,66 @@ function Message({ senderId, content, createdAt, user, senderName }) {
// useEffect(() => {
// getUserInfo();
// });
if (user._id === senderId) {
return (
<div className="message-feed right">
<div className="pull-right" />
<div className="media-body">
<h5>{senderName}</h5>
<div className="mf-content">
<ReactMarkdown plugins={[gfm]} children={content} />




if (content.startsWith("http://res.cloudinary.com/df0ldmkyd/image/upload")) {
if (user._id === senderId) {
return (
<div className="message-feed right">
<div className="pull-right" />
<div className="media-body">
<h5 className="text-black">{senderName}</h5>
<div className="image-file">
<a href={content} target="_blank"><img src={content} alt="image file" width={50}></img></a>
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
</div>
);
);
} else {
return (
<div className="message-feed media">
<div className="pull-left" />
<div className="media-body">
<h5 className="text-black">{senderName}</h5>
<div className="image-file">
<a href={content} target="_blank"><img src={content} alt="image file" height={200} width={200}></img></a>
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
</div>
);
}
} else {
return (
<div className="message-feed media">
<div className="pull-left" />
<div className="media-body">
<h5>{senderName}</h5>
<div className="mf-content">
<ReactMarkdown plugins={[gfm]} children={content} />
if (user._id === senderId) {
return (
<div className="message-feed right">
<div className="pull-right" />
<div className="media-body">
<h5>{senderName}</h5>
<div className="mf-content">
<ReactMarkdown plugins={[gfm]} children={content} />
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
</div>
);
} else {
return (
<div className="message-feed media">
<div className="pull-left" />
<div className="media-body">
<h5>{senderName}</h5>
<div className="mf-content">
<ReactMarkdown plugins={[gfm]} children={content} />
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
<small className="mf-date">{formatAMPM(new Date(createdAt))}</small>
</div>
</div>
);
);
}
}
}
export default Message;
Loading