Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call IS to get autocomplete suggestions #465

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@types/node": "^12.0.0",
"@types/qs": "^6.5.1",
"@types/ramda": "^0.26.21",
"@vtex/api": "6.45.22",
"@vtex/api": "6.46.0",
"@vtex/tsconfig": "^0.6.0",
"eslint": "^5.15.3",
"eslint-config-vtex": "^10.1.0",
Expand Down
22 changes: 19 additions & 3 deletions node/resolvers/search/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ const extractSlug = (item: SearchAutocompleteUnit) => {
return item.criteria ? `${href[3]}/${href[4]}` : href[3]
}

const extractThumb = (item: SearchItem) => {
const imageId = `${item.images[0].imageId}-25-25`
const imagePaths = item.images[0].imageUrl.split(`/${item.images[0].imageId}/`)
return `<img src="${imagePaths[0]}/${imageId}/${imagePaths[1]}" width="25" height="25" alt="${imagePaths[1].split('.')[0]}" id="" />`
}

export const resolvers = {
Items: {
slug: (root: SearchAutocompleteUnit) => extractSlug(root),
slug: (root: any) => root.linkText ?? extractSlug(root),

productId: ({ items, productId }: any) => {
if (productId) {
return productId
}
return items ? path([0, 'productId'], items) : null
},

name: (root: any) => root.productName ?? root.name ?? "",

href: (root: any) => root.href ?? `https://portal.vtexcommercestable.com.br/${root.linkText}/p`,

productId: ({ items }: SearchAutocompleteUnit) =>
items ? path([0, 'productId'], items) : null,
thumb: (root: any) => root.items.length ? extractThumb(root.items[0] as SearchItem) : root.thumb,
},
}
46 changes: 37 additions & 9 deletions node/resolvers/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,24 +315,52 @@ export const queries = {
ctx: Context
) => {
const {
clients: { search },
clients: { intelligentSearchApi },
} = ctx

if (!args.searchTerm) {
throw new UserInputError('No search term provided')
}

const translatedTerm = await translateToStoreDefaultLanguage(
ctx,
args.searchTerm
)
const { itemsReturned } = await search.autocomplete({
maxRows: args.maxRows,
searchTerm: translatedTerm,
const workspaceSearchParams = await getWorkspaceSearchParamsFromStorage(ctx)

const biggyArgs : {[key: string] : any} = {
query: args.searchTerm,
from: 0,
to: args.maxRows ? args.maxRows - 1 : 4,
sort: '',
allowRedirect: false, // When there is a redirect, no product is returned.
...workspaceSearchParams,
}

const items: (SearchAutocompleteUnit | SearchProduct)[] = []

const [productSuggestions, searchSuggestions] = await Promise.all([
intelligentSearchApi.productSearch(biggyArgs, ''),
intelligentSearchApi.autocompleteSearchSuggestions({query: args.searchTerm})
])

const criterias = searchSuggestions.searches.filter((searchItem: any) => searchItem.term === args.searchTerm)
if (criterias.length) {
criterias[0].attributes.map((att: any) => {
items.push({
items: [],
thumb: "",
thumbUrl: null,
name: `${args.searchTerm} ${att.labelValue}`,
href: `https://portal.vtexcommercestable.com.br/${att.value}/${args.searchTerm}`,
criteria: `£${args.searchTerm} ${att.labelValue}¢/${att.value}/${args.searchTerm}`, // validate with catalog
})
})
}

productSuggestions.products.map((product: SearchProduct) => {
items.push(product)
})

return {
cacheId: args.searchTerm,
itemsReturned,
itemsReturned: items,
}
},
facets: async (_: any, args: FacetsInput, ctx: any) => {
Expand Down
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,10 @@
lodash.unescape "4.0.1"
semver "5.5.0"

"@vtex/api@6.45.22":
version "6.45.22"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.22.tgz#fa9bbfde1a4d4fbbaf6cce9f6dbc9bb9ee929ba3"
integrity sha512-g5cGUDhF4FADgSMpQmce/bnIZumwGlPG2cabwbQKIQ+cCFMZqOEM/n+YQb1+S8bCyHkzW3u/ZABoyCKi5/nxxg==
"@vtex/api@6.46.0":
version "6.46.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.46.0.tgz#208d14b96cbc8fd5eb6bd18fbd0c8424886e6154"
integrity sha512-XAvJlD1FG1GynhPXiMcayunahFCL2r3ilO5MHAWKxYvB/ljyxi4+U+rVpweeaQGpxHfhKHdfPe7qNEEh2oa2lw==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down
Loading