Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Navbar.jsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import AppBar from '@mui/material/AppBar';
  2. import Box from '@mui/material/Box';
  3. import Typography from '@mui/material/Typography';
  4. import { signOut, useSession } from 'next-auth/react';
  5. import Image from 'next/image';
  6. import Link from 'next/link';
  7. import { useRouter } from 'next/router';
  8. import {
  9. BASE_PAGE,
  10. CART_PAGE,
  11. PRODUCTS_PAGE,
  12. PROFILE_PAGE,
  13. } from '../../../constants/pages';
  14. import { useStore } from '../../../store/cart-context';
  15. import { useUserUpdate } from '../../../store/user-context';
  16. const Navbar = () => {
  17. const router = useRouter();
  18. const { totalQuantity } = useStore();
  19. const { data: session } = useSession();
  20. const { clearUser } = useUserUpdate();
  21. const signOutHandler = async () => {
  22. const data = await signOut({ redirect: false, callbackUrl: '/' });
  23. clearUser();
  24. router.push(data.url);
  25. };
  26. return (
  27. <AppBar
  28. position="absolute"
  29. sx={{
  30. zIndex: 100,
  31. top: 20,
  32. width: '100%',
  33. backgroundColor: 'transparent',
  34. boxShadow: 'none',
  35. height: 40,
  36. }}
  37. >
  38. <Box sx={{ display: 'flex', width: '100%' }}>
  39. <Box
  40. sx={{
  41. flexGrow: 1,
  42. maxWidth: '50%',
  43. height: 30,
  44. display: 'flex',
  45. px: 10,
  46. }}
  47. >
  48. <Link key="home" href={BASE_PAGE}>
  49. <Typography
  50. textAlign="center"
  51. sx={{
  52. mx: 'auto',
  53. fontSize: { md: 20, xs: 17 },
  54. fontWeight: 500,
  55. mr: { lg: 0, xs: 2 },
  56. color: router.pathname === '/' ? 'white' : 'black',
  57. textDecoration: 'none',
  58. cursor: 'pointer',
  59. }}
  60. >
  61. Home
  62. </Typography>
  63. </Link>
  64. <Link key="menu" href={BASE_PAGE}>
  65. <Typography
  66. textAlign="center"
  67. sx={{
  68. mx: 'auto',
  69. fontSize: { md: 20, xs: 17 },
  70. fontWeight: 500,
  71. mr: { lg: 0, xs: 2 },
  72. color: router.pathname === '/' ? 'white' : 'black',
  73. textDecoration: 'none',
  74. cursor: 'pointer',
  75. }}
  76. >
  77. Menu
  78. </Typography>
  79. </Link>
  80. <Link key="about" href={BASE_PAGE}>
  81. <Typography
  82. textAlign="center"
  83. sx={{
  84. mx: 'auto',
  85. fontSize: { md: 20, xs: 17 },
  86. fontWeight: 500,
  87. mr: { lg: 0, xs: 2 },
  88. color: router.pathname === '/' ? 'white' : 'black',
  89. textDecoration: 'none',
  90. cursor: 'pointer',
  91. }}
  92. >
  93. About
  94. </Typography>
  95. </Link>
  96. <Link key="store" href={PRODUCTS_PAGE}>
  97. <Typography
  98. textAlign="center"
  99. sx={{
  100. mx: 'auto',
  101. fontSize: { md: 20, xs: 17 },
  102. fontWeight: 500,
  103. mr: { lg: 0, xs: 2 },
  104. color: router.pathname === '/' ? 'white' : 'black',
  105. textDecoration: 'none',
  106. cursor: 'pointer',
  107. }}
  108. >
  109. Store
  110. </Typography>
  111. </Link>
  112. <Link key="contact" href={BASE_PAGE}>
  113. <Typography
  114. textAlign="center"
  115. sx={{
  116. mx: 'auto',
  117. fontSize: { md: 20, xs: 17 },
  118. fontWeight: 500,
  119. color: router.pathname === '/' ? 'white' : 'black',
  120. textDecoration: 'none',
  121. cursor: 'pointer',
  122. }}
  123. >
  124. Contact
  125. </Typography>
  126. </Link>
  127. </Box>
  128. <Box
  129. sx={{
  130. flexGrow: 1,
  131. maxWidth: '50%',
  132. height: 30,
  133. display: 'flex',
  134. justifyContent: 'right',
  135. pt: 0.5,
  136. mr: 4,
  137. }}
  138. >
  139. {session?.user?._id && (
  140. <Box
  141. sx={{
  142. mx: 2,
  143. mt: 0.1,
  144. cursor: 'pointer',
  145. }}
  146. onClick={signOutHandler}
  147. >
  148. <Image
  149. src="/images/logout.svg"
  150. alt="profile"
  151. width={18}
  152. height={20}
  153. />
  154. </Box>
  155. )}
  156. <Box
  157. sx={{
  158. mx: 2,
  159. cursor: 'pointer',
  160. }}
  161. >
  162. <Link key="home" href={PROFILE_PAGE}>
  163. <Image
  164. src="/images/profile.svg"
  165. alt="profile"
  166. width={24}
  167. height={24}
  168. />
  169. </Link>
  170. </Box>
  171. <Box
  172. sx={{
  173. mr: 6,
  174. ml: 2,
  175. cursor: 'pointer',
  176. }}
  177. >
  178. <Link key="home" href={CART_PAGE}>
  179. <Box>
  180. <Box
  181. sx={{
  182. color: 'white',
  183. zIndex: 3,
  184. width: 20,
  185. height: 20,
  186. borderRadius: 20,
  187. textAlign: 'center',
  188. px: 0.5,
  189. ml: 2.2,
  190. mt: -1,
  191. fontSize: 16,
  192. position: 'absolute',
  193. backgroundColor: 'primary.main',
  194. }}
  195. >
  196. {totalQuantity}
  197. </Box>
  198. <Image
  199. src="/images/cart.svg"
  200. alt="cart"
  201. width={24}
  202. height={24}
  203. />
  204. </Box>
  205. </Link>
  206. </Box>
  207. </Box>
  208. </Box>
  209. </AppBar>
  210. );
  211. };
  212. export default Navbar;