cart-01-block-ex
stackzero/cart-01-block-exFreeExample
stackzero publishes no description for this item.
Install it
npx shadcn@latest add https://ui.stackzero.co/r/cart-01-block-ex.jsonSource
1"use client";23import Cart_01, {4 CartCheckoutPayload,5 CartProduct,6} from "@/components/commerce-ui/cart-01";7import { Button } from "@/components/ui/button";8import { useEffect, useState } from "react";910export default function CartExample_01() {11 // Initial products for the demo12 const initialProducts: CartProduct[] = [13 {14 id: "prod-1",15 imageUrl:16 "https://raw.githubusercontent.com/stackzero-labs/ui/refs/heads/main/public/placeholders/headphone-1.jpg",17 name: "Wireless Headphones",18 price: 98.96,19 quantity: 1,20 },21 {22 id: "prod-2",23 imageUrl:24 "https://raw.githubusercontent.com/stackzero-labs/ui/refs/heads/main/public/placeholders/smartwatch-01.jpg",25 name: "Smart Watch",26 price: 129.99,27 quantity: 2,28 },29 {30 id: "prod-3",31 imageUrl:32 "https://raw.githubusercontent.com/stackzero-labs/ui/refs/heads/main/public/placeholders/speaker-01.jpg",33 name: "Bluetooth Speaker",34 price: 79.95,35 quantity: 1,36 },37 ];3839 // State management40 const [products, setProducts] = useState<CartProduct[]>(initialProducts);41 const [subtotal, setSubtotal] = useState<number>(0);42 const [shippingCost] = useState<number>(15.99);43 const [vatRate] = useState<number>(0.2); // 20% VAT rate44 const [isLoading, setIsLoading] = useState<boolean>(false);45 const [errorMessage, setErrorMessage] = useState<string>("");4647 // Calculate subtotal whenever the products change48 useEffect(() => {49 const newSubtotal = products.reduce(50 (total, product) => total + product.price * product.quantity,51 052 );53 setSubtotal(newSubtotal);54 }, [products]);5556 // Handler to update product quantity57 const handleUpdateQuantity = (productId: string, newQuantity: number) => {58 setProducts(59 products.map((product) =>60 product.id === productId61 ? { ...product, quantity: newQuantity }62 : product63 )64 );65 };6667 // Handler to remove a product68 const handleRemoveProduct = (productId: string) => {69 setProducts(products.filter((product) => product.id !== productId));70 };7172 // Handler for checkout73 const handleCheckout = (payload: CartCheckoutPayload) => {74 const itemCount = payload.products.reduce((sum, p) => sum + p.quantity, 0);75 window.alert(76 `Proceeding to checkout with ${itemCount} items.\nTotal: ${payload.currencyPrefix}${payload.totalAmount.toFixed(2)}77 `78 );79 };8081 // Handler for continue shopping82// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from stackzero
All 112 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| address-02-block-exaddress-02-block-ex | stackzero | Examples | Free | 2 deps | |
| face-rating-basic-ex-01face-rating-basic-ex-01 | stackzero | Examples | Free | 1 dep | |
| face-rating-basic-ex-02face-rating-basic-ex-02 | stackzero | Examples | Free | 1 dep | |
| face-rating-basic-ex-03face-rating-basic-ex-03 | stackzero | Examples | Free | 1 dep | |
| face-rating-gradient-ex-01face-rating-gradient-ex-01 | stackzero | Examples | Free | 1 dep | |
| image-carousel-basic-ex-01image-carousel-basic-ex-01 | stackzero | Examples | Free | 1 dep | |
| image-carousel-basic-ex-02image-carousel-basic-ex-02 | stackzero | Examples | Free | 1 dep | |
| image-carousel-basic-ex-03image-carousel-basic-ex-03 | stackzero | Examples | Free | 1 dep |
