Skip to content

Commit 04f4972

Browse files
committed
Update navigation bar feature to go to home page
1 parent 846a8f2 commit 04f4972

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Frontend/src/components/NavigationBar.jsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import Container from 'react-bootstrap/Container';
22
import Navbar from 'react-bootstrap/Navbar';
3+
import { useLocation, useNavigate } from 'react-router-dom'; // Import hooks
34
import UserAvatarBox from "./user/userAvatarBox";
45

56
function NavigationBar() {
7+
const location = useLocation(); // Get the current location
8+
const navigate = useNavigate(); // Function to navigate programmatically
9+
10+
const handleBrandClick = () => {
11+
if (location.pathname === '/home') {
12+
// Scroll to top if on the home page
13+
window.scrollTo({ top: 0, behavior: 'smooth' });
14+
} else if (location.pathname !== '/home') {
15+
// Navigate to /home if not on the home page
16+
navigate('/home');
17+
}
18+
// Do nothing if already on /home
19+
};
620

721
return (
822
<Navbar className='bg-light' sticky="top">
923
<Container fluid className="d-flex justify-content-between" style={{ paddingLeft: "32px", paddingRight: "32px" }}>
1024
<Navbar.Brand
11-
onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })} // Scroll to top of home page on click
12-
style={{ cursor: 'pointer' }}
25+
onClick={handleBrandClick} // Handle the click event
26+
style={{ cursor: 'pointer' }}
1327
>
1428
PeerPrep
1529
</Navbar.Brand>
16-
<UserAvatarBox/>
30+
<UserAvatarBox />
1731
</Container>
1832
</Navbar>
1933
);

0 commit comments

Comments
 (0)