//------------------------------ MODULE -------------------------------------
import { useState } from "react";
import styled from "styled-components";
import { FaSearch } from "react-icons/fa";
import { Link, useNavigate } from "react-router-dom";
//------------------------------ CSS ----------------------------------------
const StyledSearchBar = styled.div`
border: solid 1px lightgrey;
border-radius : 100px;
padding:8px;
height:17px;
&>input{
border:none;
outline:none;
width:97%;
margin:none;
vertical-align:text-top;
}
`;
const StyledIcon = styled.span`
cursor:pointer;
`;
//------------------------------ COMPONENT ----------------------------------
const SearchBar = () => {
const [keyword, setKeyword] = useState(null);
const navigate = useNavigate();
const search_exec = () => {
navigate(`/Search?keyword=${keyword}`);
}
const enterkey = () => {
if(window.event.keyCode == 13){
search_exec();
}
}
const onChangeInput = (e) => {
setKeyword(e.target.value);
};
return(
<>
<StyledSearchBar>
<input onChange={onChangeInput} onKeyUp={enterkey}/>
<StyledIcon onClick={search_exec} ><FaSearch /></StyledIcon>
</StyledSearchBar>
</>
)
}
export default SearchBar;
//------------------------------ MODULE -------------------------------------
import styled from "styled-components";
import { Link, useLocation } from "react-router-dom";
import QueryString from 'qs';
//------------------------------ CSS ----------------------------------------
const StyledResult = styled.div`
grid-column: 1 / span 4;
height:50px;
font-size : 20px;
`;
const StyledDiv = styled.div`
width:320px;
height:320px;
padding:15px;
text-align:center;
`;
const StyledImg = styled.img`
width:220px;
height:220px;
padding:15px;
`;
const StyledText = styled.div`
font-size:8px;
font-style:italic;
font-weight:bold;
`;
//------------------------------ COMPONENT ----------------------------------
const Search = ({data}) => {
const sdata = data.reduce((acc, cur) => acc.concat(cur));
const location = useLocation();
const params = QueryString.parse(location.search, { ignoreQueryPrefix: true });
const keyword = params.keyword;
const real_data = keyword ? sdata.filter((unit) => unit.name.includes(keyword)) : [];
return (
<>
<StyledResult>{`Total: ${real_data.length}`}</StyledResult>
{real_data.map((item, index) => {
return (
<StyledDiv key={index}>
<Link to={`/Detail?id=${item.id}`}>
<StyledImg src = {item.thumbnail}></StyledImg>
<StyledText>Name : {item.name}</StyledText>
<StyledText>Price : {item.price}</StyledText>
</Link>
</StyledDiv>
)
})}
</>
)
}
export default Search;