summaryrefslogtreecommitdiff
path: root/src/components/Header.js
blob: f7607a18622a4478878545cd4a5f1412ab461499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
84
85
86
87
88
89
90
91
92
93
94
95
import Image from "next/image";
import Link from "next/link";
import ShoppingCartIcon from "../icons/shopping-cart.svg";
import SearchIcon from "../icons/search.svg";
import MenuIcon from "../icons/menu.svg";
import ChevronRightIcon from "../icons/chevron-right.svg";
import { useState } from "react";
import Head from "next/head";
import Logo from "../images/logo.png";
import { useRouter } from "next/router";


export default function Header({ title }) {
  const router = useRouter();
  const { q = '' } = router.query;
  const [searchQuery, setSearchQuery] = useState(q);
  const [isMenuActive, setIsMenuActive] = useState(false);

  const openMenu = () => setIsMenuActive(true);
  const closeMenu = () => setIsMenuActive(false);

  const searchSubmit = (e) => {
    e.preventDefault();
    router.push(`/shop/search?q=${searchQuery}`);
  }

  return (
    <>
      <Head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <title>{title}</title>
      </Head>
      <div className={isMenuActive ? 'menu-wrapper active' : 'menu-wrapper'}>
        <div className="flex gap-x-2 items-center">
          <Link href="/login" className="w-full py-2 btn-light">Masuk</Link>
          <Link href="/register" className="w-full py-2 btn-primary">Daftar</Link>
        </div>
        <div className="flex flex-col gap-y-4 mt-5">
          <Link className="flex w-full font-normal text-gray-800" href="/shop/brands">
              <span>Brand</span>
              <div className="ml-auto">
                <ChevronRightIcon className="stroke-gray-700" />
              </div>
          </Link>
          <Link className="flex w-full font-normal text-gray-800" href="/blog">
              <span>Blog</span>
              <div className="ml-auto">
                <ChevronRightIcon className="stroke-gray-700" />
              </div>
          </Link>
          <button className="flex w-full font-normal text-gray-800" id="open_category_parent_menu">
              <span>Kategori</span>
              <div className="ml-auto">
                <ChevronRightIcon className="stroke-gray-700" />
              </div>
          </button>
        </div>
      </div>
      <div className={isMenuActive ? 'menu-overlay block opacity-100' : 'menu-overlay hidden opacity-0'} onClick={closeMenu}></div>

      <div className="sticky-header">
        <div className="flex justify-between items-center">
          <Link href="/">
            <Image src={Logo} alt="Logo Indoteknik" width={120} height={40} className="w-auto" />
          </Link>
          <div className="flex gap-4">
            <Link href="/shop/cart">
                <ShoppingCartIcon className="w-6" />
            </Link>
            <button onClick={openMenu}>
              <MenuIcon className="w-6" />
            </button>
          </div>
        </div>
        <form onSubmit={searchSubmit} className="flex mt-2">
            <input 
              type="text" 
              name="q" 
              onChange={(e) => setSearchQuery(e.target.value)} 
              value={searchQuery} 
              className="form-input rounded-r-none border-r-0 focus:outline-none" 
              placeholder="Ketikan nama, merek, part number"
            />
            <button 
              type="submit" 
              aria-label="search" 
              className="btn-light bg-transparent px-2 py-1 rounded-l-none border-l-0"
            >
              <SearchIcon />
            </button>
        </form>
      </div>
    </>
  )
}