Skip to content

Update App State Based on Events #24

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 4 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
13 changes: 8 additions & 5 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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"
function handleDarkMode() {
setIsDark((isDark) => !isDark)
}

const appClass = isDark ? "App dark" : "App light"

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

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

function handleCartAdd(){
setInCart((inCart) => !inCart)
}

const liClass = inCart ? "" : "in-cart"

return (
<li className="">
<li className={liClass}>
<span>{name}</span>
<span className="category">{category}</span>
<button className="add">Add to Cart</button>
<button onClick={handleCartAdd} className="add">{inCart ? "Add to Cart" : "Remove From Cart"}</button>
</li>
);
}
Expand Down
20 changes: 17 additions & 3 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import React from "react";
import React, {useState} from "react";
import Item from "./Item";

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

function handleFilterChange(event) {
setSelectedCategory(event.target.value)
}

const itemsToDisplay = items.filter((item) => {
if (selectedCategory === "All") {
return true
} else {
return item.category === selectedCategory
}
})

return (
<div className="ShoppingList">
<div className="Filter">
<select name="filter">
<select name="filter" onChange={handleFilterChange}>
<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) => (
{itemsToDisplay.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
))}
</ul>
Expand Down