Skip to content

Commit

Permalink
Add proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
tanish35 committed Oct 9, 2024
1 parent 66b53c1 commit 12ad6dd
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 205 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/AddDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const AddDetails = () => {
e.preventDefault();
setLoading(true);
try {
const response = await axios.post("/user/addDetails", formData, {
const response = await axios.post("/api/user/addDetails", formData, {
withCredentials: true,
});
Toast({
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/AddUsername.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const AddUsername = () => {
e.preventDefault();
setLoading(true);
try {
const response = await axios.post("/user/addusername", formData, {
const response = await axios.post("/api/user/addusername", formData, {
withCredentials: true,
});
setLoading(false);
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const LoginPage = () => {
const result = await signInWithPopup(auth, googleProvider);
const user = result.user;
const response = await axios.post(
"/user/google",
"/api/user/google",
{
email: user.email,
displayName: user.displayName,
Expand Down Expand Up @@ -85,7 +85,7 @@ const LoginPage = () => {
const result = await signInWithPopup(auth, githubProvider);
const user = result.user;
const response = await axios.post(
"/user/github",
"/api/user/github",
{
email: user.email,
displayName: user.displayName,
Expand Down Expand Up @@ -137,7 +137,7 @@ const LoginPage = () => {
setLoading(true);
try {
const response = await axios.post(
"/user/login",
"/api/user/login",
{ email, password },
{ withCredentials: true }
);
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/components/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Posts = () => {
collegeId = selectedCommunity;
}
const response = await axios.post(
"/post/fetch",
"/api/post/fetch",
{
page: page,
collegeId,
Expand Down Expand Up @@ -81,7 +81,7 @@ const Posts = () => {
useEffect(() => {
const fetchCommunities = async () => {
try {
const response = await axios.get("/post/communities", {
const response = await axios.get("/api/post/communities", {
withCredentials: true,
});
setCommunities(response.data.college);
Expand All @@ -99,7 +99,7 @@ const Posts = () => {
useEffect(() => {
const fetchAllCommunities = async () => {
try {
const response = await axios.get("/post/allcommunities", {
const response = await axios.get("/api/post/allcommunities", {
withCredentials: true,
});
setAllCommunities(response.data.college);
Expand All @@ -119,7 +119,7 @@ const Posts = () => {

const handleLike = async (postId) => {
try {
await axios.post("/post/like", { postId }, { withCredentials: true });
await axios.post("/api/post/like", { postId }, { withCredentials: true });
setPosts((prevPosts) =>
prevPosts.map((post) => {
if (post.post_id === postId) {
Expand All @@ -135,7 +135,7 @@ const Posts = () => {

const handleCreatePost = async ({ title, content, community }) => {
const response = await axios.post(
"/post/create",
"/api/post/create",
{
title: title,
content: content,
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/components/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const RegisterForm = () => {
try {
registerSchema.parse(formData);
setErrors({});
const response = await axios.post("/user/register", formData, {
const response = await axios.post("/api/user/register", formData, {
withCredentials: true,
});
toast({
Expand All @@ -183,7 +183,9 @@ const RegisterForm = () => {

return (
<Box w="100%" maxW="500px" mx="auto" mt="5">
<FormLabel fontSize="5xl" textAlign="center" >Register</FormLabel>
<FormLabel fontSize="5xl" textAlign="center">
Register
</FormLabel>
{/* <Navbar btnName="Login"/> */}
<form onSubmit={handleSubmit}>
<VStack spacing={4}>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const SearchBar = () => {
const fetchSuggestions = async (value) => {
try {
const response = await axios.post(
"/post/search",
"/api/post/search",
{ query: value },
{ withCredentials: true }
);
Expand Down Expand Up @@ -69,7 +69,7 @@ const SearchBar = () => {
const handleSearchSubmit = async () => {
try {
const response = await axios.post(
"/post/search",
"/api/post/search",
{ query: searchTerm },
{ withCredentials: true }
);
Expand Down
16 changes: 12 additions & 4 deletions frontend/src/components/SinglePost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SinglePost = () => {
useEffect(() => {
const fetchPost = async () => {
try {
const response = await axios.get(`/post/fetch/${id}`, {
const response = await axios.get(`/api/post/fetch/${id}`, {
withCredentials: true,
});
setPost(response.data.post);
Expand All @@ -39,7 +39,7 @@ const SinglePost = () => {
const checkIfLiked = async () => {
try {
const response = await axios.post(
`/post/liked`,
`/api/post/liked`,
{ postId: id },
{ withCredentials: true }
);
Expand All @@ -58,15 +58,23 @@ const SinglePost = () => {
const handleLike = async (postId) => {
if (postLiked) {
try {
await axios.post("/post/unlike", { postId }, { withCredentials: true });
await axios.post(
"/api/post/unlike",
{ postId },
{ withCredentials: true }
);
setPost((prevPost) => ({ ...prevPost, likes: prevPost.likes - 1 }));
setPostLiked(false);
} catch (error) {
console.error("Error unliking post:", error);
}
} else {
try {
await axios.post("/post/like", { postId }, { withCredentials: true });
await axios.post(
"/api/post/like",
{ postId },
{ withCredentials: true }
);
setPost((prevPost) => ({ ...prevPost, likes: prevPost.likes + 1 }));
setPostLiked(true);
} catch (error) {
Expand Down
97 changes: 56 additions & 41 deletions frontend/src/components/chatroomui/chatroom.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ const Chatroom = () => {
const [allMsg, setAllMsg] = useState([]);
const [prevMsg, setPrevMsg] = useState([]);
const [myMsg, setMyMsg] = useState("");
const messageEndRef = useRef(null);
const {loading, userDeatils} = useUser();


const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket('ws://localhost:8080');
const messageEndRef = useRef(null);
const { loading, userDeatils } = useUser();

const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket(
"ws://localhost:8080"
);

useEffect(() => {
const fetchChatHistory = async () => {
try {
const response = await axios.get(`/chat/history/${roomId}`, {
const response = await axios.get(`/api/chat/history/${roomId}`, {
withCredentials: true,
});
setPrevMsg(response.data);

setPrevMsg(response.data);

const tosend = {
type: "joinRoom",
data: {
userId: userId,
roomId: roomId,
}
},
};
sendJsonMessage(tosend);
} catch (error) {
Expand All @@ -40,21 +39,26 @@ const Chatroom = () => {
};

fetchChatHistory();
}, [roomId, userId, sendJsonMessage]);
}, [roomId, userId, sendJsonMessage]);
useEffect(() => {
if (lastJsonMessage !== null) {
if (lastJsonMessage.type === "error") {
alert("Room not connected...");
} else if (lastJsonMessage.type === "newMessage" && lastJsonMessage.data.roomId === roomId) {
} else if (
lastJsonMessage.type === "newMessage" &&
lastJsonMessage.data.roomId === roomId
) {
const data = lastJsonMessage.data.message;


if (data.senderId !== userId) {
setAllMsg(prev => [...prev, {
senderId: data.senderId,
message: data.content,
at: data.timestamp,
}]);
setAllMsg((prev) => [
...prev,
{
senderId: data.senderId,
message: data.content,
at: data.timestamp,
},
]);
}
}
}
Expand All @@ -81,41 +85,45 @@ const Chatroom = () => {
data: {
roomId: roomId,
userId: userId,
message: myMsg
}
message: myMsg,
},
};
sendJsonMessage(tosend);


setAllMsg(prev => [...prev, {
senderId: "you",
message: myMsg,
at: new Date().toISOString(),
}]);
setMyMsg("");
setAllMsg((prev) => [
...prev,
{
senderId: "you",
message: myMsg,
at: new Date().toISOString(),
},
]);
setMyMsg("");
};

// Format date
const formatDate = (isoString) => {
const date = new Date(isoString);
return new Intl.DateTimeFormat('en-US', {
weekday: 'short',
hour: 'numeric',
minute: 'numeric',
hour12: true
return new Intl.DateTimeFormat("en-US", {
weekday: "short",
hour: "numeric",
minute: "numeric",
hour12: true,
}).format(date);
};

const constatus = {
[ReadyState.CONNECTING]: 'Connecting',
[ReadyState.OPEN]: 'Open',
[ReadyState.CLOSING]: 'Closing',
[ReadyState.CLOSED]: 'Closed',
[ReadyState.UNINSTANTIATED]: 'Uninstantiated',
[ReadyState.CONNECTING]: "Connecting",
[ReadyState.OPEN]: "Open",
[ReadyState.CLOSING]: "Closing",
[ReadyState.CLOSED]: "Closed",
[ReadyState.UNINSTANTIATED]: "Uninstantiated",
}[readyState];

// Combine and sort messages
const combinedMessages = [...prevMsg, ...allMsg].sort((a, b) => new Date(a.at) - new Date(b.at));
const combinedMessages = [...prevMsg, ...allMsg].sort(
(a, b) => new Date(a.at) - new Date(b.at)
);

return (
<div className="min-h-screen flex flex-col bg-gradient-to-br from-gray-800 to-slate-600 p-4 text-white">
Expand All @@ -137,14 +145,22 @@ const Chatroom = () => {
<div
key={index}
className={`p-4 rounded-lg my-3 shadow-md space-y-2 ${
(data.senderId === "you" || data.senderId === userId) ? "bg-green-700 ml-auto" : "bg-slate-700 mr-auto"
data.senderId === "you" || data.senderId === userId
? "bg-green-700 ml-auto"
: "bg-slate-700 mr-auto"
} max-w-xs`}
>
<div>{(data.senderId === "you" || data.senderId === userId) ? "You" : (data.senderId || data.id)}</div>
<div>
{data.senderId === "you" || data.senderId === userId
? "You"
: data.senderId || data.id}
</div>
<div>
<b>{data.message || data.content}</b>
</div>
<div className="text-sm text-gray-400">Sent at: {formatDate(data.at) || formatDate(data.timestamp)}</div>
<div className="text-sm text-gray-400">
Sent at: {formatDate(data.at) || formatDate(data.timestamp)}
</div>
</div>
))
) : (
Expand All @@ -168,7 +184,6 @@ const Chatroom = () => {
>
Send
</button>

</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 12ad6dd

Please sign in to comment.