Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rivadar-internship #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
51 changes: 26 additions & 25 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@
text-align: center;
}

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

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

.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;
input {
margin-right: 10px;
}

.App-link {
color: #61dafb;
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

th {
background-color: #f2f2f2;
}

tr:nth-child(even) {
background-color: #f9f9f9;
}

tr:hover {
background-color: #ddd;
}
21 changes: 5 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
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>
);
}

export default App;
export default App;
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.