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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import Image from "next/image";
import Link from "./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 { useCallback, useEffect, useRef, useState } from "react";
import Head from "next/head";
import Logo from "../images/logo.png";
import { useRouter } from "next/router";
import { getAuth } from "../helpers/auth";
import axios from "axios";
export default function Header({ title }) {
const router = useRouter();
const { q = '' } = router.query;
const [searchQuery, setSearchQuery] = useState(q);
const [suggestions, setSuggestions] = useState([]);
const searchQueryRef = useRef();
const [isMenuActive, setIsMenuActive] = useState(false);
const [auth, setAuth] = useState();
useEffect(() => {
if (!auth) setAuth(getAuth())
}, [auth]);
useEffect(() => {
if (q) {
searchQueryRef.current.blur();
setSuggestions([]);
};
}, [q])
const clickSuggestion = (value) => {
console.log(value);
router.push(`/shop/search?q=${value}`, undefined, { scroll: false });
}
const getSuggestion = useCallback(async () => {
if (searchQuery.trim().length > 0) {
let result = await axios(`${process.env.SELF_HOST}/api/shop/suggest?q=${searchQuery.trim()}`);
setSuggestions(result.data.suggest.mySuggester[searchQuery.trim()].suggestions);
} else {
setSuggestions([]);
}
}, [searchQuery]);
useEffect(() => {
if (document.activeElement == searchQueryRef.current) getSuggestion();
}, [getSuggestion]);
const openMenu = () => setIsMenuActive(true);
const closeMenu = () => setIsMenuActive(false);
const searchSubmit = (e) => {
e.preventDefault();
if (searchQuery.length > 0) {
router.push(`/shop/search?q=${searchQuery}`, undefined, { scroll: false });
} else {
searchQueryRef.current.focus();
}
}
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={'menu-wrapper' + (isMenuActive ? ' active ' : '')}>
<div className="flex gap-x-2 items-center border-b border-gray_r-6 p-4">
{auth ? (
<h1>Hi, {auth.name}</h1>
) : (
<>
<Link href="/login" onClick={closeMenu} className="w-full py-2 btn-light text-gray_r-12">Masuk</Link>
<Link href="/register" onClick={closeMenu} className="w-full py-2 btn-yellow text-gray_r-12">Daftar</Link>
</>
)}
</div>
<div className="flex flex-col">
{auth ? (
<Link className="flex w-full font-normal text-gray-800 border-b border-gray_r-6 p-4 py-3" href="/shop/brands" onClick={closeMenu}>
<span>Profil Saya</span>
<div className="ml-auto">
<ChevronRightIcon className="stroke-gray-700 w-5 h-5" />
</div>
</Link>
) : ''}
<Link className="flex w-full font-normal text-gray-800 border-b border-gray_r-6 p-4 py-3" href="/shop/brands" onClick={closeMenu}>
<span>Semua Brand</span>
<div className="ml-auto">
<ChevronRightIcon className="stroke-gray-700 w-5 h-5" />
</div>
</Link>
<Link className="flex w-full font-normal text-gray-800 border-b border-gray_r-6 p-4 py-3" href="/blog" onClick={closeMenu}>
<span>Blog Indoteknik</span>
<div className="ml-auto">
<ChevronRightIcon className="stroke-gray-700 w-5 h-5" />
</div>
</Link>
<button className="flex w-full font-normal text-gray-800 border-b border-gray_r-6 p-4 py-3" onClick={closeMenu}>
<span>Kategori</span>
<div className="ml-auto">
<ChevronRightIcon className="stroke-gray-700 w-5 h-5" />
</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="/" scroll={false}>
<Image src={Logo} alt="Logo Indoteknik" width={120} height={40} />
</Link>
<div className="flex gap-4">
<Link href="/shop/cart">
<ShoppingCartIcon className="w-6 stroke-gray-900" />
</Link>
<button onClick={openMenu}>
<MenuIcon className="w-6 stroke-gray-900" />
</button>
</div>
</div>
<form onSubmit={searchSubmit} className="relative flex mt-2">
<input
ref={searchQueryRef}
type="text"
name="q"
onChange={(e) => setSearchQuery(e.target.value)}
onFocus={getSuggestion}
value={searchQuery}
className="form-input rounded-r-none border-r-0 focus:ring-0"
placeholder="Ketikan nama, merek, part number"
autoComplete="off"
/>
<button
type="submit"
aria-label="search"
className="btn-light bg-transparent px-2 py-1 rounded-l-none border-l-0"
>
<SearchIcon />
</button>
{suggestions.length > 1 ? (
<div className="absolute w-full top-[50px] rounded-b bg-gray_r-2 border border-gray_r-6">
{suggestions.map((suggestion, index) => (
<p onClick={() => clickSuggestion(suggestion.term)} className="w-full p-2" key={index}>{suggestion.term}</p>
))}
</div>
) : ''}
</form>
</div>
{suggestions.length > 1 ? (
<div className="menu-overlay !z-40" onClick={() => setSuggestions([])}></div>
) : ''}
</>
)
}
|