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

[AFP-15] Fix crash upon deleting item in search results page

parent 7d32de39
import React from "react";
import ProductRow from "./ProductRow.jsx";
import { Table } from "react-bootstrap";
// import "./../styles/ProductTable.css";
export default class SearchResults extends React.Component {
import { deleteProduct } from "../actions/apiRequests";
import { withRouter } from "react-router";
class SearchResults extends React.Component {
constructor(props) {
super(props);
this.state = {
results: this.props.history.location.state.results
}
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(sku){
deleteProduct(sku)
.then(() => {
const newResults = this.state.results.filter(product => product.sku !== sku);
this.setState({results: newResults});
this.props.history.push("/searchResults", this.state);
});
}
render() {
......@@ -31,7 +43,7 @@ export default class SearchResults extends React.Component {
<tbody>
{this.state.results.map((product) => {
return (
<ProductRow key={product.sku} product={product} />
<ProductRow key={product.sku} product={product} handleDelete={() => this.handleDelete(product.sku)} />
);
})}
</tbody>
......@@ -43,4 +55,6 @@ export default class SearchResults extends React.Component {
);
}
}
\ No newline at end of file
}
export default withRouter(SearchResults);
\ 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