Skip to content

Commit

Permalink
api link update
Browse files Browse the repository at this point in the history
  • Loading branch information
sankhadip-roy committed Oct 25, 2024
1 parent ded2491 commit 5bf82f0
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 203 deletions.
171 changes: 95 additions & 76 deletions frontend/src/components/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,83 +1,102 @@
import React, { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import axios from 'axios';
import { useRecoilState } from 'recoil';
import { userloggedin } from '../atom/userAtom';
import axios from "axios";
import { useRecoilState } from "recoil";
import { userloggedin } from "../atom/userAtom";

export default function Login() {
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [user, setUser] = useRecoilState(userloggedin);
const navigate = useNavigate();
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [user, setUser] = useRecoilState(userloggedin);
const navigate = useNavigate();

const handleSubmit = async (e) => {
e.preventDefault();
await axios.post("http://localhost:3001/login", { name, password })
.then(result => {
if (result.data.stat === "Success") {
// console.log(result); //log
setUser(result.data.name);
// localStorage.setItem('user', JSON.stringify(user)); // locally storing the logged in user
navigate("/");
alert('Login successful: ' + result.data.name); //using result.data.name instead of user because useRecoilState is a asyncronous function taking time in updating the user variable
} else {
alert("Invalid credentials. Please try again.");
}
})
.catch(err => {
console.error(err);
alert("An error occurred. Please try again.");
});
}
const handleSubmit = async (e) => {
e.preventDefault();
await axios
.post(
"https://sankha-chatappb-serveit.codecult.tech/login",
//.post("http://localhost:3001/login", //for local development
{
name,
password,
}
)
.then((result) => {
if (result.data.stat === "Success") {
// console.log(result); //log
setUser(result.data.name);
// localStorage.setItem('user', JSON.stringify(user)); // locally storing the logged in user
navigate("/");
alert("Login successful: " + result.data.name); //using result.data.name instead of user because useRecoilState is a asyncronous function taking time in updating the user variable
} else {
alert("Invalid credentials. Please try again.");
}
})
.catch((err) => {
console.error(err);
alert("An error occurred. Please try again.");
});
};

return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="px-8 py-6 mt-4 text-left bg-white shadow-lg rounded-lg">
<h3 className="text-2xl font-bold text-center text-gray-800">Login to your account</h3>
<form onSubmit={handleSubmit}>
<div className="mt-4">
<div>
<label className="block text-sm font-medium text-gray-700" htmlFor="name">
Username
</label>
<input
type="text"
placeholder="Enter Username"
name="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600"
required
/>
</div>
<div className="mt-4">
<label className="block text-sm font-medium text-gray-700" htmlFor="password">
Password
</label>
<input
type="password"
placeholder="Enter Password"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600"
required
/>
</div>
<div className="flex items-baseline justify-between">
<button
type="submit"
className="px-6 py-2 mt-4 text-white bg-blue-600 rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-600 focus:ring-opacity-50"
>
Login
</button>
<Link to="/register" className="text-sm text-blue-600 hover:underline">
Don't have an account?
</Link>
</div>
</div>
</form>
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="px-8 py-6 mt-4 text-left bg-white shadow-lg rounded-lg">
<h3 className="text-2xl font-bold text-center text-gray-800">
Login to your account
</h3>
<form onSubmit={handleSubmit}>
<div className="mt-4">
<div>
<label
className="block text-sm font-medium text-gray-700"
htmlFor="name"
>
Username
</label>
<input
type="text"
placeholder="Enter Username"
name="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600"
required
/>
</div>
</div>
);
}
<div className="mt-4">
<label
className="block text-sm font-medium text-gray-700"
htmlFor="password"
>
Password
</label>
<input
type="password"
placeholder="Enter Password"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600"
required
/>
</div>
<div className="flex items-baseline justify-between">
<button
type="submit"
className="px-6 py-2 mt-4 text-white bg-blue-600 rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-600 focus:ring-opacity-50"
>
Login
</button>
<Link
to="/register"
className="text-sm text-blue-600 hover:underline"
>
Don't have an account?
</Link>
</div>
</div>
</form>
</div>
</div>
);
}
83 changes: 43 additions & 40 deletions frontend/src/components/OnlineUsers.jsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
import { io } from "socket.io-client";
import React, { useEffect, useMemo } from 'react';
import React, { useEffect, useMemo } from "react";
import { useRecoilState } from "recoil";
import { onlineUsersData } from "../atom/onlineUsersAtom";

const socket = io.connect("http://localhost:3001");
// const socket = io.connect("http://localhost:3001"); //for local development
const socket = io.connect("https://sankha-chatappb-serveit.codecult.tech"); //for production

const OnlineUsers = React.memo(() => {
const [onlineUsersList, setOnlineUsersList] = useRecoilState(onlineUsersData);
const [onlineUsersList, setOnlineUsersList] = useRecoilState(onlineUsersData);

useEffect(() => {
socket.on("online-users", (data) => {
setOnlineUsersList(data);
});
return () => {
socket.off("online-users"); //run before component removed from the ui
};
}, [onlineUsersList]); //tracking onlineUsers array
useEffect(() => {
socket.on("online-users", (data) => {
setOnlineUsersList(data);
});
return () => {
socket.off("online-users"); //run before component removed from the ui
};
}, [onlineUsersList]); //tracking onlineUsers array

const memoizedUsers = useMemo(() => {
return onlineUsersList.map((user, index) => (
<div key={index} className="">
{index + 1}.&nbsp;
<span>{user}</span>
</div>
));
}, [onlineUsersList]);
const memoizedUsers = useMemo(() => {
return onlineUsersList.map((user, index) => (
<div key={index} className="">
{index + 1}.&nbsp;
<span>{user}</span>
</div>
));
}, [onlineUsersList]);

return (
<>
<div>
<h3 className="font-bold pb-2">Online Users Socket Id [{Math.floor(onlineUsersList.length / 2)}]</h3>
<div className="rounded-md overflow-y-auto max-h-64" style={{
border: '1px solid black',
padding: '10px'
}}>
{
onlineUsersList.length === 0 ? (<div>Fetching ...</div>) :
(
<div>
{memoizedUsers}
</div>
)
}
</div>
</div>
</>
)
return (
<>
<div>
<h3 className="font-bold pb-2">
Online Users Socket Id [{Math.floor(onlineUsersList.length / 2)}]
</h3>
<div
className="rounded-md overflow-y-auto max-h-64"
style={{
border: "1px solid black",
padding: "10px",
}}
>
{onlineUsersList.length === 0 ? (
<div>Fetching ...</div>
) : (
<div>{memoizedUsers}</div>
)}
</div>
</div>
</>
);
});

export default OnlineUsers;
export default OnlineUsers;
Loading

0 comments on commit 5bf82f0

Please sign in to comment.