Skip to content

Commit

Permalink
Merge pull request #110 from NashTech-Labs/feature/userDetail
Browse files Browse the repository at this point in the history
added login, register, dropdown
  • Loading branch information
ankit-mogha authored Feb 21, 2024
2 parents 54772ef + 73b2b16 commit 0fc6148
Show file tree
Hide file tree
Showing 15 changed files with 819 additions and 251 deletions.
463 changes: 241 additions & 222 deletions car-ui-react/data/db.json

Large diffs are not rendered by default.

160 changes: 159 additions & 1 deletion car-ui-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion car-ui-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-free": "^6.5.1",
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@fortawesome/react-fontawesome": "^0.2.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap-icons": "^1.11.3",
"mdb-react-ui-kit": "^7.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.8",
"react-toastify": "^9.1.3",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
78 changes: 78 additions & 0 deletions car-ui-react/src/components/Elements/DropdownLoggedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Link, useNavigate } from "react-router-dom";
import { useEffect, useState } from "react";

export const DropdownLoggedIn = ({ setDropdown }) => {
const naviagte = useNavigate();
const [userEmail, setUserEmail] = useState("");

useEffect(() => {
// Fetch user data from db.json
const fetchUserData = async () => {
try {
const response = await fetch("http://localhost:8000/users");
const userData = await response.json();

// Assuming sessionStorage has been set after login
const userId = sessionStorage.getItem("userId");

// Find the user with the matching id
const user = userData.find((user) => user.id === Number(userId));

// Update the userEmail state with the user's email
setUserEmail(user.email);
} catch (error) {
console.error("Error fetching user data:", error);
}
};

fetchUserData();
}, []);

function handleLogout() {
sessionStorage.removeItem("userName");
sessionStorage.removeItem("userId");
setDropdown(false);
naviagte("/");
}
return (
<div
id="dropdownAvatar"
className="select-none absolute top-10 right-0 z-10 w-44 bg-white rounded divide-y divide-gray-100 shadow dark:bg-gray-700 dark:divide-gray-600"
>
<div className="py-3 px-4 text-sm text-gray-900 dark:text-white">
<div className="font-medium truncate">{userEmail}</div>
</div>
<ul
className="py-1 text-sm text-gray-700 dark:text-gray-200"
aria-labelledby="dropdownUserAvatarButton"
>
<li>
<Link
onClick={() => setDropdown(false)}
to="/products"
className="block py-2 px-4 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white"
>
All Cars
</Link>
</li>
<li>
<Link
onClick={() => setDropdown(false)}
to="/dashboard"
className="block py-2 px-4 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white"
>
Dashboard
</Link>
</li>
</ul>
<div className="py-1">
<span
onClick={handleLogout}
className="cursor-pointer block py-2 px-4 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
Log out
</span>
</div>
</div>
);
};
19 changes: 19 additions & 0 deletions car-ui-react/src/components/Elements/DropdownLoggedOut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Link } from "react-router-dom";

export const DropdownLoggedOut = ({setDropdown}) => {
return (
<div id="dropdownAvatar" className="select-none absolute top-10 right-0 z-10 w-44 bg-white rounded divide-y divide-gray-100 shadow dark:bg-gray-700 dark:divide-gray-600">
<ul className="py-1 text-sm text-gray-700 dark:text-gray-200" aria-labelledby="dropdownUserAvatarButton">
<li>
<Link onClick={() => setDropdown(false)} to="/products" className="block py-2 px-4 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">All Cars</Link>
</li>
<li>
<Link onClick={() => setDropdown(false)} to="/login" className="block py-2 px-4 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Login</Link>
</li>
<li>
<Link onClick={() => setDropdown(false)} to="/register" className="block py-2 px-4 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Register</Link>
</li>
</ul>
</div>
)
}
8 changes: 6 additions & 2 deletions car-ui-react/src/components/Layout/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import Logo from "../../assets/logo.png";
import {useCart} from "../../context";
import { DropdownLoggedOut, DropdownLoggedIn } from "../index";

export const Header = () => {
const [darkMode, setDarkMode] = useState(JSON.parse(localStorage.getItem("darkMode")) || false);
const [dropdown, setDropdown] = useState(false);
const {cartList} = useCart();

const id = JSON.parse(sessionStorage.getItem("userId"));

useEffect(() => {
localStorage.setItem("darkMode", JSON.stringify(darkMode));

Expand All @@ -33,7 +36,8 @@ export const Header = () => {
<span className="text-white text-sm absolute -top-1 left-2.5 bg-rose-500 px-1 rounded-full ">{cartList.length}</span>
</span>
</Link>
<span className="bi bi-person-circle cursor-pointer text-2xl text-gray-700 dark:text-white"></span>
<span onClick={()=> setDropdown(!dropdown)} className="bi bi-person-circle cursor-pointer text-2xl text-gray-700 dark:text-white"></span>
{ dropdown && ( id ? <DropdownLoggedIn setDropdown={setDropdown} /> : <DropdownLoggedOut setDropdown={setDropdown} />) }
</div>
</div>
</nav>
Expand Down
4 changes: 3 additions & 1 deletion car-ui-react/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { Header } from "./Layout/Header"
export { Footer } from "./Layout/Footer"
export { ProductCard } from "./Elements/ProductCard"
export { Rating } from "./Elements/Rating";
export { DropdownLoggedIn } from "./Elements/DropdownLoggedIn";
export {DropdownLoggedOut } from "./Elements/DropdownLoggedOut";
export { Rating } from "./Elements/Rating";
Loading

0 comments on commit 0fc6148

Please sign in to comment.