index.tsx 3.83 KB
Newer Older
Ken's avatar
Ken committed
1
import React, { useEffect, useRef, useState } from "react";
Ken's avatar
Ken committed
2 3 4 5 6 7 8 9 10
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import MenuIcon from "@mui/icons-material/Menu";
import Toolbar from "@mui/material/Toolbar";
import Button from "@mui/material/Button";
import HomeIcon from "@mui/icons-material/Home";
import Sidenav from "../Sidenav";
import { Typography } from "@mui/material";
Ken's avatar
Ken committed
11 12
import { useAppDispatch, useAppSelector } from "app/hooks";
import { getCategories } from "./headerSlice";
Ken's avatar
Ken committed
13
import { handleLoading } from "app/globalSlice";
Ken's avatar
Ken committed
14
import { headerSelector } from "app/selectors";
Ken's avatar
Ken committed
15 16

export default function Header() {
Ken's avatar
Ken committed
17
  const dispatch = useAppDispatch();
Ken's avatar
Ken committed
18 19
  const headerRef = useRef<HTMLElement>(null);
  const [mobileOpen, setMobileOpen] = useState<boolean>(false);
Ken's avatar
Ken committed
20
  const { data: categoryData } = useAppSelector(headerSelector);
Ken's avatar
Ken committed
21 22

  useEffect(() => {
Ken's avatar
Ken committed
23 24 25 26 27 28 29 30 31 32 33 34 35
    dispatch(handleLoading(true));

    try {
      const fetchData = async () => {
        await dispatch(getCategories());
      };

      fetchData();
    } catch (err) {
      console.error("ERROR: ", err);
    } finally {
      dispatch(handleLoading(false));
    }
Ken's avatar
Ken committed
36 37
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
Ken's avatar
Ken committed
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

  const handleDrawerToggle = () => {
    setMobileOpen(!mobileOpen);
  };

  return (
    <Box sx={{ display: "flex" }} className="header">
      <AppBar
        component="nav"
        position="sticky"
        sx={{ background: "transparent", overflow: "hidden" }}
      >
        <Toolbar
          sx={{
            display: "flex",
            alignItems: "center",
            color: "#000",
          }}
          className="header-toolbar"
        >
          <IconButton
            color="inherit"
            aria-label="open drawer"
            edge="start"
            onClick={handleDrawerToggle}
            sx={{ mr: 2 }}
            className="header-toolbar__iconDrawer"
          >
            <MenuIcon />
          </IconButton>
          <Box
            sx={{ gap: 2 }}
            ref={headerRef}
            className="header-toolbar__navbar"
          >
            <Button sx={{ padding: 0, minWidth: "auto", color: "#000" }}>
              <HomeIcon />
            </Button>
            <Box
              className="header-list"
              component="ul"
              sx={{
                gap: 2,
                margin: 0,
                padding: 0,
                listStyleType: "none",
Ken's avatar
Ken committed
84
                alignItems: "center",
Ken's avatar
Ken committed
85 86
              }}
            >
Ken's avatar
Ken committed
87
              {categoryData.map((item) => (
Ken's avatar
Ken committed
88 89
                <Box
                  component="li"
Ken's avatar
Ken committed
90
                  key={item.link}
Ken's avatar
Ken committed
91 92 93 94 95 96 97 98 99
                  sx={{
                    color: "#000",
                    fontWeight: 700,
                    padding: 0,
                    minWidth: "auto",
                    cursor: "pointer",
                  }}
                  className="header-list__item"
                >
Ken's avatar
Ken committed
100
                  {item.label}
Ken's avatar
Ken committed
101 102 103
                </Box>
              ))}
            </Box>
Ken's avatar
Ken committed
104 105 106 107 108 109 110 111 112 113
            <Box
              sx={{
                padding: 0,
                minWidth: "auto",
                color: "#999",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                cursor: "pointer",
              }}
Ken's avatar
Ken committed
114
              onClick={handleDrawerToggle}
Ken's avatar
Ken committed
115
              className="navbar-readAll"
Ken's avatar
Ken committed
116
            >
Ken's avatar
Ken committed
117 118 119
              <Typography
                sx={{ fontSize: 13, marginRight: "5px", fontWeight: 700 }}
              >
Ken's avatar
Ken committed
120 121 122
                Tất cả
              </Typography>
              <MenuIcon sx={{ fontSize: 17 }} />
Ken's avatar
Ken committed
123
            </Box>
Ken's avatar
Ken committed
124 125 126 127 128
          </Box>
        </Toolbar>
      </AppBar>
      <Sidenav
        handleDrawerToggle={handleDrawerToggle}
Ken's avatar
Ken committed
129
        navItems={categoryData}
Ken's avatar
Ken committed
130 131 132 133 134
        mobileOpen={mobileOpen}
      />
    </Box>
  );
}