Fix Mathu Coding und Payment fix

This commit is contained in:
FlorianSpeicher
2025-06-11 13:30:36 +02:00
parent 4b3d280789
commit ab72054820
7 changed files with 199 additions and 138 deletions

View File

@@ -1,13 +1,14 @@
import React, { createContext, useContext, useState } from 'react';
import Item from '../components/Item';
interface BasketItem {
itemId: string;
item: Item;
quantity: number;
}
interface BasketContextType {
basket: BasketItem[];
addToBasket: (itemId: string, quantity: number) => void;
addToBasket: (item: Item, quantity: number) => void;
clearBasket: () => void;
}
@@ -16,19 +17,19 @@ const BasketContext = createContext<BasketContextType | undefined>(undefined);
export const BasketProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [basket, setBasket] = useState<BasketItem[]>([]);
const addToBasket = (itemId: string, quantity: number) => {
const addToBasket = (item: Item, quantity: number) => {
setBasket((prevBasket) => {
const existingItem = prevBasket.find((item) => item.itemId === itemId);
const existingItem = prevBasket.find((basketItem) => basketItem.item.uuid === item.uuid);
if (existingItem) {
// Update quantity if item already exists
return prevBasket.map((item) =>
item.itemId === itemId
? { ...item, quantity: item.quantity + quantity }
: item
return prevBasket.map((basketItem) =>
basketItem.item.uuid === item.uuid
? { ...basketItem, quantity: basketItem.quantity + quantity }
: basketItem
);
}
// Add new item to basket
return [...prevBasket, { itemId, quantity }];
return [...prevBasket, { item, quantity }];
});
};

View File

@@ -14,7 +14,7 @@ export default function ItemCard({ item }: { item: Item }) {
const { addToBasket } = useBasket();
const handleAddToCart = () => {
addToBasket(item.id, 1);
addToBasket(item, 1);
console.log(`Added ${1} of ${item.name} to basket`);
};

View File

@@ -0,0 +1,18 @@
export default function LoginPopUp() {
return (
<div className="login-popup">
<h2>Login</h2>
<form>
<div className="form-group">
<label htmlFor="username">Username</label>
<input type="text" id="username" name="username" required />
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input type="password" id="password" name="password" required />
</div>
<button type="submit">Login</button>
</form>
</div>
);
}

View File

@@ -41,7 +41,7 @@ export default function ProductInfo({ item }: { item: Item }) {
const handleAddToCart = () => {
addToBasket(item.id, quantity);
addToBasket(item, quantity);
setOpen(true);
console.log(`Added {quantity} of €{item.name} to basket`);
};