You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import MenuIcon from '@mui/icons-material/Menu';
  2. import AppBar from '@mui/material/AppBar';
  3. import IconButton from '@mui/material/IconButton';
  4. import Toolbar from '@mui/material/Toolbar';
  5. import useMediaQuery from '@mui/material/useMediaQuery';
  6. import React, { useState } from 'react';
  7. //drawer elements used
  8. import { signOut, useSession } from 'next-auth/react';
  9. import { useRouter } from 'next/router';
  10. import { useEffect } from 'react';
  11. import { useStore } from '../../../store/cart-context';
  12. import { useUserUpdate } from '../../../store/user-context';
  13. import DesktopNav from './DesktopNav';
  14. import MobileNav from './MobileNav';
  15. export default function MainNav() {
  16. //react useState hook to save the current open/close state of the drawer, normally variables dissapear afte the function was executed
  17. const [open, setState] = useState(false);
  18. const [cartQuantity, setCartQuantity] = useState(0);
  19. const matches = useMediaQuery('(min-width: 900px)');
  20. const router = useRouter();
  21. const { data: session } = useSession();
  22. const { totalQuantity } = useStore();
  23. const { clearUser } = useUserUpdate();
  24. const signOutHandler = async () => {
  25. const data = await signOut({ redirect: false, callbackUrl: '/' });
  26. clearUser();
  27. router.push(data.url);
  28. };
  29. //function that is being called every time the drawer should open or close, the keys tab and shift are excluded so the user can focus between the elements with the keys
  30. const toggleDrawer = (open) => (event) => {
  31. if (
  32. event.type === 'keydown' &&
  33. (event.key === 'Tab' || event.key === 'Shift')
  34. ) {
  35. return;
  36. }
  37. //changes the function state according to the value of open
  38. setState(open);
  39. };
  40. useEffect(() => {
  41. if (matches) {
  42. setState(false);
  43. }
  44. }, [matches]);
  45. useEffect(() => {
  46. setCartQuantity(totalQuantity);
  47. }, [totalQuantity]);
  48. return (
  49. <AppBar
  50. position="absolute"
  51. sx={{
  52. zIndex: 100,
  53. top: 20,
  54. width: '100%',
  55. backgroundColor: 'transparent',
  56. boxShadow: 'none',
  57. height: 40,
  58. }}
  59. >
  60. <Toolbar sx={{ width: '100%' }}>
  61. <DesktopNav
  62. router={router}
  63. totalQuantity={cartQuantity}
  64. session={session}
  65. signOutHandler={signOutHandler}
  66. />
  67. <IconButton
  68. edge="start"
  69. color={router.pathname === '/' ? 'inherit' : 'primary'}
  70. aria-label="open drawer"
  71. onClick={toggleDrawer(true)}
  72. sx={{
  73. mr: 2,
  74. display: {
  75. xs: 'block',
  76. md: 'none',
  77. },
  78. }}
  79. >
  80. <MenuIcon />
  81. </IconButton>
  82. {/* The outside of the drawer */}
  83. <MobileNav
  84. totalQuantity={totalQuantity}
  85. session={session}
  86. signOutHandler={signOutHandler}
  87. toggleDrawer={toggleDrawer}
  88. open={open}
  89. />
  90. </Toolbar>
  91. </AppBar>
  92. );
  93. }