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.

MainNav.jsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 matches = useMediaQuery('(min-width: 900px)');
  19. const router = useRouter();
  20. const { data: session } = useSession();
  21. const { totalQuantity } = useStore();
  22. const { clearUser } = useUserUpdate();
  23. const signOutHandler = async () => {
  24. const data = await signOut({ redirect: false, callbackUrl: '/' });
  25. clearUser();
  26. router.push(data.url);
  27. };
  28. //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
  29. const toggleDrawer = (open) => (event) => {
  30. if (
  31. event.type === 'keydown' &&
  32. (event.key === 'Tab' || event.key === 'Shift')
  33. ) {
  34. return;
  35. }
  36. //changes the function state according to the value of open
  37. setState(open);
  38. };
  39. useEffect(() => {
  40. if (matches) {
  41. setState(false);
  42. }
  43. }, [matches]);
  44. return (
  45. <AppBar
  46. position="absolute"
  47. sx={{
  48. zIndex: 100,
  49. top: 20,
  50. width: '100%',
  51. backgroundColor: 'transparent',
  52. boxShadow: 'none',
  53. height: 40,
  54. }}
  55. >
  56. <Toolbar sx={{ width: '100%' }}>
  57. <DesktopNav
  58. router={router}
  59. totalQuantity={totalQuantity}
  60. session={session}
  61. signOutHandler={signOutHandler}
  62. />
  63. <IconButton
  64. edge="start"
  65. color={router.pathname === '/' ? 'inherit' : 'primary'}
  66. aria-label="open drawer"
  67. onClick={toggleDrawer(true)}
  68. sx={{
  69. mr: 2,
  70. display: {
  71. xs: 'block',
  72. md: 'none',
  73. },
  74. }}
  75. >
  76. <MenuIcon />
  77. </IconButton>
  78. {/* The outside of the drawer */}
  79. <MobileNav
  80. session={session}
  81. signOutHandler={signOutHandler}
  82. toggleDrawer={toggleDrawer}
  83. open={open}
  84. />
  85. </Toolbar>
  86. </AppBar>
  87. );
  88. }