From e704383cb20e7016794ccc793eda057a609be835 Mon Sep 17 00:00:00 2001 From: ertopogo Date: Sun, 1 Feb 2026 02:49:28 +0100 Subject: ajout de cart pour les commandes --- storefront/pages/cart.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 storefront/pages/cart.js (limited to 'storefront/pages/cart.js') diff --git a/storefront/pages/cart.js b/storefront/pages/cart.js new file mode 100644 index 0000000..fa625a9 --- /dev/null +++ b/storefront/pages/cart.js @@ -0,0 +1,101 @@ +import { useCallback, useEffect, useState } from "react" +import { medusaClient } from "../lib/medusa-client" +import { formatAmount } from "../lib/format" +import { getStoredCartId, clearStoredCartId } from "../lib/storefront" + +export default function CartPage() { + const [cart, setCart] = useState(null) + const [status, setStatus] = useState("") + const [isLoading, setIsLoading] = useState(true) + + const loadCart = useCallback(async () => { + const storedCartId = getStoredCartId() + if (!storedCartId) { + setCart(null) + setIsLoading(false) + return + } + + try { + const { cart: fetchedCart } = await medusaClient.carts.retrieve(storedCartId) + setCart(fetchedCart) + } catch (error) { + clearStoredCartId() + setCart(null) + } finally { + setIsLoading(false) + } + }, []) + + useEffect(() => { + loadCart() + }, [loadCart]) + + const handleRemove = async (lineItemId) => { + if (!cart) { + return + } + setStatus("") + try { + await medusaClient.carts.lineItems.delete(cart.id, lineItemId) + await loadCart() + } catch (error) { + setStatus("Impossible de retirer l'article.") + } + } + + if (isLoading) { + return

Chargement du panier...

+ } + + if (!cart || !cart.items?.length) { + return

Votre panier est vide.

+ } + + return ( +
+

Panier

+ {status &&

{status}

} +
+ {cart.items.map((item) => ( +
+
+ {item.title} +

Quantité : {item.quantity}

+

+ {formatAmount(item.unit_price, cart.region?.currency_code || "eur")} +

+
+ +
+ ))} +
+

+ Total : {formatAmount(cart.total, cart.region?.currency_code || "eur")} +

+
+ ) +} -- cgit v1.2.3