From 769ad2d2bee3092485ba1ac1ac5f28bd71e1b823 Mon Sep 17 00:00:00 2001 From: nicktnc24 Date: Thu, 5 Dec 2024 17:30:15 -0600 Subject: [PATCH] fixed sizing and edge cases --- src/app/_components/shoppingCart.tsx | 9 ++++++--- src/app/_components/shoppingData.tsx | 3 ++- src/app/customerView/page.tsx | 8 ++++---- src/app/entrees/page.tsx | 4 ++++ src/app/layout.tsx | 1 + src/app/sides/page.tsx | 8 ++++++-- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/app/_components/shoppingCart.tsx b/src/app/_components/shoppingCart.tsx index 8e3cc26..67530b7 100644 --- a/src/app/_components/shoppingCart.tsx +++ b/src/app/_components/shoppingCart.tsx @@ -27,7 +27,7 @@ export default function ShoppingCart() { function removeAllItems(): void { // setCurrentItems([]); - setShoppingData({size: -1, currentPrice: 0, numEntrees: 0, cartItems: []}); + setShoppingData({size: -1, currentPrice: 0, numEntrees: 0, cartItems: [], numSides: 0}); } const [cart, showCart] = useState(false); @@ -85,6 +85,9 @@ export default function ShoppingCart() { else if(shoppingData.cartItems[index].includes("/l") && shoppingData.cartItems[index].includes("/s")){ setShoppingData({...shoppingData, currentPrice: shoppingData.currentPrice - 4, cartItems: [...shoppingData.cartItems.slice(0, index), ...shoppingData.cartItems.slice(index + 1)]}) } + else if(shoppingData.cartItems[index].includes("/s")){ + setShoppingData({...shoppingData, numSides: shoppingData.numSides + 1, cartItems: [...shoppingData.cartItems.slice(0, index), ...shoppingData.cartItems.slice(index + 1)]}) + } else if(shoppingData.cartItems[index].includes("/p")){ setShoppingData({...shoppingData, currentPrice: shoppingData.currentPrice - 1.5, cartItems: [...shoppingData.cartItems.slice(0, index), ...shoppingData.cartItems.slice(index + 1)]}) @@ -147,7 +150,7 @@ export default function ShoppingCart() { } catch (error) { console.error('Error during checkout:', error); } - setShoppingData({size: -1, currentPrice: 0, numEntrees: 0, cartItems: []}); + removeAllItems(); console.log("Checkout successful!"); } @@ -211,7 +214,7 @@ export default function ShoppingCart() {
Your Items:
-
{listCartItems()}
+
{listCartItems()}
Price:
diff --git a/src/app/_components/shoppingData.tsx b/src/app/_components/shoppingData.tsx index d049fbd..5b6c040 100644 --- a/src/app/_components/shoppingData.tsx +++ b/src/app/_components/shoppingData.tsx @@ -12,10 +12,11 @@ export interface shoppingDataInterface { currentPrice: number; cartItems: string[]; size: number; + numSides: number; } -export const shoppingDataContext = createContext<[shoppingDataInterface, Dispatch>]>([{numEntrees: 0, currentPrice: 0, cartItems: [], size: -1}, () => {}]); +export const shoppingDataContext = createContext<[shoppingDataInterface, Dispatch>]>([{numEntrees: 0, currentPrice: 0, cartItems: [], size: -1, numSides: -0}, () => {}]); export function useShoppingDataContext(){ const data = useContext(shoppingDataContext); diff --git a/src/app/customerView/page.tsx b/src/app/customerView/page.tsx index 7b6a82a..e1540c0 100644 --- a/src/app/customerView/page.tsx +++ b/src/app/customerView/page.tsx @@ -56,16 +56,16 @@ export default function CustomerView() {
{ if (size.size_id === 0){ - changeShoppingData({...shoppingData, numEntrees: 1, currentPrice: size.price, size: size.size_id}); + changeShoppingData({...shoppingData, numEntrees: 1, currentPrice: size.price, size: size.size_id, numSides: 1}); } else if (size.size_id === 1){ - changeShoppingData({...shoppingData, numEntrees: 2, currentPrice: size.price, size: size.size_id}); + changeShoppingData({...shoppingData, numEntrees: 2, currentPrice: size.price, size: size.size_id, numSides: 1}); } else if (size.size_id === 2){ - changeShoppingData({...shoppingData, numEntrees: 3, currentPrice: size.price, size: size.size_id}); + changeShoppingData({...shoppingData, numEntrees: 3, currentPrice: size.price, size: size.size_id, numSides: 1}); } else if (size.size_id === 3){ - changeShoppingData({...shoppingData, numEntrees: 0, currentPrice: 0, size: size.size_id}); + changeShoppingData({...shoppingData, numEntrees: 10, currentPrice: 0, size: size.size_id, numSides: 10}); } }} href = "/sides"> {/* sizes */} diff --git a/src/app/entrees/page.tsx b/src/app/entrees/page.tsx index aa363e7..38ca941 100644 --- a/src/app/entrees/page.tsx +++ b/src/app/entrees/page.tsx @@ -56,6 +56,10 @@ export default function Entrees() { }; const handleAddToCart = () => { + if (shoppingData.numEntrees === 0){ + alert('You cannot add anymore entrees to the cart'); + return; + } if (typeof window !== 'undefined' && selectedEntree) { const selectedEntreeItem = entrees.find(entree => entree.food_id === selectedEntree); if (selectedEntreeItem?.premium && shoppingData.size === 3){ diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 0105a45..fce80c8 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -20,6 +20,7 @@ export default function RootLayout({ currentPrice: 0.0, cartItems: [], size: -1, + numSides: 0 }); return ( diff --git a/src/app/sides/page.tsx b/src/app/sides/page.tsx index d406883..f3cdcdb 100644 --- a/src/app/sides/page.tsx +++ b/src/app/sides/page.tsx @@ -42,17 +42,21 @@ export default function Sides() { }; const handleAddToCart = () => { + if (shoppingCart.numSides === 0) { + alert('You cannot add anymore sides to the cart'); + return; + } if (typeof window !== 'undefined') { let selectedSideItem = sides.find(side => side.food_id === selectedSide); if (selectedSide === 101){ selectedSideItem = sides.find(side => side.food_id === 0); } if (selectedSideItem && shoppingCart.size === 3){ - setShoppingCart({...shoppingCart, cartItems: [...shoppingCart.cartItems, selectedSideItem.food_name + "/l/s"], currentPrice: shoppingCart.currentPrice + 4}); + setShoppingCart({...shoppingCart, numSides: shoppingCart.numSides - 1, cartItems: [...shoppingCart.cartItems, selectedSideItem.food_name + "/l/s"], currentPrice: shoppingCart.currentPrice + 4}); } else if (selectedSideItem) { // sessionStorage.setItem('newItem', JSON.stringify(selectedSideItem.food_name)); - setShoppingCart({...shoppingCart, cartItems: [...shoppingCart.cartItems, selectedSideItem.food_name]}); + setShoppingCart({...shoppingCart, numSides: shoppingCart.numSides - 1,cartItems: [...shoppingCart.cartItems, selectedSideItem.food_name + "/s"]}); } setSelectedSide(null); }