[d7b7f00] | 1 | import React, { useState, useEffect} from "react";
|
---|
| 2 | import Logo from "../../images/logo.png";
|
---|
| 3 | import styles from "../../css/HomeCss/header.module.css";
|
---|
| 4 | import { Link, useNavigate } from 'react-router-dom';
|
---|
| 5 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
---|
| 6 | import { faCog } from '@fortawesome/free-solid-svg-icons';
|
---|
| 7 | import ShoppingCart from '../ShoppingCartComponents/ShoppingCart';
|
---|
| 8 |
|
---|
| 9 | function Header({ ingredients = [] }) {
|
---|
| 10 | const [isLoggedIn, setIsLoggedIn] = useState(false);
|
---|
| 11 | const [showDropdown, setShowDropdown] = useState(false);
|
---|
| 12 | const [user, setUser] = useState({ username: '', usersurname: '' });
|
---|
| 13 | const [role, setRole] = useState("User");
|
---|
| 14 | const navigate = useNavigate();
|
---|
| 15 |
|
---|
| 16 | useEffect(() => {
|
---|
| 17 | const token = localStorage.getItem('token');
|
---|
| 18 | if (token) {
|
---|
| 19 | setIsLoggedIn(true);
|
---|
| 20 | const userName = localStorage.getItem('userName') || 'User';
|
---|
| 21 | const userSurname = localStorage.getItem('userSurname') || 'Name';
|
---|
| 22 | setUser({ username: userName, usersurname: userSurname });
|
---|
| 23 | checkUserType(token);
|
---|
| 24 | }
|
---|
| 25 | }, []);
|
---|
| 26 |
|
---|
| 27 | const checkUserType = async (token) => {
|
---|
| 28 | try {
|
---|
| 29 | const response = await fetch("http://localhost:8080/api/usertype", {
|
---|
| 30 | method: 'GET',
|
---|
| 31 | headers: {
|
---|
| 32 | 'Authorization': `Bearer ${token}`,
|
---|
| 33 | 'Content-Type': 'application/json'
|
---|
| 34 | }
|
---|
| 35 | });
|
---|
| 36 |
|
---|
| 37 | if (response.ok) {
|
---|
| 38 | const data = await response.json();
|
---|
| 39 | setRole(data);
|
---|
| 40 | } else {
|
---|
| 41 | console.log("Error getting response.");
|
---|
| 42 | }
|
---|
| 43 | } catch (error) {
|
---|
| 44 | console.error("Error:", error);
|
---|
| 45 | }
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | const handleLogout = () => {
|
---|
| 49 | localStorage.clear();
|
---|
| 50 | setIsLoggedIn(false);
|
---|
| 51 | navigate("/login");
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | const toggleDropdown = () => {
|
---|
| 55 | setShowDropdown(!showDropdown);
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | return (
|
---|
| 59 | <header className={styles.header}>
|
---|
| 60 | <div className={styles.headerContainer}>
|
---|
| 61 | <Link to="/">
|
---|
| 62 | <div className={styles.logo}>
|
---|
| 63 | <img src={Logo} alt="CookCraft Logo" className={styles.logoImage} />
|
---|
| 64 | <span className={styles.logoText}>
|
---|
| 65 | Cook<span className={styles.accentColor}>Craft</span>
|
---|
| 66 | </span>
|
---|
| 67 | </div>
|
---|
| 68 | </Link>
|
---|
| 69 | <nav className={styles.navContainer}>
|
---|
| 70 | <Link to="/" className={styles.navLink}>Home</Link>
|
---|
| 71 | <Link to="/Recipes" className={styles.navLink}>Recipes</Link>
|
---|
| 72 | <a href="/About" className={styles.navLink}>About</a>
|
---|
| 73 | {
|
---|
| 74 | role === "User" ?
|
---|
| 75 | <Link to="/apply" className={styles.navLink}>Join our Team!</Link>
|
---|
| 76 | : role === "DeliveryPerson" ?
|
---|
| 77 | <Link to="/deliver" className={styles.navLink}>Start Delivering</Link>
|
---|
| 78 | : role === "Admin" ?
|
---|
| 79 | <Link to="/admin" className={styles.navLink}>Administrator</Link>
|
---|
| 80 | : null
|
---|
| 81 | }
|
---|
| 82 | {isLoggedIn && (
|
---|
| 83 | <div className={styles.profileMenu}>
|
---|
| 84 | <button onClick={toggleDropdown} className={styles.profileButton}>
|
---|
| 85 | <FontAwesomeIcon icon={faCog} className={styles.settingsIcon} />
|
---|
| 86 | </button>
|
---|
| 87 | {showDropdown && (
|
---|
| 88 | <div className={styles.dropdownMenu}>
|
---|
| 89 | <div className={styles.greeting}>
|
---|
| 90 | Hello, <br/>{user.username} {user.usersurname}
|
---|
| 91 | </div>
|
---|
| 92 | <Link to="/profile" state={{selected: 0}} className={styles.dropdownLink}>Profile</Link>
|
---|
| 93 | <Link to="/profile" state={{selected: 1}} className={styles.dropdownLink}>Favorite Recipes</Link>
|
---|
| 94 | <Link to="/profile" state={{selected: 2}} className={styles.dropdownLink}>My Reviews</Link>
|
---|
| 95 | <Link to="/profile" state={{selected: 3}} className={styles.dropdownLink}>Order History</Link>
|
---|
| 96 | <Link onClick={handleLogout} className={styles.dropdownLink}>Logout</Link>
|
---|
| 97 | </div>
|
---|
| 98 | )}
|
---|
| 99 | </div>
|
---|
| 100 | )}
|
---|
| 101 | {!isLoggedIn && (
|
---|
| 102 | <>
|
---|
| 103 | <Link to="/login" className={styles.authButton}>Log In</Link>
|
---|
| 104 | <Link to="/register" className={styles.authButton}>Register</Link>
|
---|
| 105 | </>
|
---|
| 106 | )}
|
---|
| 107 | <ShoppingCart ingredients={ingredients} hideIngredients={true} />
|
---|
| 108 | </nav>
|
---|
| 109 | </div>
|
---|
| 110 | </header>
|
---|
| 111 | );
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | export default Header;
|
---|