-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Search Product by categories and name
- Loading branch information
1 parent
cdfc0e5
commit 63b90a0
Showing
6 changed files
with
179 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,124 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { getCategories, list } from './salesApi'; | ||
import CardForProduct from './CardForProduct'; | ||
|
||
/** | ||
* @function Search | ||
* @param | ||
* @argument | ||
* @description | ||
* @usedBy ./Home.js | ||
*/ | ||
const Search = () => { | ||
const [data, setData] = useState({ | ||
categories: [], | ||
category: '', | ||
search: '', | ||
results: [], | ||
searched: false | ||
}); | ||
|
||
const { categories, category, search, results, searched } = data; | ||
|
||
const loadCategories = () => { | ||
getCategories().then(data => { | ||
if (data.error) { | ||
console.log(data.error); | ||
} else { | ||
setData({ ...data, categories: data }); | ||
} | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
loadCategories(); | ||
}, []); | ||
|
||
const searchData = () => { | ||
// console.log(search, category); | ||
if (search) { | ||
list({ search: search || undefined, category: category }).then( | ||
response => { | ||
if (response.error) { | ||
console.log(response.error); | ||
} else { | ||
setData({ ...data, results: response, searched: true }); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
|
||
const searchSubmit = e => { | ||
e.preventDefault(); | ||
searchData(); | ||
}; | ||
|
||
const handleChange = name => event => { | ||
setData({ ...data, [name]: event.target.value, searched: false }); | ||
}; | ||
|
||
const searchMessage = (searched, results) => { | ||
if (searched && results.length > 0) { | ||
return `Found ${results.length} products`; | ||
} | ||
if (searched && results.length < 1) { | ||
return `No products found`; | ||
} | ||
}; | ||
|
||
const searchedProducts = (results = []) => { | ||
return ( | ||
<div> | ||
<h2 className='mt-4 mb-4'>{searchMessage(searched, results)}</h2> | ||
|
||
<div className='row'> | ||
{results.map((product, i) => ( | ||
// <div className='col-4 mb-3'> | ||
<CardForProduct key={i} product={product} /> | ||
// </div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const searchForm = () => ( | ||
<form onSubmit={searchSubmit}> | ||
<span className='input-group-text'> | ||
<div className='input-group input-group-lg'> | ||
<div className='input-group-prepend'> | ||
<select className='btn mr-2' onChange={handleChange('category')}> | ||
<option value='All'>All</option> | ||
{categories.map((c, i) => ( | ||
<option key={i} value={c._id}> | ||
{c.name} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
|
||
<input | ||
type='search' | ||
className='form-control' | ||
onChange={handleChange('search')} | ||
placeholder='Search by name' | ||
/> | ||
</div> | ||
<div className='btn input-group-append' style={{ border: 'none' }}> | ||
<button className='input-group-text'>Search</button> | ||
</div> | ||
</span> | ||
</form> | ||
); | ||
|
||
return ( | ||
<div className='row'> | ||
<div className='container mb-3'>{searchForm()}</div> | ||
<div className='container-fluid mb-3'>{searchedProducts(results)}</div> | ||
{/* {JSON.stringify(results)} */} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Search; |
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 |
---|---|---|
|
@@ -129,5 +129,5 @@ | |
} | ||
|
||
.jumbotron-font-color { | ||
color: #09f127; | ||
color: #fff; | ||
} |
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