Commit 2628a19b authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-12] Finish work on search item functionality

parent b27449cb
...@@ -3,16 +3,20 @@ import axios from 'axios'; ...@@ -3,16 +3,20 @@ import axios from 'axios';
export const getAllProducts = async data => { export const getAllProducts = async data => {
const res = await axios.get(`${Config.inventoryUrl}`); const res = await axios.get(`${Config.inventoryUrl}`);
// console.log(res.data);
return res.data; return res.data;
} }
export const getFilteredProducts = async (searchTerm) => { export const getFilteredProducts = async (searchTerm, searchBy) => {
const res = await axios.get(`${Config.inventoryUrl}`); const res = await axios.get(`${Config.inventoryUrl}`);
return res.data.filter(product => { return res.data.filter(product => {
const productName = product.productName.toLowerCase(); let productFiltered;
console.log("Search Term: " + searchTerm);
console.log("Found Product Name: " + productName.includes(searchTerm.toLowerCase())); if(searchBy === "name"){
return productName.includes(searchTerm.toLowerCase()); productFiltered = product.productName.toLowerCase();
}else if(searchBy === "sku"){
productFiltered = product.sku.toLowerCase();
}
return productFiltered.includes(searchTerm.toLowerCase());
}); });
} }
\ No newline at end of file
...@@ -22,7 +22,6 @@ export default function Main() { ...@@ -22,7 +22,6 @@ export default function Main() {
return ( return (
<div> <div>
<Switch> <Switch>
<AuthRoute path="/searchResults" component={SearchResults}/>
<AuthRoute exact path="/products/new"> <AuthRoute exact path="/products/new">
<ProductForm method="POST" /> <ProductForm method="POST" />
</AuthRoute> </AuthRoute>
...@@ -39,6 +38,7 @@ export default function Main() { ...@@ -39,6 +38,7 @@ export default function Main() {
<AuthRoute exact path="/"> <AuthRoute exact path="/">
<Redirect to="/products" /> <Redirect to="/products" />
</AuthRoute> </AuthRoute>
<AuthRoute path="/searchResults" component={SearchResults}/>
<AuthRoute>404 PAGE</AuthRoute> <AuthRoute>404 PAGE</AuthRoute>
</Switch> </Switch>
</div> </div>
......
import React from 'react'; import React from 'react';
import { withRouter, Redirect } from "react-router-dom"; import { withRouter, Link } from "react-router-dom";
import { getFilteredProducts } from '../actions/apiRequests'; import { getFilteredProducts } from '../actions/apiRequests';
import "./../styles/Search.css";
class Search extends React.Component { class Search extends React.Component {
constructor(props){ constructor(props){
super(props); super(props);
this.state = { this.state = {
searchTerm: '', searchTerm: '',
results: [] searchBy: 'name',
results: [],
}; };
this.submit = this.submit.bind(this); this.submit = this.submit.bind(this);
this.changeTerm = this.changeTerm.bind(this); this.changeTerm = this.changeTerm.bind(this);
this.handleRadioButton = this.handleRadioButton.bind(this);
} }
changeTerm(event) { changeTerm(event) {
this.setState({searchTerm: event.target.value}); this.setState({searchTerm: event.target.value});
} }
handleRadioButton(value) {
this.setState({
searchBy: value
});
}
async submit(event){ async submit(event){
event.preventDefault(); event.preventDefault();
const data = await getFilteredProducts(this.state.searchTerm) const data = await getFilteredProducts(this.state.searchTerm, this.state.searchBy)
.then(res => { .then(res => {
this.setState({results: res}); this.setState({results: res});
}) })
...@@ -39,11 +48,19 @@ class Search extends React.Component { ...@@ -39,11 +48,19 @@ class Search extends React.Component {
placeholder="Search for item..." placeholder="Search for item..."
onChange={event => this.changeTerm(event)} onChange={event => this.changeTerm(event)}
/> />
<div className="form-check form-check-inline">
<input className="form-check-input" type="radio" name="inlineRadioOptions" checked={this.state.searchBy === "name"} onChange={() => this.handleRadioButton("name")} id="searchByName" value="name" />
<label className="form-check-label" htmlFor="searchByName">Name</label>
</div>
<div className="form-check form-check-inline">
<input className="form-check-input" type="radio" name="inlineRadioOptions" checked={this.state.searchBy === "sku"} onChange={() => this.handleRadioButton("sku")} id="searchBySku" value="sku" />
<label className="form-check-label" htmlFor="searchBySKU">Sku</label>
</div>
<button className="btn btn-light" type="submit"> <button className="btn btn-light" type="submit">
GO GO
</button> </button>
</form> </form>
<Redirect to={{ <Link to={{
pathname: '/searchResults', pathname: '/searchResults',
state: {results: this.state.results} state: {results: this.state.results}
}} /> }} />
......
...@@ -17,13 +17,17 @@ export default class SearchResults extends React.Component{ ...@@ -17,13 +17,17 @@ export default class SearchResults extends React.Component{
<h1 id="title" className="text-center" >Search Results</h1> <h1 id="title" className="text-center" >Search Results</h1>
<Container id="prod-grid" className="mt-3"> <Container id="prod-grid" className="mt-3">
<Row xs={1} sm={2} md={3} lg={4}> <Row xs={1} sm={2} md={3} lg={4}>
{this.state.results.map((product) => { {this.state.results.length > 0 ? this.state.results.map((product) => {
return ( return (
<Col key={product.sku}> <Col key={product.sku}>
<Product product={product} /> <Product product={product} />
</Col> </Col>
); );
})} }) :
<Col>
<p>Unable to find any matching products.</p>
</Col>
}
</Row> </Row>
</Container> </Container>
</div> </div>
......
.form-check-label{
color: rgba(255, 255, 255, 0.55)
}
.form-check.form-check-inline{
margin-top: 0.35rem;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment