Skip to content

State and Events Lab #28

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 2 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
12 changes: 9 additions & 3 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import React from "react";
import React, { useState } from "react";

import ShoppingList from "./ShoppingList";
import itemData from "../data/items";

function App() {
const [isOn, setIsOn] = useState(false);

function handleClick() {
setIsOn((isOn) => !isOn);
}

// 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 = isOn ? "App dark" : "App light";

return (
<div className={appClass}>
<header>
<h2>Shopster</h2>
<button>Dark Mode</button>
<button onClick={handleClick}>{isOn ? "Dark" : "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 [isInCart, setIsInCart] = useState(false);

function handleClick() {
setIsInCart((isInCart) => !isInCart);
}

return (
<li className="">
<li className={isInCart ? "in-cart" : ""}>
<span>{name}</span>
<span className="category">{category}</span>
<button className="add">Add to Cart</button>
<button className="add" onClick={handleClick}>
{isInCart ? "Remove from" : "Add To"} Cart
</button>
</li>
);
}
Expand Down
18 changes: 15 additions & 3 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import React from "react";
import React, { useState } from "react";

import Item from "./Item";

function ShoppingList({ items }) {
const [displayItems, setDisplayItems] = useState(items);

function handleChange(event) {
setDisplayItems(
items.filter((item) => {
if (event.target.value === "All") return true;
else return item.category === event.target.value;
})
);
}

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