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
54 changes: 29 additions & 25 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
/* src/App.css */

.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
h1 {
color: #333;
}

div {
margin-bottom: 20px;
}

input {
margin-right: 10px;
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
th {
background-color: #f2f2f2;
}

.App-link {
color: #61dafb;
tr:nth-child(even) {
background-color: #f9f9f9;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
tr:hover {
background-color: #ddd;
}

19 changes: 4 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import logo from './logo.svg';
import React from 'react';
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
30 changes: 30 additions & 0 deletions src/components/ProductTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useState } from 'react';
import ProductRow from './ProductsRow';

const ProductTable = ({ products, searchTerm, inStockOnly }) => {
const filteredProducts = products
.filter((product) =>
product.name.toLowerCase().includes(searchTerm.toLowerCase())
)
.filter((product) => (inStockOnly ? product.inStock : true));

return (
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{filteredProducts.map((product, index) => (
<ProductRow key={index} product={product} />
))}
</tbody>
</table>
</div>
);
};

export default ProductTable;
36 changes: 36 additions & 0 deletions src/components/ProductsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 [searchTerm, setSearchTerm] = useState('');
const [inStockOnly, setInStockOnly] = useState(false);

const handleSearchChange = (search) => {
setSearchTerm(search);
};

const handleStockChange = (stockStatus) => {
setInStockOnly(stockStatus);
};

return (
<div>
<h1>Root Store</h1>
<SearchBar
onSearchChange={handleSearchChange}
onStockChange={handleStockChange}
inStockOnly={inStockOnly}
/>
<ProductTable
products={products}
searchTerm={searchTerm}
inStockOnly={inStockOnly}
/>
</div>
);
}

export default ProductsPage;
17 changes: 17 additions & 0 deletions src/components/ProductsRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

const ProductRow = ({ product }) => {
const { name, category, price, inStock } = product;
const rowStyle = {
color: inStock ? 'black' : 'red',
};

return (
<tr style={rowStyle}>
<td>{name}</td>
<td>{price}</td>
</tr>
);
};

export default ProductRow;
31 changes: 31 additions & 0 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

const SearchBar = ({ onSearchChange, onStockChange, inStockOnly }) => {
const handleSearchChange = (event) => {
onSearchChange(event.target.value);
};

const handleStockChange = (event) => {
onStockChange(event.target.checked);
};

return (
<div>
<input
type="text"
placeholder="Search products..."
onChange={handleSearchChange}
/>
<label>
<input
type="checkbox"
checked={inStockOnly}
onChange={handleStockChange}
/>
Only show products in stock
</label>
</div>
);
};

export default SearchBar;
File renamed without changes.