1- import { Form , useNavigation } from "react-router" ;
21import { useState } from "react" ;
2+ import { Form , useNavigation } from "react-router" ;
3+
34import { Button , Container , Separator } from "@/components/ui" ;
45import { type Product } from "@/models/product.model" ;
56import { getProductById } from "@/services/product.service" ;
7+
68import NotFound from "../not-found" ;
9+
710import type { Route } from "./+types" ;
811
912export async function loader ( { params } : Route . LoaderArgs ) {
@@ -26,25 +29,36 @@ export default function Product({ loaderData }: Route.ComponentProps) {
2629 }
2730
2831 const showSizeSelector = product . categoryId === 1 || product . categoryId === 3 ;
29-
32+
33+ const getAttributeValueId = ( ) => { // AQUI TRAER EL AttributeValueId con el cambio de SEBAS
34+ if (
35+ ! product . variantAttributeValues ||
36+ product . variantAttributeValues . length === 0
37+ ) {
38+ return undefined ;
39+ }
40+ // Devuelve el attributeId de la posición 0
41+ return product . variantAttributeValues [ 0 ] . id ;
42+ } ;
43+
3044 const getSizeOptions = ( ) => {
3145 if ( product . categoryId === 3 ) {
3246 return {
3347 label : "Dimensiones" ,
3448 options : [
3549 { value : "Small" , label : "3x3 cm" } ,
3650 { value : "Medium" , label : "5x5 cm" } ,
37- { value : "Large" , label : "10x10 cm" }
38- ]
51+ { value : "Large" , label : "10x10 cm" } ,
52+ ] ,
3953 } ;
4054 } else {
4155 return {
4256 label : "Talla" ,
4357 options : [
4458 { value : "Small" , label : "Small" } ,
4559 { value : "Medium" , label : "Medium" } ,
46- { value : "Large" , label : "Large" }
47- ]
60+ { value : "Large" , label : "Large" } ,
61+ ] ,
4862 } ;
4963 }
5064 } ;
@@ -67,7 +81,14 @@ export default function Product({ loaderData }: Route.ComponentProps) {
6781 { product . title }
6882 { showSizeSelector && (
6983 < span className = "text-muted-foreground" >
70- { " " } ({ sizeOptions . options . find ( option => option . value === selectedSize ) ?. label } )
84+ { " " }
85+ (
86+ {
87+ sizeOptions . options . find (
88+ ( option ) => option . value === selectedSize
89+ ) ?. label
90+ }
91+ )
7192 </ span >
7293 ) }
7394 </ h1 >
@@ -78,12 +99,16 @@ export default function Product({ loaderData }: Route.ComponentProps) {
7899
79100 { showSizeSelector && (
80101 < div className = "mb-9" >
81- < p className = "text-sm font-semibold text-accent-foreground mb-2" > { sizeOptions . label } </ p >
102+ < p className = "text-sm font-semibold text-accent-foreground mb-2" >
103+ { sizeOptions . label }
104+ </ p >
82105 < div className = "flex gap-2" >
83106 { sizeOptions . options . map ( ( option ) => (
84107 < Button
85108 key = { option . value }
86- variant = { selectedSize === option . value ? "default" : "secondary" }
109+ variant = {
110+ selectedSize === option . value ? "default" : "secondary"
111+ }
87112 size = "lg"
88113 onClick = { ( ) => setSelectedSize ( option . value ) }
89114 >
@@ -100,20 +125,25 @@ export default function Product({ loaderData }: Route.ComponentProps) {
100125 name = "redirectTo"
101126 value = { `/products/${ product . id } ` }
102127 />
128+ < input
129+ type = "hidden"
130+ name = "attributeValueId"
131+ value = { getAttributeValueId ( ) ?? "" }
132+ />
103133 < Button
104134 size = "xl"
105135 className = "w-full md:w-80"
106136 type = "submit"
107- name = "productId"
108- value = { product . id }
137+ // name="productId"
138+ // value={product.id}
109139 disabled = { cartLoading }
110140 >
111141 { cartLoading ? "Agregando..." : "Agregar al Carrito" }
112142 </ Button >
113143 </ Form >
114-
144+
115145 < Separator className = "my-6" />
116-
146+
117147 < div >
118148 < h2 className = "text-sm font-semibold text-accent-foreground mb-6" >
119149 Características
@@ -129,4 +159,4 @@ export default function Product({ loaderData }: Route.ComponentProps) {
129159 </ section >
130160 </ >
131161 ) ;
132- }
162+ }
0 commit comments