From d174cbea4faa53a12f17116c23a319b26ee392a3 Mon Sep 17 00:00:00 2001 From: caroluchoa Date: Mon, 12 Feb 2024 19:26:26 -0300 Subject: [PATCH] change Product type definition --- .../ChatComponents/FunctionCalls.tsx | 54 +++++++++++-------- .../shop-assistant/types/shop-assistant.ts | 7 +-- deno.json | 2 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/components/shop-assistant/ChatComponents/FunctionCalls.tsx b/components/shop-assistant/ChatComponents/FunctionCalls.tsx index da47011c..c1ef735a 100644 --- a/components/shop-assistant/ChatComponents/FunctionCalls.tsx +++ b/components/shop-assistant/ChatComponents/FunctionCalls.tsx @@ -5,8 +5,8 @@ import { MessageContentAudio, MessageContentFile, MessageContentText, - Product, } from "../types/shop-assistant.ts"; +import { Product as ProductType } from "apps/commerce/types.ts"; import { mapProductToAnalyticsItem } from "apps/commerce/utils/productToAnalyticsItem.ts"; import { useOffer } from "$store/sdk/useOffer.ts"; import AddToCartButton from "$store/islands/AddToCartButton/vtex.tsx"; @@ -27,7 +27,7 @@ export const mapProductToAnalyticsItemAssistant = ( index = 0, quantity = 1, }: { - product: Product; + product: ProductType; price?: number; listPrice?: number; index?: number; @@ -67,7 +67,7 @@ export function FunctionCalls( return (content as Content).response !== undefined; }; - const allProducts: Product[] = messages + const allProducts: ProductType[] = messages .filter((message) => message.type === "function_calls") .flatMap((message) => message.content @@ -78,7 +78,7 @@ export function FunctionCalls( "vtex/loaders/intelligentSearch/productList.ts" && content.response.length !== 0, ) - .flatMap((content) => content.response as Product[]) + .flatMap((content) => content.response as ProductType[]) ); console.log({ allProducts }); @@ -110,7 +110,7 @@ export function FunctionCalls( } function ProductShelf( - { products, assistantIds }: { products: Product[]; assistantIds: Ids }, + { products, assistantIds }: { products: ProductType[]; assistantIds: Ids }, ) { const id = useId(); console.log(products); @@ -150,13 +150,16 @@ function ProductShelf( } function ProductCard( - { product, assistantIds }: { product: Product; assistantIds: Ids }, + { product, assistantIds }: { product: ProductType; assistantIds: Ids }, ) { + const { + price = 0, + seller = "1", + } = useOffer(product.offers); const { title, description } = extractTitleAndDescription( product.description, ); - const currency = product.offers.priceCurrency; - const price = product.offers.offers[0].price; + const currency = product.offers?.priceCurrency; return (
@@ -167,7 +170,7 @@ function ProductCard( class="w-[18rem] flex justify-center" > {product.name} @@ -189,14 +192,14 @@ function ProductCard(

{ sendEvent({ name: "add_to_cart", params: { - currency: product.offers.priceCurrency, - value: product.offers.offers[0].price, + currency: product.offers?.priceCurrency, + value: product.offers?.offers[0].price, assistantId: assistantIds.assistantId, assistantThreadID: assistantIds.threadId, items: [mapProductToAnalyticsItem({ product })], @@ -211,13 +214,13 @@ function ProductCard( } function ProductCarousel( - { products, assistantIds }: { products: Product[]; assistantIds: Ids }, + { products, assistantIds }: { products: ProductType[]; assistantIds: Ids }, ) { const id = useId(); const [currentProductIndex, setCurrentProductIndex] = useState(0); - const product = products[currentProductIndex] as Product; + const product = products[currentProductIndex] as ProductType; const currency = product.offers?.priceCurrency; - const price = product.offers.offers[0].price; + const price = product.offers?.offers[0].price; const [transition, setTransition] = useState(""); const handleNextProduct = () => { @@ -300,8 +303,8 @@ function ProductCarousel( class="flex justify-center min-w-[7rem]" > {product.image[0].name} @@ -324,14 +327,14 @@ function ProductCarousel(

{ sendEvent({ name: "add_to_cart", params: { - currency: product.offers.priceCurrency, - value: product.offers.offers[0].price, + currency: product.offers?.priceCurrency, + value: product.offers?.offers[0].price, assistantId: assistantIds.assistantId, assistantThreadID: assistantIds.threadId, items: [mapProductToAnalyticsItem({ product })], @@ -360,7 +363,8 @@ function ProductCarousel( } // Helper functions -const extractTitleAndDescription = (htmlString: string) => { +const extractTitleAndDescription = (htmlString: string | undefined) => { + if (!htmlString) return { title: "", description: htmlString }; const parser = new DOMParser(); const doc = parser.parseFromString(htmlString, "text/html"); @@ -374,7 +378,7 @@ const extractTitleAndDescription = (htmlString: string) => { return { title, description }; }; -const translatePriceCurrency = (priceCurrency: string) => { +const translatePriceCurrency = (priceCurrency: string | undefined) => { if (!priceCurrency) return ""; switch (priceCurrency) { case "BRL": @@ -388,7 +392,11 @@ const translatePriceCurrency = (priceCurrency: string) => { } }; -const transformPrice = (price: number, currency: string) => { +const transformPrice = ( + price: number | undefined, + currency: string | undefined, +) => { + if (!price) return ""; // Example: change 188.7 to 188,70 if currency is BRL, any other currency will be 188.70 switch (currency) { case "BRL": diff --git a/components/shop-assistant/types/shop-assistant.ts b/components/shop-assistant/types/shop-assistant.ts index f933a2c9..2a33ed0c 100644 --- a/components/shop-assistant/types/shop-assistant.ts +++ b/components/shop-assistant/types/shop-assistant.ts @@ -1,12 +1,13 @@ // deno-lint-ignore-file no-explicit-any -// TODO(@ItamarRocha): Import these from the other Product definition +import { Product as ProductType } from "apps/commerce/types.ts"; + export interface Content { name: string; props: { url: string; request: string; }; - response: string | Product[]; + response: string | ProductType[]; options?: string[]; } @@ -44,7 +45,7 @@ export interface PropertyValue { export interface ProductGroup { "@type": string; productGroupID: string; - hasVariant: Product[]; + hasVariant: ProductType[]; url: string; name: string; additionalProperty: PropertyValue[]; diff --git a/deno.json b/deno.json index f744b2dc..136a980a 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "imports": { "$store/": "./", "deco/": "https://denopkg.com/deco-cx/deco@1.52.10/", - "apps/": "https://denopkg.com/deco-cx/apps@395abb808acd7913c1a93f37f022f89fd2fe5dae/", + "apps/": "https://denopkg.com/deco-cx/apps@ca9dd56bc62d35c595ac55f569de3cbb68bcf47a/", "$fresh/": "https://deno.land/x/fresh@1.6.3/", "preact": "https://esm.sh/preact@10.19.2", "preact/": "https://esm.sh/preact@10.19.2/",