/
Search.js
71 lines (58 loc) · 2.18 KB
/
Search.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
import { useRef, useContext } from 'react';
import { useHistory } from 'react-router';
import { Context } from '../../context/AppContext';
import * as routeService from '../../services/route';
import * as storageService from '../../services/storage';
import * as ROUTE from '../../constants/routes';
import * as STORAGE_KEYS from '../../constants/storage-keys';
import arrow from '../../images/arrow.svg';
import search from '../../images/search.png';
const Search = () => {
const searchRef = useRef(null);
const { user } = useContext(Context);
const history = useHistory();
const home = () => {
routeService.navigate({ route: ROUTE.HOME, push: history.push });
};
const searchProducts = () => {
const keywords = searchRef.current.value;
if (keywords) {
storageService.save({ key: STORAGE_KEYS.KEYWORD, payload: keywords });
searchOrReload();
searchRef.current.value = '';
}
};
const searchOrReload = () => {
const url = window.location.href;
if (url.includes(ROUTE.SEARCH)) {
window.location.reload();
} else {
routeService.navigate({ route: ROUTE.SEARCH, push: history.push });
}
};
if (!user) return <></>;
return (
<div className="search">
<div className="search__container">
<img onClick={home} className="search__logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/EBay_logo.svg/2560px-EBay_logo.svg.png" alt="logo" />
<div className="search__category">
<span>Shop By Category</span>
<img className="search__arrow" src={arrow} alt="arrow" />
</div>
<div className="search__form">
<img className="search__formic" src={search} alt="search" />
<input className="search__formi" type="text" placeholder="Search for anything" ref={searchRef} />
<div className="search__formca">
<span>All Categories</span>
<img className="search__formcaarrow" src={arrow} alt="arrow" />
</div>
</div>
<div className="search__forma">
<button onClick={searchProducts}>Search</button>
<span>Advanced</span>
</div>
</div>
</div>
);
};
export default Search;