Skip to content

Commit 3c6a97f

Browse files
Implement checkout process
Implement the checkout process to complete a purchase without payment.
1 parent a3d76cf commit 3c6a97f

File tree

6 files changed

+324
-6
lines changed

6 files changed

+324
-6
lines changed

package-lock.json

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"preview": "vite preview"
1212
},
1313
"dependencies": {
14-
"@hookform/resolvers": "^3.9.0",
14+
"@hookform/resolvers": "^4.1.3",
1515
"@radix-ui/react-accordion": "^1.2.0",
1616
"@radix-ui/react-alert-dialog": "^1.1.1",
1717
"@radix-ui/react-aspect-ratio": "^1.1.0",

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { BrowserRouter, Routes, Route } from "react-router-dom";
88
import Index from "./pages/Index";
99
import Products from "./pages/Products";
1010
import ProductDetail from "./pages/ProductDetail";
11+
import Checkout from "./pages/Checkout";
1112
import NotFound from "./pages/NotFound";
1213
import { CartProvider } from "@/context/CartContext";
1314

@@ -27,6 +28,7 @@ const App = () => (
2728
<Route path="/" element={<Index />} />
2829
<Route path="/products" element={<Products />} />
2930
<Route path="/products/:id" element={<ProductDetail />} />
31+
<Route path="/checkout" element={<Checkout />} />
3032
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
3133
<Route path="*" element={<NotFound />} />
3234
</Routes>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
3+
import { Product } from "@/lib/products";
4+
5+
interface OrderSummaryProps {
6+
items: Array<{
7+
product: Product;
8+
quantity: number;
9+
}>;
10+
totalPrice: number;
11+
}
12+
13+
const OrderSummary = ({ items, totalPrice }: OrderSummaryProps) => {
14+
const shippingCost = 0; // Free shipping
15+
const taxRate = 0.08; // 8% tax
16+
const taxAmount = totalPrice * taxRate;
17+
const orderTotal = totalPrice + taxAmount + shippingCost;
18+
19+
return (
20+
<Card>
21+
<CardHeader>
22+
<CardTitle className="text-xl">Order Summary</CardTitle>
23+
</CardHeader>
24+
<CardContent>
25+
<div className="space-y-4">
26+
{/* Items */}
27+
<div className="space-y-2">
28+
{items.map((item) => (
29+
<div key={item.product.id} className="flex justify-between text-sm">
30+
<span>
31+
{item.product.name} x {item.quantity}
32+
</span>
33+
<span className="font-medium">
34+
${(item.product.price * item.quantity).toFixed(2)}
35+
</span>
36+
</div>
37+
))}
38+
</div>
39+
40+
{/* Divider */}
41+
<div className="border-t border-gray-200 my-4"></div>
42+
43+
{/* Subtotal */}
44+
<div className="flex justify-between">
45+
<span>Subtotal</span>
46+
<span className="font-medium">${totalPrice.toFixed(2)}</span>
47+
</div>
48+
49+
{/* Tax */}
50+
<div className="flex justify-between text-sm text-gray-600">
51+
<span>Tax (8%)</span>
52+
<span>${taxAmount.toFixed(2)}</span>
53+
</div>
54+
55+
{/* Shipping */}
56+
<div className="flex justify-between text-sm text-gray-600">
57+
<span>Shipping</span>
58+
<span>{shippingCost === 0 ? "Free" : `$${shippingCost.toFixed(2)}`}</span>
59+
</div>
60+
61+
{/* Divider */}
62+
<div className="border-t border-gray-200 my-4"></div>
63+
64+
{/* Total */}
65+
<div className="flex justify-between font-medium text-lg">
66+
<span>Total</span>
67+
<span>${orderTotal.toFixed(2)}</span>
68+
</div>
69+
</div>
70+
</CardContent>
71+
</Card>
72+
);
73+
};
74+
75+
export default OrderSummary;

src/pages/Cart.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import { useEffect } from "react";
3+
import { useNavigate } from "react-router-dom";
34
import { X, ShoppingBag, ArrowRight } from "lucide-react";
45
import { Button } from "@/components/ui/button";
56
import CartItem from "@/components/ui/CartItem";
@@ -14,6 +15,7 @@ const Cart = () => {
1415
totalPrice,
1516
clearCart
1617
} = useCart();
18+
const navigate = useNavigate();
1719

1820
// Prevent body scrolling when cart is open
1921
useEffect(() => {
@@ -28,6 +30,11 @@ const Cart = () => {
2830
};
2931
}, [isCartOpen]);
3032

33+
const handleCheckout = () => {
34+
toggleCart(); // Close the cart
35+
navigate("/checkout"); // Navigate to checkout page
36+
};
37+
3138
return (
3239
<>
3340
{/* Cart Sidebar */}
@@ -92,7 +99,7 @@ const Cart = () => {
9299
<p className="text-gray-500 text-sm mb-4">
93100
Shipping and taxes calculated at checkout
94101
</p>
95-
<Button className="w-full mb-2">
102+
<Button className="w-full mb-2" onClick={handleCheckout}>
96103
Checkout
97104
<ArrowRight className="ml-2 h-4 w-4" />
98105
</Button>

0 commit comments

Comments
 (0)