Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import logo from './logo.svg';
import './App.css';
import ProductsPage from './components/ProductsPage';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<ProductsPage/>
</div>
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/components/ProductRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function ProductRow({ product }) {
return (
<tr style={{ color: product.inStock ? 'black' : 'red' }}>
<td>{product.name}</td>
<td>{product.price}</td>
<td>{product.inStock ? 'In Stock' : 'Out of Stock'}</td>
</tr>
);
}

export default ProductRow;

22 changes: 22 additions & 0 deletions src/components/ProductTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ProductRow from './ProductRow';

function ProductTable({ products }) {
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{products.map((product) => (
<ProductRow key={product.id} product={product} />
))}
</tbody>
</table>
);
}

export default ProductTable;
28 changes: 28 additions & 0 deletions src/components/ProductsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useState } from 'react';
import jsonData from '../data.json';
import SearchBar from './SearchBar';
import ProductTable from './ProductTable';

function ProductsPage() {
const [products, setProducts] = useState(jsonData);
const [filteredProducts, setFilteredProducts] = useState(products);

const handleSearch = (searchTerm) => {
const filtered = products.filter(product =>
product.name.toLowerCase().includes(searchTerm.toLowerCase())
);
setFilteredProducts(filtered);
};

return (
<div>
<h1>Root Store</h1>
<SearchBar onSearch={handleSearch} />
<div>
<ProductTable products={filteredProducts} />
</div>
</div>
);
}

export default ProductsPage;
37 changes: 37 additions & 0 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useState } from 'react';

function SearchBar({ onSearch }) {
const [searchTerm, setSearchTerm] = useState('');
const [onlyInStock, setOnlyInStock] = useState(false);

const handleChange = (event) => {
setSearchTerm(event.target.value);
onSearch(event.target.value, onlyInStock); // Include stock filter
};

const handleCheckboxChange = (event) => {
setOnlyInStock(event.target.checked);
onSearch(searchTerm, event.target.checked); // Trigger search with stock filter
};

return (
<div>
<input
type="text"
placeholder="Search..."
value={searchTerm}
onChange={handleChange}
/>
<label>
<input
type="checkbox"
checked={onlyInStock}
onChange={handleCheckboxChange}
/>
Only show products in stock
</label>
</div>
);
}

export default SearchBar;