Skip to content

Commit c4c9ed6

Browse files
committed
feat(order item): agregamos la logica para mostrar la variante a nivel de la grilla de detalle de ordenes, asi como el precio con la variante, el total, se logra crear la orden con los order items, asociados para usuarios visitantes y existentes.
1 parent 8a245d2 commit c4c9ed6

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

prisma/initial_data.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ export const categoryVariants = [
4848
// Variantes para Stickers (categoryId: 3) - con modificador de precio
4949
{
5050
categoryId: 3,
51-
value: "3x3 cm",
52-
label: "3×3",
51+
value: "3x3",
52+
label: "3×3 cm",
5353
priceModifier: 0,
5454
sortOrder: 1,
5555
},
5656
{
5757
categoryId: 3,
58-
value: "5x5 cm",
59-
label: "5×5",
58+
value: "5x5",
59+
label: "5×5 cm",
6060
priceModifier: 1.0,
6161
sortOrder: 2,
6262
},
6363
{
6464
categoryId: 3,
65-
value: "10x10 cm",
66-
label: "10×10",
65+
value: "10x10",
66+
label: "10×10 cm",
6767
priceModifier: 3.0,
6868
sortOrder: 3,
6969
},

src/models/cart.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export type CartItem = {
3232
export interface CartItemInput {
3333
productId: Product["id"];
3434
quantity: number;
35+
categoryVariantId: number | null;
36+
variantInfo: string | null;
3537
title: Product["title"];
3638
price: Product["price"];
3739
imgSrc: Product["imgSrc"];

src/models/order.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export type Order = Omit<PrismaOrder, "totalAmount"> & {
2929

3030
export interface OrderItemInput {
3131
productId: number;
32+
categoryVariantId?: number | null;
3233
quantity: number;
3334
title: string;
35+
variantInfo?: string | null; // ← NUEVO
3436
price: number;
3537
imgSrc: string;
3638
}

src/routes/checkout/index.tsx

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ export async function action({ request }: Route.ActionArgs) {
105105

106106
const items = cartItems.map((item) => ({
107107
productId: item.product.id,
108+
categoryVariantId: item.categoryVariantId, // ← NUEVO
108109
quantity: item.quantity,
109110
title: item.product.title,
110-
price: item.product.price,
111+
variantInfo: item.categoryVariant
112+
? getVariantInfoText(item.categoryVariantId, item.categoryVariant)
113+
: null,
114+
price: item.finalPrice,
111115
imgSrc: item.product.imgSrc,
112116
}));
113117

@@ -130,6 +134,15 @@ export async function action({ request }: Route.ActionArgs) {
130134
});
131135
}
132136

137+
function getVariantInfoText(
138+
categoryId: number | null,
139+
categoryVariant: any
140+
): string {
141+
if (categoryId === 1) return `Talla: ${categoryVariant.label}`;
142+
if (categoryId === 3) return `Tamaño: ${categoryVariant.label}`;
143+
return `Opción: ${categoryVariant.label}`;
144+
}
145+
133146
export async function loader({ request }: Route.LoaderArgs) {
134147
const session = await getSession(request.headers.get("Cookie"));
135148
const sessionCartId = session.get("sessionCartId");
@@ -249,28 +262,37 @@ export default function Checkout({
249262
<div className="flex-grow">
250263
<h2 className="text-lg font-medium mb-4">Resumen de la orden</h2>
251264
<div className="border border-border rounded-xl bg-background flex flex-col">
252-
{cart?.items?.map(({ product, quantity }) => (
253-
<div
254-
key={product.id}
255-
className="flex gap-6 p-6 border-b border-border"
256-
>
257-
<div className="w-20 rounded-xl bg-muted">
258-
<img
259-
src={product.imgSrc}
260-
alt={product.title}
261-
className="w-full aspect-square object-contain"
262-
/>
263-
</div>
264-
<div className="flex flex-col justify-between flex-grow">
265-
<h3 className="text-sm leading-5">{product.title}</h3>
266-
<div className="flex text-sm font-medium gap-4 items-center self-end">
267-
<p>{quantity}</p>
268-
<X className="w-4 h-4" />
269-
<p>S/{product.price.toFixed(2)}</p>
265+
{cart?.items?.map(
266+
({ product, quantity, finalPrice, categoryVariant }) => (
267+
<div
268+
key={`${product.id}-${categoryVariant?.id}`}
269+
className="flex gap-6 p-6 border-b border-border"
270+
>
271+
<div className="w-20 rounded-xl bg-muted">
272+
<img
273+
src={product.imgSrc}
274+
alt={product.title}
275+
className="w-full aspect-square object-contain"
276+
/>
277+
</div>
278+
<div className="flex flex-col justify-between flex-grow">
279+
<div className="flex items-center">
280+
<h3 className="text-sm leading-5">{product.title}</h3>
281+
{categoryVariant && (
282+
<p className="text-sm leading-5">
283+
({categoryVariant.label})
284+
</p>
285+
)}
286+
</div>
287+
<div className="flex text-sm font-medium gap-4 items-center self-end">
288+
<p>{quantity}</p>
289+
<X className="w-4 h-4" />
290+
<p>S/{finalPrice.toFixed(2)}</p>
291+
</div>
270292
</div>
271293
</div>
272-
</div>
273-
))}
294+
)
295+
)}
274296
<div className="flex justify-between p-6 text-base font-medium">
275297
<p>Total</p>
276298
<p>S/{total.toFixed(2)}</p>

src/services/order.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export async function createOrder(
2626
items: {
2727
create: items.map((item) => ({
2828
productId: item.productId,
29+
categoryVariantId: item.categoryVariantId,
2930
quantity: item.quantity,
3031
title: item.title,
32+
variantInfo: item.variantInfo,
3133
price: item.price,
3234
imgSrc: item.imgSrc,
3335
})),

0 commit comments

Comments
 (0)