Skip to content
Open

done #39

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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-scripts": "^5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
21 changes: 6 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
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
28 changes: 28 additions & 0 deletions src/Product.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ProductRow.css */

.product-row {
color: black;
}

.product-row.out-of-stock {
color: red;
}

/* ProductTable.css */

.product-table {
width: 100%;
border-collapse: collapse;
}

.product-table th,
.product-table td {
border: 1px solid #dddddd;
padding: 8px;
text-align: left;
}

.product-table th {
background-color: #f2f2f2;
}

18 changes: 18 additions & 0 deletions src/components/ProductRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


import React from 'react';
import "../Product.css";

function ProductRow({ product }) {
const rowClassName = product.inStock ? 'product-row' : 'product-row out-of-stock';


return (
<tr className={rowClassName}>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
);
}

export default ProductRow;
27 changes: 27 additions & 0 deletions src/components/ProductTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


import React from 'react';
import ProductRow from './ProductRow';
import "../Product.css";

function ProductTable({ products }) {
return (
<div>
<table className="product-table">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{products.map((product, index) => (
<ProductRow key={index} product={product} />
))}
</tbody>
</table>
</div>
);
}

export default ProductTable;
45 changes: 45 additions & 0 deletions src/components/ProductsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


import React, { 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(jsonData);
const [showOnlyInStock, setShowOnlyInStock] = useState(false);


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

const handleCheckboxChange = (e) => {
setShowOnlyInStock(e.target.checked);
if (e.target.checked) {
const filtered = products.filter(product => product.inStock);
setFilteredProducts(filtered);
} else {
setFilteredProducts(products);
}
};






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

export default ProductsPage;
17 changes: 17 additions & 0 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


import React from 'react';

function SearchBar({ handleSearch,handleCheckboxChange }) {
return (
<div>
<input type="text" placeholder="Search..." onChange={(e) => handleSearch(e.target.value)} />
<label>
<input type="checkbox" onChange={handleCheckboxChange} />
Only show products in stock
</label>
</div>
);
}

export default SearchBar;