From 2664a39c3a042f0a987705ffa2ed28b0d5f8666a Mon Sep 17 00:00:00 2001 From: Claydson Fernando Date: Wed, 27 Nov 2024 00:23:06 -0300 Subject: [PATCH 1/2] feat: attachment product --- components/product/AddToCartButton/common.tsx | 10 +- components/product/AddToCartButton/vtex.tsx | 96 +++++++++++++++---- components/product/dryzun/ProductMain.tsx | 2 +- static/tailwind.css | 30 ++++++ 4 files changed, 116 insertions(+), 22 deletions(-) diff --git a/components/product/AddToCartButton/common.tsx b/components/product/AddToCartButton/common.tsx index dc2e5be..107b83f 100644 --- a/components/product/AddToCartButton/common.tsx +++ b/components/product/AddToCartButton/common.tsx @@ -43,9 +43,15 @@ export default function AddToCartButton(props: Props) { return ( ); } diff --git a/components/product/AddToCartButton/vtex.tsx b/components/product/AddToCartButton/vtex.tsx index a77a642..e4fd85c 100644 --- a/components/product/AddToCartButton/vtex.tsx +++ b/components/product/AddToCartButton/vtex.tsx @@ -9,34 +9,92 @@ export interface Props extends Omit { } function AddToCartButton({ seller, productID, category, eventParams }: Props) { - const { addItems } = useCart(); + const { cart, addItems, addItemAttachment } = useCart(); + const { items } = cart.value ?? { items: [] }; const [selectedSize, setSelectedSize] = useState(""); - const onAddItem = () => - addItems({ - orderItems: [{ - id: productID, - seller: seller, - quantity: 1, - }], + const handleSizeChange = (e: Event) => { + const target = e.target as HTMLSelectElement; + setSelectedSize(target.value); + }; + + const onAddItem = async () => { + await addItems({ + orderItems: [{ id: productID, seller, quantity: 1 }], }); + + if (category === "Anéis") { + const existingItemIndex = items.findIndex((item) => item.id === productID); + const index = existingItemIndex >= 0 ? existingItemIndex : items.length; + + await addItemAttachment({ + index: index, + attachment: "personalization", + content: { aro: selectedSize }, + }); + } + } + + const RING_SIZES = [ + "11", + "12", + "13", + "14", + "15", + "16", + "17", + "18", + "19", + "20", + "21", + "22" + ]; + return ( <> - {/*

{category}

*/} - - { - /* {category === "Anéis" && ( - + + + {RING_SIZES.map((size) => ( + + ))} + - )} */ - } + )} + + {category === "Anéis" && ( + ); } diff --git a/components/product/AddToCartButton/vtex.tsx b/components/product/AddToCartButton/vtex.tsx index e4fd85c..4d5ad1f 100644 --- a/components/product/AddToCartButton/vtex.tsx +++ b/components/product/AddToCartButton/vtex.tsx @@ -24,9 +24,10 @@ function AddToCartButton({ seller, productID, category, eventParams }: Props) { orderItems: [{ id: productID, seller, quantity: 1 }], }); - if (category === "Anéis") { - const existingItemIndex = items.findIndex((item) => item.id === productID); + const existingItemIndex = items.findIndex((item) => + item.id === productID + ); const index = existingItemIndex >= 0 ? existingItemIndex : items.length; await addItemAttachment({ @@ -35,36 +36,36 @@ function AddToCartButton({ seller, productID, category, eventParams }: Props) { content: { aro: selectedSize }, }); } - } - + }; + const RING_SIZES = [ - "11", - "12", - "13", - "14", - "15", - "16", - "17", - "18", - "19", - "20", - "21", - "22" + "11", + "12", + "13", + "14", + "15", + "16", + "17", + "18", + "19", + "20", + "21", + "22", ]; return ( <> - {category === "Anéis" && ( + {category === "Anéis" && (

Tamanho

)} {category === "Anéis" && ( - )} {category === "Anéis" && (