-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d9d479
commit 8438db3
Showing
9 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react'; | ||
import { useSearch } from '../../context/Search'; | ||
import axios from 'axios'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
const SearchInput = () => { | ||
const [values, setValues] = useSearch(); | ||
const navigate = useNavigate(); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
try { | ||
const { data } = await axios.get( | ||
`${process.env.REACT_APP_API}/api/v1/product/search/${values.keyword}` | ||
); | ||
setValues({ ...values, result: data }); | ||
navigate('/search'); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<form className="d-flex me-3" role="search" onSubmit={handleSubmit}> | ||
<input | ||
className="form-control me-2" | ||
type="search" | ||
placeholder="Search" | ||
aria-label="Search" | ||
value={values.keyword} | ||
onChange={(e) => setValues({ ...values, keyword: e.target.value })} | ||
/> | ||
<button className="btn btn-outline-success" type="submit"> | ||
Search | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useState, useContext, createContext } from 'react'; | ||
const SearchContext = createContext(); | ||
|
||
const SearchProvider = ({ children }) => { | ||
const [search, setSearch] = useState({ | ||
keyword: '', | ||
results: [], | ||
}); | ||
|
||
return ( | ||
<SearchContext.Provider value={[search, setSearch]}> | ||
{children} | ||
</SearchContext.Provider> | ||
); | ||
}; | ||
|
||
//custom hook | ||
const useSearch = () => useContext(SearchContext); | ||
|
||
export { useSearch, SearchProvider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import { useSearch } from '../context/Search'; | ||
import Layout from '../components/Layout/Layout'; | ||
|
||
const SearchPage = () => { | ||
const [values, setValues] = useSearch(); | ||
return ( | ||
<Layout title={'Search Results'}> | ||
<div className="container"> | ||
<div className="text-center"> | ||
<h1>Search Results</h1> | ||
<h6> | ||
{values?.result.length < 1 | ||
? 'No Products Found' | ||
: `Found ${values?.result.length}`} | ||
</h6> | ||
<div className="d-flex flex-wrap mt-4"> | ||
{values?.result.map((p) => ( | ||
<div key={p._id} className="col-lg-3 col-md-4 col-sm-6 mb-4"> | ||
{' '} | ||
<div className="card m-2"> | ||
<img | ||
src={`${process.env.REACT_APP_API}/api/v1/product/product-photo/${p._id}`} | ||
className="card-img-top" | ||
alt={p.name} | ||
/> | ||
<div className="card-body"> | ||
<div className="d-flex justify-content-between"> | ||
<h5 className="card-title">{p.name}</h5> | ||
<h5>{p.price}$</h5> | ||
</div> | ||
<p className="card-text"> | ||
{p.description.substring(0, 25)}... | ||
</p> | ||
<button class="btn btn-info ms-1">More Details</button> | ||
<button class="btn btn-secondary ms-1">Add to Cart</button> | ||
</div> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default SearchPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters