Commit b27449cb authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-12] Got search by name working

parent 89b7e0ed
......@@ -10,9 +10,9 @@ export const getAllProducts = async data => {
export const getFilteredProducts = async (searchTerm) => {
const res = await axios.get(`${Config.inventoryUrl}`);
return res.data.filter(product => {
const productName = product.name.toLowerCase();
const productName = product.productName.toLowerCase();
console.log("Search Term: " + searchTerm);
console.log("Found Product Name: " + productName.includes(searchTerm));
return productName.includes(searchTerm);
console.log("Found Product Name: " + productName.includes(searchTerm.toLowerCase()));
return productName.includes(searchTerm.toLowerCase());
});
}
\ No newline at end of file
......@@ -22,7 +22,7 @@ export default function Main() {
return (
<div>
<Switch>
<AuthRoute path="/searchResults"><SearchResults /></AuthRoute>
<AuthRoute path="/searchResults" component={SearchResults}/>
<AuthRoute exact path="/products/new">
<ProductForm method="POST" />
</AuthRoute>
......
import React from 'react';
import { Redirect, withRouter } from "react-router-dom";
import { withRouter, Redirect } from "react-router-dom";
import { getFilteredProducts } from '../actions/apiRequests';
class Search extends React.Component {
......@@ -19,14 +19,13 @@ class Search extends React.Component {
}
async submit(event){
console.log(this.state.searchTerm);
event.preventDefault();
const data = await getFilteredProducts(this.state.searchTerm)
.then(res => {
this.setState({result: res.data}, () => {
this.props.history.push("/searchResults");
});
this.setState({results: res});
})
.catch(err => console.log(err));
this.props.history.push("/searchResults", this.state);
}
//Need to search by name or SKU
......@@ -38,18 +37,16 @@ class Search extends React.Component {
type="search"
className="form-control me-2"
placeholder="Search for item..."
onChange={this.changeTerm}
onChange={event => this.changeTerm(event)}
/>
<button className="btn btn-light" type="submit">
GO
</button>
</form>
{/* {
<Redirect to={{
pathname: '/searchResults',
state: {results: this.state.results}
}} />
} */}
</div>
);
}
......
......@@ -7,7 +7,7 @@ export default class SearchResults extends React.Component{
constructor(props){
super(props);
this.state = {
results: []
results: this.props.history.location.state.results
}
}
......
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