Skip to content

Commit 11ea7df

Browse files
committed
header/routes refacturing
1 parent 1878d79 commit 11ea7df

File tree

6 files changed

+76
-31
lines changed

6 files changed

+76
-31
lines changed

Hobbie_fullstack/react-frontend/src/App.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import AccountBusiness from "./components/root/users/business/AccountBusiness/Ac
1313
import TestForm from "./components/root/users/user/test/TestForm";
1414
import CreateOffer from "./components/root/users/business/Offer/CreateOffer";
1515
import MyHobbies from "./components/root/users/user/MyHobbies";
16-
import ProtectedRoutes from "./components/protectedRoutes/ProtectedRoutes";
16+
import ProtectedRoutesGuest from "./components/protectedRoutes/ProtectedRoutesGuest";
17+
import ProtectedRoutesUser from "./components/protectedRoutes/ProtectedRoutesUser";
1718
import ProtectedRoutesBusiness from "./components/protectedRoutes/ProtectedRoutesBusiness";
1819
import EditUserProfile from "./components/root/users/user/accountUser/EditUserProfile";
1920
import EditBusinessProfile from "./components/root/users/business/AccountBusiness/EditBusinessProfile";
@@ -27,17 +28,18 @@ function App() {
2728
<div className="App">
2829
<Header />
2930
<Routes>
30-
<Route path="/" element={<Home />} />
31-
<Route path="/signup" element={<SignUp />} />
32-
<Route path="/register-business" element={<RegisterBusiness />} />
33-
<Route path="/login" element={<Login />} />
34-
<Route path="/change-password" element={<PasswordChange />} />
35-
<Route
36-
path="/change-password-new/:id"
37-
element={<SetUpNewPassword />}
38-
/>
39-
40-
<Route element={<ProtectedRoutes />}>
31+
<Route element={<ProtectedRoutesGuest />}>
32+
<Route path="/" element={<Home />} />
33+
<Route path="/signup" element={<SignUp />} />
34+
<Route path="/register-business" element={<RegisterBusiness />} />
35+
<Route path="/login" element={<Login />} />
36+
<Route path="/change-password" element={<PasswordChange />} />
37+
<Route
38+
path="/change-password-new/:id"
39+
element={<SetUpNewPassword />}
40+
/>
41+
</Route>
42+
<Route element={<ProtectedRoutesUser />}>
4143
<Route path="/edit-profile" element={<EditUserProfile />} />
4244
<Route path="/user-home" element={<UserHome />} />
4345
<Route path="/account-user" element={<AccountUser />} />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react";
2+
import { Outlet } from "react-router-dom";
3+
import AuthenticationService from "../../api/authentication/AuthenticationService";
4+
import { Navigate } from "react-router-dom";
5+
6+
const isUserLoggedIn = AuthenticationService.isUserLoggedIn();
7+
const isBusinessLoggedIn = AuthenticationService.isBusinessLoggedIn();
8+
9+
const ProtectedRoutesGuest = () => {
10+
if (isUserLoggedIn) {
11+
return <Navigate to="/user-home" />;
12+
} else if (isBusinessLoggedIn) {
13+
return <Navigate to="/business-home" />;
14+
} else {
15+
return <Outlet />;
16+
}
17+
};
18+
19+
export default ProtectedRoutesGuest;

Hobbie_fullstack/react-frontend/src/components/protectedRoutes/ProtectedRoutes.jsx renamed to Hobbie_fullstack/react-frontend/src/components/protectedRoutes/ProtectedRoutesUser.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { Navigate } from "react-router-dom";
66
const useAuth = () => {
77
return AuthenticationService.isUserLoggedIn();
88
};
9-
const ProtectedRoutes = () => {
9+
const ProtectedRoutesUser = () => {
1010
const isAuth = useAuth();
1111
return isAuth ? <Outlet /> : <Navigate to="/login" />;
1212
};
1313

14-
export default ProtectedRoutes;
14+
export default ProtectedRoutesUser;

Hobbie_fullstack/react-frontend/src/components/root/fragments/header/logo/Logo.jsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,64 @@
11
import React from "react";
22
import styles from "../../../../../css/Logo.module.css";
33
import logo from "../../../../../img/logo.svg";
4-
import { Link } from "react-router-dom";
54
import AuthenticationService from "../../../../../api/authentication/AuthenticationService";
5+
import { useNavigate } from "react-router-dom";
6+
import { NavLink } from "react-router-dom";
67

78
const Logo = () => {
89
const isUserLoggedIn = AuthenticationService.isUserLoggedIn();
910
const isBusinessLoggedIn = AuthenticationService.isBusinessLoggedIn();
11+
const navigate = useNavigate();
1012

1113
return (
1214
<section className={styles.logo_container}>
1315
{!isBusinessLoggedIn && !isUserLoggedIn && (
14-
<Link to="/" className="">
16+
<NavLink onClick={() => navigate("/")} to="/" className="">
1517
<img className={styles.imgHeader} src={logo} alt="logo" />
16-
</Link>
18+
</NavLink>
1719
)}
1820
{!isBusinessLoggedIn && !isUserLoggedIn && (
19-
<Link to="/" className="">
21+
<NavLink onClick={() => navigate("/")} to="/" className="">
2022
<h4 className={styles.logo}>obbie</h4>
21-
</Link>
23+
</NavLink>
2224
)}
2325

2426
{isBusinessLoggedIn && (
25-
<Link to="/business-owner" className="">
27+
<NavLink
28+
onClick={() => navigate("/business-home")}
29+
to="/business-home"
30+
className=""
31+
>
2632
<img className={styles.imgHeader} src={logo} alt="logo" />
27-
</Link>
33+
</NavLink>
2834
)}
2935
{isBusinessLoggedIn && (
30-
<Link to="/business-owner" className="">
36+
<NavLink
37+
onClick={() => navigate("/business-home")}
38+
to="/business-home"
39+
className=""
40+
>
3141
<h4 className={styles.logo}>obbie</h4>
32-
</Link>
42+
</NavLink>
3343
)}
3444

3545
{isUserLoggedIn && (
36-
<Link to="/user-home" className="">
46+
<NavLink
47+
onClick={() => navigate("/user-home")}
48+
to="/user-home"
49+
className=""
50+
>
3751
<img className={styles.imgHeader} src={logo} alt="logo" />
38-
</Link>
52+
</NavLink>
3953
)}
4054
{isUserLoggedIn && (
41-
<Link to="/user-home" className="">
55+
<NavLink
56+
onClick={() => navigate("/user-home")}
57+
to="/user-home"
58+
className=""
59+
>
4260
<h4 className={styles.logo}>obbie</h4>
43-
</Link>
61+
</NavLink>
4462
)}
4563
</section>
4664
);

Hobbie_fullstack/react-frontend/src/components/root/fragments/header/navbar/mobileMenu/Menu.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import AuthenticationService from "../../../../../../api/authentication/Authenti
55
import { NavLink } from "react-router-dom";
66
import styles from "../../../../../../css/Navbar.module.css";
77
import { useState } from "react";
8+
import { useEffect } from "react";
89

910
const Menu = () => {
1011
const [clicked, setClicked] = useState(false);
1112
const userLogged = AuthenticationService.isUserLoggedIn();
1213
const businessLogged = AuthenticationService.isBusinessLoggedIn();
1314
const location = useLocation();
15+
const pathname = location.pathname;
16+
17+
useEffect(() => {
18+
setClicked(false);
19+
}, [pathname]);
1420

1521
return (
1622
<section className="menu">
@@ -72,17 +78,17 @@ const Menu = () => {
7278
{clicked && !businessLogged && !userLogged && (
7379
<menu className={styles.popup_menu}>
7480
<ul className={styles.nav_links_popup}>
75-
{location.pathname !== "/signup" && location.pathname !== "/" && (
81+
{pathname !== "/signup" && location.pathname !== "/" && (
7682
<li className={styles.nav_link}>
7783
<NavLink to="/signup">Sign up</NavLink>
7884
</li>
7985
)}
80-
{location.pathname !== "/register-business" && (
86+
{pathname !== "/register-business" && (
8187
<li className={styles.nav_link}>
8288
<NavLink to="register-business">Register Bizz</NavLink>
8389
</li>
8490
)}
85-
{location.pathname !== "/login" && (
91+
{pathname !== "/login" && (
8692
<li className={styles.nav_link}>
8793
<NavLink to="/login">Login</NavLink>
8894
</li>

Hobbie_fullstack/react-frontend/src/css/Navbar.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
padding-right: 20px;
4242
font-size: 12px;
4343
filter: invert(85%) sepia(3%) saturate(5839%) hue-rotate(183deg)
44-
brightness(74%) contrast(95%);
44+
brightness(74%) contrast(95%);
4545
color: rgb(135, 158, 187);
4646
cursor: pointer;
4747
font-weight: 200;

0 commit comments

Comments
 (0)