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