Skip to content

State and Events Lab #26

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

Open
wants to merge 3 commits 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
9 changes: 4 additions & 5 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import React from "react";
import React, { useState } from "react";
import ShoppingList from "./ShoppingList";
import itemData from "../data/items";

function App() {
const [isDark, setIsDark] = useState(false);

// replace 'false' with a state variable that can be toggled between true and false
// this will be used for the Dark Mode Toggle feature
const appClass = false ? "App dark" : "App light"
const appClass = isDark ? "App dark" : "App light";

return (
<div className={appClass}>
<header>
<h2>Shopster</h2>
<button>Dark Mode</button>
<button onClick={() => setIsDark(!isDark)}>Dark Mode</button>
</header>
<ShoppingList items={itemData} />
</div>
Expand Down
11 changes: 8 additions & 3 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from "react";
import React, { useState } from "react";

function Item({ name, category }) {
const [inCart, setInCart] = useState(false);

const itemClass = inCart ? "in-cart" : "";
const buttonText = inCart ? "Remove From Cart" : "Add to Cart";

return (
<li className="">
<li className={itemClass}>
<span>{name}</span>
<span className="category">{category}</span>
<button className="add">Add to Cart</button>
<button className="add" onClick={() => setInCart(!inCart)}>{buttonText}</button>
</li>
);
}
Expand Down
28 changes: 21 additions & 7 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import React from "react";
import React, { useState } from "react";
import Item from "./Item";

function ShoppingList({ items }) {
const [selectedCategory, setCategory] = useState("All");

function filterItems(items) {
const itemsList =
selectedCategory === "All"
? items
: items.filter((item) => item.category === selectedCategory);

return itemsList.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
));
}

const itemsList = filterItems(items);

return (
<div className="ShoppingList">
<div className="Filter">
<select name="filter">
<select
name="filter"
onChange={(event) => setCategory(event.target.value)}
>
<option value="All">Filter by category</option>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
</select>
</div>
<ul className="Items">
{items.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
))}
</ul>
<ul className="Items">{itemsList}</ul>
</div>
);
}
Expand Down