index.tsx 3.23 KB
'use client'; // This is a client component ๐Ÿ‘ˆ๐Ÿฝ

import React, { useState, useEffect } from 'react';
import { Button, TextField, Tabs, Tab, Container, Grid } from '@mui/material';
import SearchIcon from '@mui/icons-material/Search';

import { useRouter } from 'next/navigation';

import Link from 'next/link';
import Image from 'next/image';

// import { useStore } from '../store'; // Adjust the path according to your file structure
// import ContactDialog from './ContactDialog'; // Adjust the path according to your file structure

function Header() {
  const [input, setInput] = useState('');
  const [tab, setTab] = useState(0);
  const {
    keyword,
    updateKeyword,
    updateDialogOpen,
    updateDisplay,
    updatePageNo,
  } = {};

  const router = useRouter();

  useEffect(() => {
    setInput(keyword);
  }, [keyword]);

  const handleKeyDown = (e) => {
    if (e.keyCode === 13) {
      handleClick();
    }
  };

  const handleClick = () => {
    updateDisplay(!input);
    updateKeyword(input);
    updatePageNo(1);
    router.push('/products');
  };

  const handleTabClick = () => {
    updateDisplay(true);
    updateKeyword('');
  };

  useEffect(() => {
    const currentPath = router.pathname;
    const tabValue =
      currentPath === '/'
        ? 0
        : currentPath === '/products'
        ? 1
        : currentPath === '/about'
        ? 2
        : 0;
    setTab(tabValue);
  }, [router.pathname]);

  return (
    <>
      <Container className="pt-2">
        <Grid container alignItems="center">
          <Grid item xs={1} sx={{ p: 0, height: '64px' }}>
            <Link href="/">
              <Image src="icon/logo.jpg" alt="canrud" width={64} height={64} />
            </Link>
          </Grid>
          <Grid item xs={6} md={8} sx={{ px: 0 }}>
            <TextField
              label="Search keyword"
              variant="outlined"
              onKeyDown={handleKeyDown}
              value={input}
              onChange={(e) => setInput(e.target.value)}
              append-inner-icon="mdi-magnify"
              InputProps={{
                endAdornment: <SearchIcon onClick={handleClick} />,
              }}
              fullWidth
            />
          </Grid>
          <Grid item xs={4} md={2} className="ml-3">
            <Button color="primary" onClick={() => updateDialogOpen(true)}>
              Contact Us
            </Button>
          </Grid>
        </Grid>
      </Container>
      <Tabs
        value={tab}
        onChange={(e, newVal) => setTab(newVal)}
        textColor="inherit"
        variant="fullWidth"
        className="bg-primary-blue text-white"
      >
        <Tab
          label="Home"
          className="text-grey-lighten-3 tw-font-bold"
          component={Link}
          href="/"
          onClick={handleTabClick}
        />
        <Tab
          label="Products"
          component={Link}
          href="/products"
          className="text-grey-lighten-3 tw-font-bold"
          onClick={handleTabClick}
        />
        <Tab
          label="About"
          component={Link}
          href="/about"
          className="text-grey-lighten-3 tw-font-bold"
          onClick={handleTabClick}
        />
      </Tabs>
      {/* <ContactDialog /> */}
    </>
  );
}

export default Header;