Skip to content
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
5 changes: 4 additions & 1 deletion src/__tests__/Item.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import Item from "../components/Item";
import ShoppingList from "../components/ShoppingList";


test("the <li> does not have a className when initialized", () => {
const { container } = render(<Item name="Milk" category="Dairy" />);
Expand All @@ -9,7 +11,8 @@ test("the <li> does not have a className when initialized", () => {
});

test("the <li> has a className of 'in-cart' when the Add to Cart button is clicked", () => {
const { container } = render(<Item name="Milk" category="Dairy" />);
const items = [{ id: 4, name: "Milk", category: "Dairy" }]
const { container } = render(<ShoppingList items={items} />);
fireEvent.click(screen.getByText(/ Cart/));
expect(container.querySelector(".in-cart")).toBeInTheDocument();
});
21 changes: 9 additions & 12 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import React from "react";
import ShoppingList from "./ShoppingList";
import itemData from "../data/items";
import React, { useState } from "react"
import ShoppingList from "./ShoppingList"
import itemData from "../data/items"
import Header from "./Header"

function App() {

// 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 [darkMode, setDarkMode] = useState(false)
const appClass = darkMode ? "App dark" : "App light"
const onDarkModeClick = () => setDarkMode(!darkMode)

return (
<div className={appClass}>
<header>
<h2>Shopster</h2>
<button>Dark Mode</button>
</header>
<Header onDarkModeClick={onDarkModeClick} darkMode={darkMode}/>
<ShoppingList items={itemData} />
</div>
);
}

export default App;
export default App
16 changes: 16 additions & 0 deletions src/components/Filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";

function Filter({ filterCart }) {
return (
<div className="Filter">
<select name="filter" onChange={filterCart}>
<option value="All">Filter by category</option>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
</select>
</div>
)
}

export default Filter
12 changes: 12 additions & 0 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";

function Header({ onDarkModeClick, darkMode }) {
return (
<header>
<h2>Shopster</h2>
<button onClick={onDarkModeClick}>{darkMode === true ? "Dark Mode" : "Light Mode"}</button>
</header>
);
}

export default Header;
8 changes: 4 additions & 4 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react";

function Item({ name, category }) {
function Item({ name, category, addItemFun, cartClass = "" }) {
return (
<li className="">
<li className={cartClass}>
<span>{name}</span>
<span className="category">{category}</span>
<button className="add">Add to Cart</button>
<button className="add" onClick={addItemFun}>{cartClass === "" ? "Add to Cart" : "Remove From Cart"}</button>
</li>
);
)
}

export default Item;
74 changes: 61 additions & 13 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@
import React from "react";
import React, { useState } from "react";
import Item from "./Item";
import Filter from "./Filter";

function ShoppingList({ items }) {
const [cart, setCart] = useState(items)

const updateCart = (e) => {
const action = e.target.innerHTML

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const action = e.target.innerHTML
const buttonText = e.target.innerHTML

console.log('Func ran', e.target.innerHTML)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('Func ran', e.target.innerHTML)

if(action === 'Add to Cart'){
addToCart(e)
} else {
removeFromCart(e)
}
}

const addToCart = (e) => {
const itemName = e.target.parentElement.childNodes[0].textContent
const updatedCart = cart.map(item => {
if(item.name === itemName){
return {
...item,
cartClass: "in-cart"
}
} else {
return item
}
})
setCart(updatedCart)
}

const removeFromCart = (e) => {
const itemName = e.target.parentElement.childNodes[0].textContent
const updatedCart = cart.map(item => {
if(item.name === itemName){
return {
...item,
cartClass: ""
}
} else {
return item
}
})
setCart(updatedCart)
}

const handleFilter = (e) => {
const category = e.target.value
const filteredItems = items.filter(item => item.category === category)
setCart(filteredItems)
}

return (
<div className="ShoppingList">
<div className="Filter">
<select name="filter">
<option value="All">Filter by category</option>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
</select>
</div>
<Filter filterCart={handleFilter}/>
<ul className="Items">
{items.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
{cart.map((item) => (
<Item
cartClass={item.cartClass}
key={item.id}
name={item.name}
category={item.category}
addItemFun={updateCart}
/>
))}
</ul>
</div>
);
)
}

export default ShoppingList;
export default ShoppingList
2 changes: 1 addition & 1 deletion src/data/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const items = [
{ id: 1, name: "Yogurt", category: "Dairy" },
{ id: 2, name: "Pomegranate", category: "Produce" },
{ id: 3, name: "Lettuce", category: "Produce" },
{ id: 4, name: "String Cheese", category: "Dairy" },
{ id: 4, name: "Milk", category: "Dairy" },
{ id: 5, name: "Cookies", category: "Dessert" },
];

Expand Down