-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHeader.js
72 lines (63 loc) · 2.89 KB
/
Header.js
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
import { useEffect, useState } from "react"
import { Link } from "react-router-dom"
import { useDispatch, useSelector } from "react-redux"
import { toggleSideBar } from "../utils/appSlice"
import SearchContainer from "./SearchContainer"
import { YOUTUBE_SEARCH_SUGGESTION_API } from "../utils/constant"
import { setSearchCache } from "../utils/searchCacheSlice"
const Header = () => {
const [searchQuery, setSearchQuery] = useState("")
const [suggestion, setSuggestion] = useState([])
const [isSearchFocus, setSearchFocus] = useState("false")
// toggle sidebar
const dispatch = useDispatch()
function handleSideBar() {
dispatch(toggleSideBar())
}
const searchCache = useSelector(store => store.search.searchCache)
useEffect(() => {
// debounding 140ms
const timer = setTimeout(() => {
// first check data in cache
if (searchCache[searchQuery]) {
setSuggestion(searchCache[searchQuery])
} else {
// If data not found in cache, then call API
getSearchSuggestion()
}
}, 150)
return () => {
clearTimeout(timer)
}
}, [searchQuery])
async function getSearchSuggestion() {
const data = await fetch(YOUTUBE_SEARCH_SUGGESTION_API + searchQuery)
const json = await data.json()
setSuggestion(json[1])
// Update Cache
dispatch(setSearchCache({ [searchQuery]: json[1] }))
}
return (
<header className="shadow-md p-2">
<nav className="max-w-7xl mx-auto p-1 flex justify-between items-center ">
<div className="flex gap-2 items-center">
<button onClick={() => handleSideBar()}>
<img className="w-6 " alt="hanburger-menu" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQgz8qMxRmaHif6zYN0OyOJ2zi8gBulBwLPnw&usqp=CAU" />
</button>
<Link to="/">
<img className="w-24 hover:opacity-80" alt="logo" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRMzqLGj4nnVHSqs3ydU8FFlP7JjOq50ccCWg&usqp=CAU" />
</Link>
</div>
<div className="flex">
<input type="text" placeholder="Search" value={searchQuery} onFocus={() => setSearchFocus(true)} onBlur={() => setSearchFocus(false)} onChange={e => setSearchQuery(e.target.value)} className="border-2 py-2 px-6 rounded-l-full w-[500px] focus:outline focus:outline-blue-500 focus:border focus:border-black " />
<button className="bg-slate-200 pl-2 pr-3 rounded-r-full text-gray-900">Search</button>
{isSearchFocus && (suggestion?.length ? <SearchContainer suggestionList={suggestion} /> : null)}
</div>
<div className="">
<img className="w-8 cursor-pointer hover:opacity-80" alt="user" src="https://static.vecteezy.com/system/resources/previews/008/442/086/original/illustration-of-human-icon-user-symbol-icon-modern-design-on-blank-background-free-vector.jpg" />
</div>
</nav>
</header>
)
}
export default Header