From 8ec9875244aa1b942e2046575e0346e58be7deb6 Mon Sep 17 00:00:00 2001 From: Thalyta Fabrine Date: Tue, 7 Mar 2023 10:15:55 -0300 Subject: [PATCH] Add shippingOptions to productSuggestions query --- CHANGELOG.md | 3 +++ react/components/Autocomplete/index.tsx | 8 +++++++- react/utils/biggy-client.ts | 4 +++- react/utils/getSession.tsx | 26 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 react/utils/getSession.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a147c6..e6a44d52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- `shippingOptions` to product suggestions query. + ## [2.14.2] - 2023-01-05 ### Fixed diff --git a/react/components/Autocomplete/index.tsx b/react/components/Autocomplete/index.tsx index 70592013..82053ded 100644 --- a/react/components/Autocomplete/index.tsx +++ b/react/components/Autocomplete/index.tsx @@ -28,6 +28,7 @@ import { handleProductClick, handleSeeAllClick, } from '../../utils/pixel' +import getSession from '../../utils/getSession' const MAX_TOP_SEARCHES_DEFAULT = 10 const MAX_SUGGESTED_TERMS_DEFAULT = 5 @@ -305,6 +306,10 @@ class AutoComplete extends React.Component< isProductsLoading: true, }) + const session = await getSession() + const shippingOptions = + session?.map((item: Record) => item.value) ?? [] + const result = await this.client.suggestionProducts( term, queryFromHover ? queryFromHover.key : undefined, @@ -313,7 +318,8 @@ class AutoComplete extends React.Component< simulationBehavior, hideUnavailableItems, orderBy, - this.props.maxSuggestedProducts || MAX_SUGGESTED_PRODUCTS_DEFAULT + this.props.maxSuggestedProducts || MAX_SUGGESTED_PRODUCTS_DEFAULT, + shippingOptions ) if (!queryFromHover) { diff --git a/react/utils/biggy-client.ts b/react/utils/biggy-client.ts index 38cf7571..c2f11b6f 100644 --- a/react/utils/biggy-client.ts +++ b/react/utils/biggy-client.ts @@ -41,7 +41,8 @@ export default class BiggyClient { simulationBehavior: 'default' | 'skip' | null = 'default', hideUnavailableItems = false, orderBy?: string, - count?: number + count?: number, + shippingOptions?: string[] ): Promise> { return this.client.query({ query: suggestionProducts, @@ -54,6 +55,7 @@ export default class BiggyClient { facetValue: attributeValue, productOriginVtex: productOrigin, count, + shippingOptions, }, fetchPolicy: 'network-only', }) diff --git a/react/utils/getSession.tsx b/react/utils/getSession.tsx new file mode 100644 index 00000000..cce5e5f7 --- /dev/null +++ b/react/utils/getSession.tsx @@ -0,0 +1,26 @@ +const getSession = async () => { + const headers = new Headers() + + headers.append('Content-Type', 'application/json') + + const requestOptions: RequestInit = { + method: 'GET', + headers, + redirect: 'follow', + } + + const session = await fetch( + `${window.location.origin}/api/sessions?items=public.shippingOption`, + requestOptions + ) + + const data = await session.json() + + if (!data?.namespaces?.public?.shippingOption?.value) { + return null + } + + return JSON.parse(data.namespaces.public.shippingOption.value) +} + +export default getSession