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

Lab Assignment-15 #53

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
18 changes: 3 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
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>
<h1 className="store-title">Store</h1>
<ProductsPage />
</div>
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/components/ProductRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";

function ProductRow({ product }) {
return (
<tr>
<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 './css/style.css'

function ProductTable({ products }) {
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{products.map((product) => (
<tr
key={product.id}
className={!product.inStock ? "out-of-stock" : ""}
>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
))}
</tbody>
</table>
);
}
export default ProductTable;
50 changes: 50 additions & 0 deletions src/components/ProductsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState } from "react";
import SearchBar from "./SearchBar";
import ProductTable from "./ProductTable";
import data from "../data.json";
import "./css/style.css";

function ProductsPage() {
const [searchQuery, setSearchQuery] = useState("");
const [onlyInStock, setOnlyInStock] = useState(false);

const handleSearchChange = (query) => {
setSearchQuery(query);
};

const handleInStockChange = (event) => {
setOnlyInStock(event.target.checked);
};

const filteredProducts = data.filter((product) => {
const matchesSearchQuery = product.name
.toLowerCase()
.includes(searchQuery.toLowerCase());
const matchesStockFilter = !onlyInStock || product.inStock;
return matchesSearchQuery && matchesStockFilter;
});

return (
<div>
<div className="search-title">Search</div>
<SearchBar
searchQuery={searchQuery}
onSearchChange={handleSearchChange}
/>
<div className="in-stock-filter">
<input
type="checkbox"
id="in-stock-checkbox"
checked={onlyInStock}
onChange={handleInStockChange}
/>
<label htmlFor="in-stock-checkbox">Only show products in stock</label>
</div>
<div className="table-wrapper">
<ProductTable products={filteredProducts} />
</div>
</div>
);
}

export default ProductsPage;
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 React from 'react'
import './css/style.css'

function SearchBar({ searchQuery, onSearchChange }) {
return (
<div className="search-bar">
<input
type="text"
value={searchQuery}
onChange={(e) => onSearchChange(e.target.value)}
/>
</div>
);
}

export default SearchBar
77 changes: 77 additions & 0 deletions src/components/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.table-wrapper {
display: flex;
justify-content: center;
align-items: center;
width: 1500px;
margin-left: 0px;
padding-left: 200px;
}

table {
width: 100%;
max-width: 2000px;
border-collapse: collapse;
}

table th:first-child
{
width: 100%;
padding-left: 0px;
padding-right: -300px;
}

table th:nth-child(2) {
width: 300px;
margin-left: 500px;
}

table th:first-child,
table th:nth-child(2)
{
background-color: rgb(209, 213, 222);
color: grey;
}

table th,
table td {
padding-top: 20px;
padding-bottom: 20px;
padding-right: 300px;
text-align: center;
}

td:first-child {
width: 300px;
padding-left: 0px;
}

table th:nth-child(2) {
width: 150px;
padding-right: -200px;
}

th:last-child,
.search-bar {
width: 100%;
margin-bottom: 20px;
}

.search-bar input {
width: 80%;
padding: 8px;
box-sizing: border-box;
}

div.search-title {
padding-bottom: 15px !important;
}

.in-stock-filter {
padding-bottom: 50px;
}


.out-of-stock {
color: red;

}