Skip to content

Commit

Permalink
Merge branch 'admin'
Browse files Browse the repository at this point in the history
  • Loading branch information
elyse502 committed Nov 7, 2024
2 parents 989ee54 + 1c797c6 commit 04560bf
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 17 deletions.
52 changes: 38 additions & 14 deletions frontend/src/components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ import { ShopContext } from "../context/ShopContext";

const Navbar = () => {
const [visible, setVisible] = useState(false);
const { setShowSearch, getCartCount } = useContext(ShopContext);
const {
setShowSearch,
getCartCount,
navigate,
token,
setToken,
setCartItems,
} = useContext(ShopContext);

const logOut = () => {
navigate("/login");
localStorage.removeItem("token");
setToken("");
setCartItems({});
};

return (
<div className="flex items-center justify-between py-5 font-medium">
Expand Down Expand Up @@ -44,20 +58,30 @@ const Navbar = () => {
/>

<div className="group relative">
<Link to={"/login"}>
<img
src={assets.profile_icon}
className="w-5 cursor-pointer"
alt="profile_icon"
/>
</Link>
<div className="group-hover:block hidden absolute dropdown-menu right-0 pt-4">
<div className="flex flex-col gap-2 w-36 py-3 px-5 bg-slate-100 text-gray-500 rounded">
<p className="cursor-pointer hover:text-black">My Profile</p>
<p className="cursor-pointer hover:text-black">Orders</p>
<p className="cursor-pointer hover:text-black">Logout</p>
<img
onClick={() => (token ? null : navigate("/login"))}
src={assets.profile_icon}
className="w-5 cursor-pointer"
alt="profile_icon"
/>

{/* Dropdown Menu */}
{token && (
<div className="group-hover:block hidden absolute dropdown-menu right-0 pt-4">
<div className="flex flex-col gap-2 w-36 py-3 px-5 bg-slate-100 text-gray-500 rounded">
<p className="cursor-pointer hover:text-black">My Profile</p>
<p
onClick={() => navigate("/orders")}
className="cursor-pointer hover:text-black"
>
Orders
</p>
<p onClick={logOut} className="cursor-pointer hover:text-black">
Logout
</p>
</div>
</div>
</div>
)}
</div>
<Link to="/cart" className="relative">
<img src={assets.cart_icon} className="w-5 min-w-5" alt="cart_icon" />
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/context/ShopContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ShopContextProvider = (props) => {
const [showSearch, setShowSearch] = useState(false);
const [cartItems, setCartItems] = useState({});
const [products, setProducts] = useState([]);
const [token, setToken] = useState("");
const navigate = useNavigate();

const addToCart = async (itemId, size) => {
Expand Down Expand Up @@ -79,7 +80,7 @@ const ShopContextProvider = (props) => {
const getProductsData = async () => {
try {
const response = await axios.get(backendUrl + "/api/product/list");
console.log(response.data);
// console.log(response.data);

if (response.data.success) {
setProducts(response.data.products);
Expand All @@ -96,6 +97,12 @@ const ShopContextProvider = (props) => {
getProductsData();
}, []);

useEffect(() => {
if (!token && localStorage.getItem("token")) {
setToken(localStorage.getItem("token"));
}
}, []);

const value = {
products,
currency,
Expand All @@ -111,6 +118,8 @@ const ShopContextProvider = (props) => {
getCartAmount,
navigate,
backendUrl,
setToken,
token,
};

return (
Expand Down
59 changes: 57 additions & 2 deletions frontend/src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
import React, { useState } from "react";
import React, { useContext, useEffect, useState } from "react";
import { ShopContext } from "../context/ShopContext";
import axios from "axios";
import { toast } from "react-toastify";

const Login = () => {
const [currentState, setCurrentState] = useState("Sign Up");
const [currentState, setCurrentState] = useState("Login");
const { token, setToken, navigate, backendUrl } = useContext(ShopContext);

const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [email, setEmail] = useState("");

const onSubmitHandler = async (event) => {
event.preventDefault();

try {
if (currentState === "Sign Up") {
const response = await axios.post(backendUrl + "/api/user/register", {
name,
email,
password,
});
// console.log(response.data);

if (response.data.success) {
setToken(response.data.token);
localStorage.setItem("token", response.data.token);
} else {
toast.error(response.data.message);
}
} else {
const response = await axios.post(backendUrl + "/api/user/login", {
email,
password,
});
// console.log(response.data);

if (response.data.success) {
setToken(response.data.token);
localStorage.setItem("token", response.data.token);
} else {
toast.error(response.data.message);
}
}
} catch (error) {
console.log(error);
toast.error(error.message);
}
};

useEffect(() => {
if (token) {
navigate("/");
}
}, [token]);

return (
<form
onSubmit={onSubmitHandler}
Expand All @@ -19,19 +68,25 @@ const Login = () => {
""
) : (
<input
onChange={(e) => setName(e.target.value)}
value={name}
type="text"
className="w-full px-3 py-2 border border-gray-800"
placeholder="Name"
required
/>
)}
<input
onChange={(e) => setEmail(e.target.value)}
value={email}
type="email"
className="w-full px-3 py-2 border border-gray-800"
placeholder="Email"
required
/>
<input
onChange={(e) => setPassword(e.target.value)}
value={password}
type="password"
className="w-full px-3 py-2 border border-gray-800"
placeholder="Password"
Expand Down

0 comments on commit 04560bf

Please sign in to comment.