Skip to content
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

Update e fixs #158

Open
wants to merge 3 commits into
base: main
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
48 changes: 48 additions & 0 deletions dist/assets/index-3TtnXBFW.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/assets/index-DoTl-FFb.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/shoppingreact/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>E-Plant</title>
<meta name="keywords" content="Vite, React, plants, gardening, Air Purifying Plants, Aromatic Fragrant Plants, Insect Repellent Plants, Medicinal Plants, Low Maintenance Plants" /> <!-- Add meta keywords here -->

<script type="module" crossorigin src="/shoppingreact/assets/index-3TtnXBFW.js"></script>
<link rel="stylesheet" crossorigin href="/shoppingreact/assets/index-DoTl-FFb.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
1 change: 1 addition & 0 deletions dist/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 112 additions & 49 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,128 @@ import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';

const CartItem = ({ onContinueShopping }) => {
const cart = useSelector(state => state.cart.items);
const dispatch = useDispatch();
const cart = useSelector(state => state.cart.items);
const dispatch = useDispatch();

// Calculate total amount for all products in the cart
const calculateTotalAmount = () => {

};
// Calcula o valor total de todos os itens no carrinho
const calculateTotalAmount = () => {
return cart.reduce((total, item) => {
// Remove o símbolo '$' e converte para número
const cost = parseFloat(item.cost.replace('$', ''));
return total + (cost * item.quantity);
}, 0).toFixed(2); // Mantém 2 casas decimais
};

const handleContinueShopping = (e) => {

};
// Manipula o botão "Continuar Comprando"
const handleContinueShopping = (e) => {
e.preventDefault();
onContinueShopping(); // Chama a função passada como prop
};

// Manipula o botão de checkout
const handleCheckoutShopping = () => {
alert('Functionality to be added for future reference');
};

// Incrementa a quantidade de um item
const handleIncrement = (item) => {
dispatch(updateQuantity({
name: item.name,
quantity: item.quantity + 1
}));
};

const handleIncrement = (item) => {
};
// Decrementa a quantidade de um item
const handleDecrement = (item) => {
if (item.quantity === 1) {
// Remove o item se a quantidade chegar a zero
dispatch(removeItem(item.name));
} else {
dispatch(updateQuantity({
name: item.name,
quantity: item.quantity - 1
}));
}
};

const handleDecrement = (item) => {

};
// Remove um item do carrinho
const handleRemove = (item) => {
dispatch(removeItem(item.name));
};

const handleRemove = (item) => {
};
// Calcula o custo total de um item específico
const calculateTotalCost = (item) => {
const cost = parseFloat(item.cost.replace('$', ''));
return (cost * item.quantity).toFixed(2);
};

// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
};
// Calcula a quantidade total de itens no carrinho
const calculateTotalItems = () => {
return cart.reduce((total, item) => total + item.quantity, 0);
};

return (
<div className="cart-container">
<h2 style={{ color: 'black' }}>Total Cart Amount: ${calculateTotalAmount()}</h2>
<div>
{cart.map(item => (
<div className="cart-item" key={item.name}>
<img className="cart-item-image" src={item.image} alt={item.name} />
<div className="cart-item-details">
<div className="cart-item-name">{item.name}</div>
<div className="cart-item-cost">{item.cost}</div>
<div className="cart-item-quantity">
<button className="cart-item-button cart-item-button-dec" onClick={() => handleDecrement(item)}>-</button>
<span className="cart-item-quantity-value">{item.quantity}</span>
<button className="cart-item-button cart-item-button-inc" onClick={() => handleIncrement(item)}>+</button>
</div>
<div className="cart-item-total">Total: ${calculateTotalCost(item)}</div>
<button className="cart-item-delete" onClick={() => handleRemove(item)}>Delete</button>
return (
<div className="cart-container">
<h2 style={{ color: 'black' }}>
Total Cart Amount: ${calculateTotalAmount()}
<span className="total-items">({calculateTotalItems()} items)</span>
</h2>

<div>
{cart.map(item => (
<div className="cart-item" key={item.name}>
<img className="cart-item-image" src={item.image} alt={item.name} />
<div className="cart-item-details">
<div className="cart-item-name">{item.name}</div>
<div className="cart-item-cost">{item.cost} per unit</div>
<div className="cart-item-quantity">
<button
className="cart-item-button cart-item-button-dec"
onClick={() => handleDecrement(item)}
>
-
</button>
<span className="cart-item-quantity-value">{item.quantity}</span>
<button
className="cart-item-button cart-item-button-inc"
onClick={() => handleIncrement(item)}
>
+
</button>
</div>
<div className="cart-item-total">Total: ${calculateTotalCost(item)}</div>
<button
className="cart-item-delete"
onClick={() => handleRemove(item)}
>
Delete
</button>
</div>
</div>
))}
</div>
</div>
))}
</div>
<div style={{ marginTop: '20px', color: 'black' }} className='total_cart_amount'></div>
<div className="continue_shopping_btn">
<button className="get-started-button" onClick={(e) => handleContinueShopping(e)}>Continue Shopping</button>
<br />
<button className="get-started-button1">Checkout</button>
</div>
</div>
);
};

export default CartItem;
<div style={{ marginTop: '20px', color: 'black' }} className='total_cart_amount'>
Total Items: {calculateTotalItems()}
</div>

<div className="continue_shopping_btn">
<button
className="get-started-button"
onClick={handleContinueShopping}
>
Continue Shopping
</button>
<br />
<button
className="get-started-button1"
onClick={handleCheckoutShopping}
>
Checkout
</button>
</div>
</div>
);
};

export default CartItem;
16 changes: 14 additions & 2 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ export const CartSlice = createSlice({
},
reducers: {
addItem: (state, action) => {

const { name, image, cost } = action.payload;
const existingItem = state.items.find(item => item.name === name);
if (existingItem) {
existingItem.quantity++;
} else {
state.items.push({ name, image, cost, quantity: 1 });
}
},
removeItem: (state, action) => {
state.items = state.items.filter(item => item.name !== action.payload);

},
updateQuantity: (state, action) => {


const { name, quantity } = action.payload;
const itemToUpdate = state.items.find(item => item.name === name);
if (itemToUpdate) {
itemToUpdate.quantity = quantity;
}
},
},
});
Expand Down
Loading