From 5a3f0497545acf9232a41e94479f94fd0c73ff56 Mon Sep 17 00:00:00 2001 From: Lucas Sacci Date: Thu, 27 Jun 2024 08:47:54 -0300 Subject: [PATCH] fix(magento): pdp in uses only gql --- magento/loaders/product/detailsPageGQL.ts | 71 +++++++---------------- magento/utils/clientGraphql/types.ts | 2 +- 2 files changed, 21 insertions(+), 52 deletions(-) diff --git a/magento/loaders/product/detailsPageGQL.ts b/magento/loaders/product/detailsPageGQL.ts index b86e3714f..328597293 100644 --- a/magento/loaders/product/detailsPageGQL.ts +++ b/magento/loaders/product/detailsPageGQL.ts @@ -10,8 +10,6 @@ import { import { formatUrlSuffix, getCustomFields } from "../../utils/utilsGraphQL.ts"; import { GetCompleteProduct } from "../../utils/clientGraphql/queries.ts"; import { toProductGraphQL } from "../../utils/transform.ts"; -import { URL_KEY } from "../../utils/constants.ts"; -import stringifySearchCriteria from "../../utils/stringifySearchCriteria.ts"; export interface Props { slug: RequestURLParam; @@ -49,61 +47,32 @@ async function loader( site, minInstallmentValue, maxInstallments, - currencyCode, - storeId, - clientAdmin, } = ctx; const STALE = enableCache ? DecoStale : undefined; const customAttributes = getCustomFields(customFields, ctx.customAttributes); const defaultPath = useSuffix ? formatUrlSuffix(site) : undefined; - const getProductSku = async () => { - const searchCriteria = { - filterGroups: [ - { - filters: [{ field: URL_KEY, value: slug }], - }, - ], - }; - - const queryParams = { - site, - currencyCode, - storeId, - ...stringifySearchCriteria(searchCriteria), - }; - - const itemSku = await clientAdmin["GET /rest/:site/V1/products"]( - { - ...queryParams, + const { products } = await clientGraphql.query< + ProductDetailsGraphQL, + ProductDetailsInputs + >( + { + variables: { + filter: { url_key: { eq: slug } }, }, - STALE, - ).then((res) => res.json()); - if (!itemSku.items.length) return null; - return itemSku.items[0].sku; - }; - - const getFullProduct = async (sku: string) => - await clientGraphql.query( - { - variables: { - filter: { sku: { eq: sku } }, - }, - ...GetCompleteProduct(customAttributes, isBreadcrumbProductName), - }, - STALE, - ); + ...GetCompleteProduct(customAttributes, isBreadcrumbProductName), + }, + STALE, + ); - const sku = await getProductSku(); + const product = products.items.find((p) => p.url_key === slug); - if (!sku) { - console.log("product not found"); + if (!product) { return null; } - const { products } = await getFullProduct(sku); const productCanonicalUrl = new URL( - (defaultPath ?? "") + products.items[0]?.canonical_url ?? "", + (defaultPath ?? "") + product?.canonical_url ?? "", url.origin, ); @@ -111,12 +80,12 @@ async function loader( "@type": "ListItem", item: productCanonicalUrl.href, position: 1, - name: products.items[0]?.name ?? "", + name: product?.name ?? "", } as ListItem; const itemListElement: ListItem[] = isBreadcrumbProductName ? [productListElement] - : products.items[0].categories?.map( + : product.categories?.map( ({ position, url_path, url_key, name }) => ({ "@type": "ListItem", item: @@ -133,7 +102,7 @@ async function loader( itemListElement, numberOfItems: itemListElement.length, }, - product: toProductGraphQL(products.items[0], { + product: toProductGraphQL(product, { originURL: url, imagesQtd: 999, customAttributes, @@ -142,9 +111,9 @@ async function loader( defaultPath, }), seo: { - title: products.items[0].meta_title?.trim() ?? - products.items[0].name.trim() ?? "", - description: products.items[0].meta_description?.trim() ?? "", + title: product.meta_title?.trim() ?? + product.name.trim() ?? "", + description: product.meta_description?.trim() ?? "", canonical: productCanonicalUrl.href, }, }; diff --git a/magento/utils/clientGraphql/types.ts b/magento/utils/clientGraphql/types.ts index f00aa40fc..2003009df 100644 --- a/magento/utils/clientGraphql/types.ts +++ b/magento/utils/clientGraphql/types.ts @@ -249,7 +249,7 @@ export interface ProductSearchInputs { export interface ProductDetailsInputs { filter: { - sku: { + url_key: { eq: string; }; };