@@ -6,38 +6,53 @@ import CartItem from '@/components/CartItem'
66import { formatPrice } from '@/lib/utils'
77
88export default function CartPage ( ) {
9- const [ cart , setCart ] = useState < any > ( null )
9+ const [ cart , setCart ] = useState < any | null > ( null )
10+ const [ error , setError ] = useState < string | null > ( null )
1011
1112 async function load ( ) {
12- const data = await getCart ( )
13- setCart ( data )
13+ try {
14+ const data = await getCart ( )
15+ setCart ( data )
16+ } catch ( err ) {
17+ console . error ( err )
18+ setError ( 'Failed to load cart' )
19+ setCart ( { items : [ ] , subtotal : 0 } )
20+ }
1421 }
1522
1623 useEffect ( ( ) => {
1724 load ( )
1825 } , [ ] )
1926
20- if ( ! cart ) return < div > Loading…</ div >
27+ if ( error ) {
28+ return < div className = "p-4 text-red-600" > { error } </ div >
29+ }
30+
31+ if ( ! cart ) {
32+ return < div className = "p-4" > Loading…</ div >
33+ }
34+
35+ const items = cart . items ?? [ ]
2136
2237 return (
2338 < div className = "max-w-3xl mx-auto p-4" >
2439 < h1 className = "text-xl font-semibold mb-4" > Your Cart</ h1 >
2540
26- { cart . items . length === 0 && < p > Your cart is empty</ p > }
41+ { items . length === 0 && < p > Your cart is empty</ p > }
2742
28- { cart . items . map ( ( item : any ) => (
43+ { items . map ( ( item : any ) => (
2944 < CartItem key = { item . variant . id } item = { item } onRefresh = { load } />
3045 ) ) }
3146
3247 < div className = "flex justify-between mt-6 text-lg font-semibold" >
3348 < span > Subtotal</ span >
34- < span > { formatPrice ( cart . Subtotal ) } </ span >
49+ < span > { formatPrice ( cart . subtotal ?? 0 ) } </ span >
3550 </ div >
3651
3752 < button
38- disabled = { cart . items . length === 0 }
53+ disabled = { items . length === 0 }
3954 className = "mt-6 w-full bg-black text-white py-3 rounded disabled:opacity-50"
40- onClick = { ( ) => location . href = '/checkout' }
55+ onClick = { ( ) => ( location . href = '/checkout' ) }
4156 >
4257 Proceed to Checkout
4358 </ button >
0 commit comments