@@ -23,82 +23,127 @@ export async function loader({ request }: Route.LoaderArgs) {
2323export default function Cart ( { loaderData } : Route . ComponentProps ) {
2424 const { cart, total } = loaderData ;
2525
26+ // ✅ AGREGAR: Verificación para carrito vacío
27+ if ( ! cart || ! cart . items || cart . items . length === 0 ) {
28+ return (
29+ < Section >
30+ < Container className = "max-w-xl" >
31+ < h1 className = "text-3xl leading-8 font-bold text-center mb-12" >
32+ Carrito de compras
33+ </ h1 >
34+ < div className = "border-solid border rounded-xl flex flex-col p-6 text-center" >
35+ < p className = "text-muted-foreground mb-4" > Tu carrito está vacío</ p >
36+ < Button size = "lg" className = "w-full" asChild >
37+ < Link to = "/" > Ir a la tienda</ Link >
38+ </ Button >
39+ </ div >
40+ </ Container >
41+ </ Section >
42+ ) ;
43+ }
44+
2645 return (
2746 < Section >
2847 < Container className = "max-w-xl" >
2948 < h1 className = "text-3xl leading-8 font-bold text-center mb-12" >
3049 Carrito de compras
3150 </ h1 >
3251 < div className = "border-solid border rounded-xl flex flex-col" >
33- { cart ?. items ?. map ( ( { product, quantity, id } ) => (
34- < div key = { product . id } className = "flex gap-7 p-6 border-b" >
35- < div className = "w-20 rounded-xl bg-muted" >
36- < img
37- src = { product . imgSrc }
38- alt = { product . alt || product . title }
39- className = "w-full aspect-[2/3] object-contain"
40- />
41- </ div >
42- < div className = "flex grow flex-col justify-between" >
43- < div className = "flex gap-4 justify-between items-center" >
44- < h2 className = "text-sm" > { product . title } </ h2 >
45- < Form method = "post" action = "/cart/remove-item" >
46- < Button
47- size = "sm-icon"
48- variant = "outline"
49- name = "itemId"
50- value = { id }
51- >
52- < Trash2 />
53- </ Button >
54- </ Form >
52+ { cart . items . map (
53+ ( { product, quantity, id, finalPrice, categoryVariant } ) => (
54+ < div
55+ key = { `${ product . id } -${ categoryVariant ?. id || "no-variant" } ` }
56+ className = "flex gap-7 p-6 border-b"
57+ >
58+ < div className = "w-20 rounded-xl bg-muted" >
59+ < img
60+ src = { product . imgSrc }
61+ alt = { product . alt || product . title }
62+ className = "w-full aspect-[2/3] object-contain"
63+ />
5564 </ div >
56- < div className = "flex flex-col gap-2 md:flex-row md:justify-between md:items-center" >
57- < p className = "text-sm font-medium" >
58- ${ product . price . toFixed ( 2 ) }
59- </ p >
60- < div className = "flex gap-4 items-center" >
61- < Form method = "post" action = "/cart/add-item" >
62- < input type = "hidden" name = "quantity" value = "-1" />
65+ < div className = "flex grow flex-col justify-between" >
66+ < div className = "flex gap-4 justify-between items-center" >
67+ < div className = "flex items-center gap-2" >
68+ < h2 className = "text-sm" > { product . title } </ h2 >
69+ { /* ✅ CORREGIDO: Mejor layout para la variante */ }
70+ { categoryVariant && (
71+ < p className = "text-sm" > ({ categoryVariant . label } )</ p >
72+ ) }
73+ </ div >
74+ < Form method = "post" action = "/cart/remove-item" >
6375 < Button
64- name = "productId"
65- value = { product . id }
66- variant = "outline"
6776 size = "sm-icon"
68- disabled = { quantity === 1 }
69- >
70- < Minus />
71- </ Button >
72- </ Form >
73- < span className = "h-8 w-8 flex justify-center items-center border rounded-md py-2 px-4" >
74- { quantity }
75- </ span >
76- < Form method = "post" action = "/cart/add-item" >
77- < Button
7877 variant = "outline"
79- size = "sm-icon "
80- name = "productId"
81- value = { product . id }
78+ name = "itemId "
79+ value = { id }
80+ type = "submit"
8281 >
83- < Plus />
82+ < Trash2 />
8483 </ Button >
8584 </ Form >
8685 </ div >
86+ < div className = "flex flex-col gap-2 md:flex-row md:justify-between md:items-center" >
87+ < p className = "text-sm font-medium" >
88+ S/{ finalPrice . toFixed ( 2 ) }
89+ </ p >
90+ < div className = "flex gap-4 items-center" >
91+ < Form method = "post" action = "/cart/add-item" >
92+ < input type = "hidden" name = "quantity" value = "-1" />
93+ < input
94+ type = "hidden"
95+ name = "productId"
96+ value = { product . id }
97+ />
98+ { categoryVariant && (
99+ < input
100+ type = "hidden"
101+ name = "categoryVariantId"
102+ value = { categoryVariant . id }
103+ />
104+ ) }
105+ < Button
106+ type = "submit"
107+ variant = "outline"
108+ size = "sm-icon"
109+ disabled = { quantity === 1 }
110+ >
111+ < Minus />
112+ </ Button >
113+ </ Form >
114+ < span className = "h-8 w-8 flex justify-center items-center border rounded-md py-2 px-4" >
115+ { quantity }
116+ </ span >
117+ < Form method = "post" action = "/cart/add-item" >
118+ < input
119+ type = "hidden"
120+ name = "productId"
121+ value = { product . id }
122+ />
123+ { categoryVariant && (
124+ < input
125+ type = "hidden"
126+ name = "categoryVariantId"
127+ value = { categoryVariant . id }
128+ />
129+ ) }
130+ < Button type = "submit" variant = "outline" size = "sm-icon" >
131+ < Plus />
132+ </ Button >
133+ </ Form >
134+ </ div >
135+ </ div >
87136 </ div >
88137 </ div >
89- </ div >
90- ) ) }
138+ )
139+ ) }
91140 < div className = "flex justify-between p-6 text-base font-medium border-b" >
92141 < p > Total</ p >
93142 < p > S/{ total . toFixed ( 2 ) } </ p >
94143 </ div >
95144 < div className = "p-6" >
96145 < Button size = "lg" className = "w-full" asChild >
97- { cart ?. items && cart . items . length > 0 ? (
98- < Link to = "/checkout" > Continuar Compra</ Link >
99- ) : (
100- < Link to = "/" > Ir a la tienda</ Link >
101- ) }
146+ < Link to = "/checkout" > Continuar Compra</ Link >
102147 </ Button >
103148 </ div >
104149 </ div >
0 commit comments