Skip to content
This repository was archived by the owner on Jul 24, 2018. It is now read-only.

Commit e7794c3

Browse files
author
Oscar Lodriguez
committed
simplified code, added advice from Atlas7
1 parent a9fb3ba commit e7794c3

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

app/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {render} from 'react-dom';
44
import FilterableProductTable from './src/filterable-product-table';
55

66
// init shell
7-
renderShell();
7+
initShell();
88

9-
function renderShell() {
9+
function initShell() {
1010
var shell = document.createElement('main');
1111
shell.className = 'app-shell';
1212
document.body.appendChild(shell);

app/src/components/category-row.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React from 'react';
22

3-
const CategoryRow = ({category}) => (
3+
export default ({category}) => (
44
<tr>
55
<th colSpan="2">{category}</th>
66
</tr>
7-
);
8-
9-
export default CategoryRow;
7+
);

app/src/components/product-row.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React from 'react';
22

3-
const ProductRow = ({product}) => {
3+
export default ({product}) => {
44
var name = product.stocked ? product.name : <span style={{color: 'red'}}> {product.name} </span>;
55
return (
66
<tr>
77
<td>{name}</td>
88
<td>{product.price}</td>
99
</tr>
1010
);
11-
};
12-
13-
export default ProductRow;
11+
};

app/src/components/product-table.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@ import ProductRow from './product-row';
44

55
export default ({products, filterText, inStockOnly}) => {
66

7-
let rows = [],
8-
lastCategory = null;
7+
let rows = [], lastCategory = null;
98

109
products.map((product) => {
11-
if (product.name.indexOf(filterText) === -1 || (!product.stocked && inStockOnly)) {
10+
if (product.name.toLowerCase().indexOf(filterText.toLowerCase()) === -1 || (!product.stocked && inStockOnly)) {
1211
return;
1312
}
1413

1514
if (product.category !== lastCategory) {
16-
rows.push(
17-
<CategoryRow category={product.category} key={product.category}/>
18-
);
15+
rows.push(<CategoryRow category={product.category} key={product.category}/>);
1916
}
17+
2018
rows.push(<ProductRow product={product} key={product.name}/>);
2119
lastCategory = product.category;
20+
2221
});
2322

24-
return (
25-
<table>
26-
<thead>
27-
<tr>
28-
<th>Name</th>
29-
<th>Price</th>
30-
</tr>
31-
</thead>
32-
<tbody>{rows}</tbody>
33-
</table>
34-
);
23+
if (rows.length > 0) {
24+
return (
25+
<table>
26+
<thead>
27+
<tr>
28+
<th>Name</th>
29+
<th>Price</th>
30+
</tr>
31+
</thead>
32+
<tbody>{rows}</tbody>
33+
</table>
34+
);
35+
} else {
36+
return (<p>No results found</p>);
37+
}
3538
}

app/src/components/search-bar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react';
1+
import React, {Component} from 'react';
22

3-
class SearchBar extends React.Component {
3+
class SearchBar extends Component {
44

55
constructor() {
66
super();

0 commit comments

Comments
 (0)