Skip to content

complete #18

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 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
19 changes: 14 additions & 5 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import React from "react";
import React, { useState } from "react";
import ShoppingList from "./ShoppingList";
import itemData from "../data/items";

function App() {
// Create a state variable to track whether dark mode is enabled
const [isDarkMode, setIsDarkMode] = 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"
// Toggle dark mode
const toggleDarkMode = () => {
setIsDarkMode(!isDarkMode);
};

// Define the CSS class based on the state variable
const appClass = isDarkMode ? "App dark" : "App light";

// Define the button text based on the state variable
const buttonText = isDarkMode ? "Toggle Light Mode" : "Toggle Dark Mode";

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

function Item({ name, category }) {
const [isInCart, setIsInCart] = useState(false); // Initialize with false
const toggleCartStatus = () => {
setIsInCart(!isInCart); // Toggle the cart status
};
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={toggleCartStatus}>
{isInCart ? "Remove From Cart" : "Add to cart"} {/* "Add to cart" here */}
</button>
</li>
);
}
Expand Down
24 changes: 21 additions & 3 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import React from "react";
import React, { useState } from "react";
import Item from "./Item";

function ShoppingList({ items }) {
// Create a state variable for the selected category and initialize it to "All"
const [selectedCategory, setSelectedCategory] = useState("All");

// Function to handle changes in the <select> element
const handleCategoryChange = (event) => {
setSelectedCategory(event.target.value);
};

// Filter items based on the selected category
const filteredItems =
selectedCategory === "All"
? items
: items.filter((item) => item.category === selectedCategory);

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