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.5KB

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