From ad2a4ddee2fec4778593a78fdf8b86f801082e2c Mon Sep 17 00:00:00 2001 From: guitavano Date: Wed, 23 Oct 2024 07:26:13 -0300 Subject: [PATCH] improve dynamic-options id loader --- vtex/loaders/options/productIdByTerm.ts | 28 +++++++++++++++++++------ website/components/Image.tsx | 4 +++- website/handlers/proxy.ts | 3 +-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/vtex/loaders/options/productIdByTerm.ts b/vtex/loaders/options/productIdByTerm.ts index d1decf347..a5fd938e0 100644 --- a/vtex/loaders/options/productIdByTerm.ts +++ b/vtex/loaders/options/productIdByTerm.ts @@ -3,6 +3,13 @@ import { allowCorsFor } from "@deco/deco"; interface Props { term?: string; } + +interface DynamicOptionsReturn { + value: string; + label: string; + image?: string; +} + const loader = async (props: Props, req: Request, ctx: AppContext) => { Object.entries(allowCorsFor(req)).map(([name, value]) => { ctx.response.headers.set(name, value); @@ -19,11 +26,20 @@ const loader = async (props: Props, req: Request, ctx: AppContext) => { }]; } - return suggestions?.products?.map((product) => ({ - value: `${product.productID}`, - label: - `${product.productID} - ${product.isVariantOf?.name} ${product.name} - ${product.isVariantOf?.productGroupID}`, - image: product.image?.[0]?.url, - })); + let suggestionsArray: DynamicOptionsReturn[] = []; + + suggestions?.products?.forEach((product) => { + const variants = product.isVariantOf?.hasVariant?.map((variant) => { + return { + value: `${variant.productID}`, + label: + `${variant.productID} - ${product.isVariantOf?.name} ${variant.name} - ${product.isVariantOf?.productGroupID}`, + image: variant.image?.[0]?.url, + }; + }) || []; + suggestionsArray = suggestionsArray.concat(variants); + }); + + return suggestionsArray; }; export default loader; diff --git a/website/components/Image.tsx b/website/components/Image.tsx index 3774ab93f..b3d607af6 100644 --- a/website/components/Image.tsx +++ b/website/components/Image.tsx @@ -100,7 +100,9 @@ export const getOptimizedMediaUrl = (opts: OptimizationOptions) => { } if ( - /(vteximg.com.br|vtexassets.com|myvtex.com)\/arquivos\/ids\/\d+/.test(originalSrc) + /(vteximg.com.br|vtexassets.com|myvtex.com)\/arquivos\/ids\/\d+/.test( + originalSrc, + ) ) { return optimizeVTEX(opts); } diff --git a/website/handlers/proxy.ts b/website/handlers/proxy.ts index eaa9f2a45..ae5dab232 100644 --- a/website/handlers/proxy.ts +++ b/website/handlers/proxy.ts @@ -113,7 +113,7 @@ export default function Proxy({ const to = new URL( `${proxyUrl}${avoidAppendPath ? "" : sanitize(path)}`, ); - + to.search = qs; const headers = new Headers(req.headers); @@ -132,7 +132,6 @@ export default function Proxy({ headers.set("host", hostToUse ?? to.host); headers.set("x-forwarded-host", url.host); - for (const { key, value } of customHeaders) { if (key === "cookie") { const existingCookie = headers.get("cookie");