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
70 changes: 45 additions & 25 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
/* Global reset */
* {
box-sizing: border-box;
}

/* App container */
.App {
max-width: 700px;
margin: 40px auto;
padding: 20px;
font-family: Arial, Helvetica, sans-serif;
background-color: #f9f9f9;
}

/* Title */
h1 {
text-align: center;
margin-bottom: 20px;
}

/* Search bar */
input[type="text"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
font-size: 14px;
}

.App-logo {
height: 40vmin;
pointer-events: none;
/* Checkbox area */
label {
font-size: 14px;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
/* Table */
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
background-color: white;
}

.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;
/* Table header */
th {
text-align: left;
padding: 8px;
border-bottom: 2px solid #ddd;
}

.App-link {
color: #61dafb;
/* Table rows */
td {
padding: 8px;
border-bottom: 1px solid #eee;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
/* Row hover effect */
tbody tr:hover {
background-color: #f1f1f1;
}
28 changes: 10 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import logo from './logo.svg';
import './App.css';

// src/App.js

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>
<div className="App">
<ProductsPage />
</div>
);
}

export default App;
export default App;
14 changes: 14 additions & 0 deletions src/components/ProductRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function ProductRow({ product }) {
const textStyle = {
color: product.inStock ? "black" : "red",
};

return (
<tr>
<td style={textStyle}>{product.name}</td>
<td>{product.price}</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>
</tr>
</thead>

<tbody>
{products.map((product) => (
<ProductRow key={product.name} product={product} />
))}
</tbody>
</table>
);
}

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

import SearchBar from "./SearchBar";
import ProductTable from "./ProductTable";

function ProductsPage() {
const [products] = useState(jsonData);
const [searchText, setSearchText] = useState("");
const [inStockOnly, setInStockOnly] = useState(false);

const filteredProducts = products.filter((product) => {
const matchesSearch = product.name
.toLowerCase()
.includes(searchText.toLowerCase());

const matchesStock = inStockOnly ? product.inStock : true;

return matchesSearch && matchesStock;
});

return (
<div>
<h1>Root Store</h1>

<SearchBar
searchText={searchText}
onSearchChange={setSearchText}
inStockOnly={inStockOnly}
onInStockChange={setInStockOnly}
/>

<ProductTable products={filteredProducts} />
</div>
);
}

export default ProductsPage;
28 changes: 28 additions & 0 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function SearchBar({
searchText,
onSearchChange,
inStockOnly,
onInStockChange,
}) {
return (
<div style={{ marginBottom: "20px" }}>
<input
type="text"
placeholder="Search..."
value={searchText}
onChange={(e) => onSearchChange(e.target.value)}
/>

<label>
<input
type="checkbox"
checked={inStockOnly}
onChange={(e) => onInStockChange(e.target.checked)}
/>
{" "}Only show products in stock
</label>
</div>
);
}

export default SearchBar;