Locaft-backend/src/components/NavBar.js

137 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-01-24 09:54:06 -08:00
import React, {useContext,useEffect } from 'react';
2020-12-07 09:49:14 -08:00
import { Link } from "react-router-dom";
2020-11-21 09:27:17 -08:00
import '../navbar.css';
2020-11-30 08:56:17 -08:00
import UserContext from "../context/UserContext";
2021-01-20 09:05:30 -08:00
import styled,{ css } from 'styled-components';
2020-11-20 09:26:49 -08:00
2021-01-20 09:05:30 -08:00
const Header = styled.header`
2021-03-17 10:46:05 -07:00
@import url('https://fonts.googleapis.com/css?family=Montserrat|Ubuntu');
2021-01-20 09:05:30 -08:00
background: #66bfbf;
2021-01-21 09:08:56 -08:00
position: fixed;
2021-01-20 09:05:30 -08:00
top: 0;
left: 0;
width: 100%;
display: flex;
2021-01-21 09:08:56 -08:00
justify-content: space-between;
2021-01-20 09:05:30 -08:00
align-items: center;
transition: 0.6s;
padding: 10px 15px;
z-index: 100000;
font-family: Ubuntu;
2021-01-21 09:08:56 -08:00
${ props => props.sticky ? css`
${Header};
padding: 3px 45px;
min-height: 6vh;
2021-01-20 09:05:30 -08:00
opacity: 0.85;
2021-01-21 09:08:56 -08:00
display: fixed;
`:css``};
2021-01-20 09:05:30 -08:00
& .logo {
font-family: "Ubuntu";
font-size: 2rem;
font-weight: bold;
position: relative;
color: #fff;
text-decoration: none;
text-transform: lowercase;
padding-left: 100px;
transition: 0.6s;
}
2021-03-17 10:46:05 -07:00
`;
const ListElement = styled.li`
`;
const Anchor = styled.a`
2021-01-20 09:05:30 -08:00
`;
const List = styled.ul`
${Header};
2021-03-17 10:46:05 -07:00
position: relative;
display: flex;
justify-content: center;
align-items: center;
& ${ListElement} {
2021-01-20 09:05:30 -08:00
position: relative;
list-style:none;
}
2021-03-17 10:46:05 -07:00
& ${ListElement} ${Anchor} {
position: relative;
margin: 5px 15px;
text-transform: uppercase;
color: #fff;
letter-spacing: 2px;
font-weight: 500px;
padding: 3px 0;
transition: 0.6s;
}
2021-01-20 09:05:30 -08:00
`;
2020-11-29 09:05:56 -08:00
export default function NavBar() {
2020-11-30 08:56:17 -08:00
const { userData, setUserData } = useContext(UserContext);
2021-01-21 09:08:56 -08:00
2020-11-30 08:56:17 -08:00
const logout = () => {
setUserData({
token: undefined,
user: undefined,
});
localStorage.setItem("auth-token", "");
};
2021-01-20 09:05:30 -08:00
2021-01-21 09:08:56 -08:00
const [scrolled,setScrolled]=React.useState(false);
const handleScroll=() => {
const offset=window.scrollY;
if(offset > 200 ){
setScrolled(true);
}
else{
setScrolled(false);
}
}
useEffect(() => {
window.addEventListener('scroll',handleScroll)
})
2021-01-20 09:05:30 -08:00
2020-11-30 08:56:17 -08:00
return (
2020-12-06 08:59:40 -08:00
<div className="navbar">
2021-01-21 09:08:56 -08:00
<Header sticky = {scrolled} >
2021-02-27 09:25:16 -08:00
2020-12-07 09:49:14 -08:00
<a href="/" className="logo">locaft</a>
2020-11-30 08:56:17 -08:00
<ul>
2020-12-07 09:49:14 -08:00
<li><a href="/">Home</a></li>
<li><a href="/#about-us">About</a></li>
<li><a href="/#features">Services</a></li>
<li><a href="/#footer">Contact us</a></li>
2020-11-30 08:56:17 -08:00
{userData.user ? (
2020-12-21 08:27:28 -08:00
<React.Fragment>
2020-12-07 09:49:14 -08:00
<li><Link onClick={logout}>{userData.user.username}</Link></li>
2020-12-21 08:27:28 -08:00
</React.Fragment>
2020-11-30 08:56:17 -08:00
) : (
2020-12-04 08:38:55 -08:00
<React.Fragment>
2020-12-07 09:49:14 -08:00
<li><Link to="/user/register">Register</Link></li>
<li><Link to="/user/login">login</Link></li>
2020-12-04 08:38:55 -08:00
</React.Fragment>
2020-11-30 08:56:17 -08:00
)}
</ul>
2021-01-20 09:05:30 -08:00
</Header>
2020-11-30 08:56:17 -08:00
</div>
)
}
2020-11-29 09:05:56 -08:00