Skip to content

Commit

Permalink
add segment pdp
Browse files Browse the repository at this point in the history
  • Loading branch information
evertonvargasvtex committed Oct 28, 2024
1 parent 45e7e12 commit 7f7a23f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
21 changes: 4 additions & 17 deletions packages/api/src/platforms/vtex/clients/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SearchArgs {
hideUnavailableItems?: boolean
showInvisibleItems?: boolean
showSponsored?: boolean
segment?: string
}

export interface ProductLocator {
Expand All @@ -56,19 +57,6 @@ export const IntelligentSearch = (
const base = `https://${account}.${environment}.com.br/api/io`
const storeCookies = getStoreCookie(ctx)

const getVtexSegment = (cookies?: string) => {
if (typeof cookies !== 'string') {
return null;
}

const match = cookies.match(/vtex_segment=([^;]*)/);

console.log("match", match)
return match ? match[1] : null;
};

const segmentCookie = ctx ? getVtexSegment(ctx.headers.cookie) : null

const getPolicyFacet = (): IStoreSelectedFacet | null => {
const { salesChannel } = ctx.storage.channel

Expand Down Expand Up @@ -128,6 +116,7 @@ export const IntelligentSearch = (
type,
fuzzy = 'auto',
showInvisibleItems,
segment = undefined,
}: SearchArgs): Promise<T> => {
const params = new URLSearchParams({
page: (page + 1).toString(),
Expand Down Expand Up @@ -158,12 +147,10 @@ export const IntelligentSearch = (
.map(({ key, value }) => `${key}/${value}`)
.join('/')

console.log("search", segmentCookie)

const headers = segmentCookie
const headers = segment
? {
headers: {
'Cookie': `vtex_segment=${segmentCookie}`,
'Cookie': `vtex_segment=${segment}`,
},
}
: undefined
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
findSkuId,
findSlug,
transformSelectedFacet,
findSegment
} from '../utils/facets'
import { SORT_MAP } from '../utils/sort'
import { StoreCollection } from './collection'
Expand All @@ -34,6 +35,7 @@ export const Query = {
const locale = findLocale(locator)
const id = findSkuId(locator)
const slug = findSlug(locator)
const segment = findSegment(locator)

if (channel) {
mutateChannelContext(ctx, channel)
Expand Down Expand Up @@ -94,6 +96,7 @@ export const Query = {
page: 0,
count: 1,
query: `product:${route.id}`,
segment
})

if (!product) {
Expand Down Expand Up @@ -121,6 +124,9 @@ export const Query = {
const channel = findChannel(selectedFacets)
const locale = findLocale(selectedFacets)
const crossSelling = findCrossSelling(selectedFacets)
const segment = findSegment(selectedFacets)

console.log("search segment", segment)

if (channel) {
mutateChannelContext(ctx, channel)
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/platforms/vtex/utils/facets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ export const findLocale = (facets?: Maybe<SelectedFacet[]>) =>

export const findChannel = (facets?: Maybe<SelectedFacet[]>) =>
facets?.find((facet) => facet.key === 'channel')?.value ?? null

export const findSegment = (facets?: Maybe<SelectedFacet[]>) =>
facets?.find((facet) => facet.key === 'segment')?.value ?? undefined

0 comments on commit 7f7a23f

Please sign in to comment.