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
38 changes: 0 additions & 38 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +0,0 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.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;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
19 changes: 3 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import logo from './logo.svg';
import './App.css';
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
11 changes: 11 additions & 0 deletions src/components/ProductRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function ProductRow({ prodName, price, stock }) {
let itemColor = stock ? "black" : "red";
return (
<tr>
<td style={{ color: itemColor }}>{prodName}</td>
<td>{price}</td>
</tr>
);
}

export default ProductRow;
16 changes: 16 additions & 0 deletions src/components/ProductTable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#tablebox {
width: 80%;
text-align: center;
border-collapse: collapse;
margin: 30px;
}

#tablebox th {
background-color: #e7ecef;
padding: 15px;
}

#tablebox td {
padding: 15px;
border-top: 3px solid #e7ecef;
}
26 changes: 26 additions & 0 deletions src/components/ProductTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "./ProductTable.css";
import ProductRow from "./ProductRow";

function ProductTable({ prodList }) {
let prodItems = prodList.map((item) => (
<ProductRow
prodName={item.name}
price={item.price}
stock={item.inStock}
key={item.id}
/>
));
return (
<table id="tablebox">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{prodItems}</tbody>
</table>
);
}

export default ProductTable;
5 changes: 5 additions & 0 deletions src/components/ProductsPage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#products-page {
display: flex;
flex-direction: column;
align-items: center;
}
41 changes: 41 additions & 0 deletions src/components/ProductsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState } from "react";
import "./ProductsPage.css";

import jsonData from "./../data.json";
import SearchBar from "./SearchBar";
import ProductTable from "./ProductTable";

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

let stockProducts = jsonData
.filter((item) => item.inStock)
.map((item) => item);

function handleChange(e) {
let filteredProducts = products
.filter((item) =>
item.name.toLowerCase().includes(e.target.value.toLowerCase())
)
.map((item) => item);
setProducts(filteredProducts);
}

function handleCheck(e) {
if (e.target.checked) {
setProducts(stockProducts);
} else {
setProducts(jsonData);
}
}

return (
<div id="products-page">
<h1>Store</h1>
<SearchBar onTypeChange={handleChange} onChecked={handleCheck} />
<ProductTable prodList={products} />
</div>
);
}

export default ProductsPage;
15 changes: 15 additions & 0 deletions src/components/SearchBar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#search-bar {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 90%;
padding: 10px;
gap: 10px;
}

#psearch {
height: 30px;
width: 90%;
box-sizing: border-box;
}
16 changes: 16 additions & 0 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "./SearchBar.css";

function SearchBar({ onTypeChange, onChecked }) {
return (
<div id="search-bar">
<div>Search</div>
<input type="text" id="psearch" onChange={onTypeChange} />
<div>
<input type="checkbox" id="sfilter" onChange={onChecked} />
<label htmlFor="sfilter">Only show products in stock</label>
</div>
</div>
);
}

export default SearchBar;