File size: 2,828 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import React, { useState, useEffect } from "react";
import { NavLink, Link } from "react-router-dom";
import activeUser from "./custom_hooks/activeUser";
import token from "./custom_hooks/GetJwtToken";
import axios from "axios";
import ShoppingCartIcon from "@material-ui/icons/ShoppingCart";
import { cartCountUrl } from "./../urls";
import { connect } from "react-redux";
import "bootstrap/dist/css/bootstrap.css";
import "../Navbar.css";
const Navbar = (props) => {
const { display, count } = props;
const [currentUser, setCurrentUser] = useState({ name: "" });
const [cartNumber, setCartNumber] = useState(0);
const searchIconStyle = { fontSize: "1.5rem" };
useEffect(() => {
const cartCount = async () => {
try {
const { data } = await axios({
method: "get",
url: cartCountUrl(),
headers: {
token: token(),
},
});
setCartNumber(data.count);
} catch (e) {
// alert(e.message);
}
};
cartCount();
}, [count]);
useEffect(() => {
const showCurrentUserAndGettingCartCount = async () => {
const user = await activeUser();
setCurrentUser({ ...user });
try {
const { data } = await axios({
method: "get",
url: cartCountUrl(),
headers: {
token: token(),
},
});
setCartNumber(data.count);
} catch (e) {
// alert(e.message);
// console.log("user is not login");
}
};
showCurrentUserAndGettingCartCount();
}, []);
return (
<nav className="container-navbar" style={{ display: display }}>
<NavLink to="/">
<img
src="https://www.freepnglogos.com/uploads/amazon-png-logo-vector/large-images-amazon-png-logo-vector-7.png3ft3d1416935166817"
alt="amazon-logo"
className="amazon-logo"
/>
</NavLink>
{/* </div> */}
<div className="search" tabindex="1">
<input type="text" />
<button>
<i className="ion-ios-search-strong" style={searchIconStyle}></i>
</button>
</div>
<div className="options account">
{currentUser.name ? (
<small>
hello, <span>{currentUser.name}</span>
</small>
) : (
<small>
<Link to="/login">Login</Link>
</small>
)}
<b>account</b>
</div>
<div className="options account">
<small>orders</small>
<b>& Return</b>
</div>
<NavLink to="/am/cart" className="options ForCart">
<ShoppingCartIcon></ShoppingCartIcon>
<span>{cartNumber}</span>
</NavLink>
{currentUser.name && (
<div
className="options"
style={{ fontSize: "80%" }}
onClick={() => {
localStorage.removeItem("token");
window.location = "/";
}}
>
log out
</div>
)}
</nav>
);
};
function mapStateToProps(state) {
const { isCount } = state;
return {
count: isCount,
};
}
export default connect(mapStateToProps)(Navbar);
|