Skip to content

Commit

Permalink
Merge pull request #181 from vtex-apps/feature/shipping-options
Browse files Browse the repository at this point in the history
Add shippingOptions to productSuggestions query
  • Loading branch information
thalytafabrine authored Mar 17, 2023
2 parents 89a617e + 8ec9875 commit 096dc53
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion react/components/Autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -305,6 +306,10 @@ class AutoComplete extends React.Component<
isProductsLoading: true,
})

const session = await getSession()
const shippingOptions =
session?.map((item: Record<string, string>) => item.value) ?? []

const result = await this.client.suggestionProducts(
term,
queryFromHover ? queryFromHover.key : undefined,
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion react/utils/biggy-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default class BiggyClient {
simulationBehavior: 'default' | 'skip' | null = 'default',
hideUnavailableItems = false,
orderBy?: string,
count?: number
count?: number,
shippingOptions?: string[]
): Promise<ApolloQueryResult<{ productSuggestions: IProductsOutput }>> {
return this.client.query({
query: suggestionProducts,
Expand All @@ -54,6 +55,7 @@ export default class BiggyClient {
facetValue: attributeValue,
productOriginVtex: productOrigin,
count,
shippingOptions,
},
fetchPolicy: 'network-only',
})
Expand Down
26 changes: 26 additions & 0 deletions react/utils/getSession.tsx
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 096dc53

Please sign in to comment.