mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
fixed search
This commit is contained in:
parent
089240afa2
commit
4929032e5f
@ -2,7 +2,84 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next"
|
import { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
import commerce from '@lib/api/commerce'
|
import commerce from '@lib/api/commerce'
|
||||||
import { truncate } from "fs/promises"
|
import truncate from "truncate-html";
|
||||||
|
|
||||||
|
|
||||||
|
import { Product } from '@commerce/types/product'
|
||||||
|
|
||||||
|
|
||||||
|
const SORT: { [key: string]: string | undefined } = {
|
||||||
|
latest: 'id',
|
||||||
|
trending: 'total_sold',
|
||||||
|
price: 'price',
|
||||||
|
}
|
||||||
|
|
||||||
|
const LIMIT = 12
|
||||||
|
|
||||||
|
// Return current cart info
|
||||||
|
const getProducts = async ({ search, config }: any) => {
|
||||||
|
// Use a dummy base as we only care about the relative path
|
||||||
|
const url = new URL('/v3/catalog/products', 'http://a')
|
||||||
|
|
||||||
|
url.searchParams.set('is_visible', 'true')
|
||||||
|
url.searchParams.set('limit', String(LIMIT))
|
||||||
|
|
||||||
|
if (search) url.searchParams.set('keyword', search)
|
||||||
|
|
||||||
|
// if (categoryId && Number.isInteger(Number(categoryId)))
|
||||||
|
// url.searchParams.set('categories:in', String(categoryId))
|
||||||
|
|
||||||
|
// if (brandId && Number.isInteger(Number(brandId)))
|
||||||
|
// url.searchParams.set('brand_id', String(brandId))
|
||||||
|
|
||||||
|
// if (sort) {
|
||||||
|
// const [_sort, direction] = sort.split('-')
|
||||||
|
// const sortValue = SORT[_sort]
|
||||||
|
|
||||||
|
// if (sortValue && direction) {
|
||||||
|
// url.searchParams.set('sort', sortValue)
|
||||||
|
// url.searchParams.set('direction', direction)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// We only want the id of each product
|
||||||
|
url.searchParams.set('include_fields', 'id')
|
||||||
|
|
||||||
|
const commerceConfig = commerce.provider.config
|
||||||
|
|
||||||
|
const { data } = await commerceConfig.storeApiFetch<{ data: { id: number }[] }>(
|
||||||
|
url.pathname + url.search
|
||||||
|
)
|
||||||
|
|
||||||
|
const ids = data.map((p) => String(p.id))
|
||||||
|
const found = ids.length > 0
|
||||||
|
|
||||||
|
// We want the GraphQL version of each product
|
||||||
|
const graphqlData = await commerce.getAllProducts({
|
||||||
|
variables: { first: LIMIT, ids },
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Put the products in an object that we can use to get them by id
|
||||||
|
const productsById = graphqlData.products.reduce<{
|
||||||
|
[k: string]: Product
|
||||||
|
}>((prods, p) => {
|
||||||
|
prods[Number(p.id)] = p
|
||||||
|
return prods
|
||||||
|
}, {})
|
||||||
|
|
||||||
|
const products: Product[] = found ? [] : graphqlData.products
|
||||||
|
|
||||||
|
// Populate the products array with the graphql products, in the order
|
||||||
|
// assigned by the list of entity ids
|
||||||
|
ids.forEach((id) => {
|
||||||
|
const product = productsById[id]
|
||||||
|
if (product) products.push(product)
|
||||||
|
})
|
||||||
|
|
||||||
|
return products
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
|
|
||||||
@ -22,7 +99,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const filter = `${req.query.search}`.toLowerCase()
|
const search = `${req.query.search}`.toLowerCase()
|
||||||
|
|
||||||
// const products = await getProducts({filter})
|
// const products = await getProducts({filter})
|
||||||
|
|
||||||
@ -31,27 +108,54 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const preview = false
|
const preview = false
|
||||||
|
|
||||||
const config = { locale, locales: [locale] }
|
const config = { locale, locales: [locale] }
|
||||||
const { products } = await commerce.getAllProducts({
|
|
||||||
variables: { first: 100 },
|
// const { products } = await commerce.getAllProducts({
|
||||||
config,
|
// variables: { first: 100 },
|
||||||
preview,
|
// config,
|
||||||
})
|
// preview,
|
||||||
|
// })
|
||||||
|
|
||||||
|
|
||||||
const ret = products
|
// const ret = products
|
||||||
.filter(p => {
|
// .filter(p => {
|
||||||
return filter === ""
|
// return filter === ""
|
||||||
|| p.name.toLowerCase().indexOf(filter) !== -1
|
// || p.name.toLowerCase().indexOf(filter) !== -1
|
||||||
|| p.description.toLowerCase().indexOf(filter) !== -1
|
// || p.description.toLowerCase().indexOf(filter) !== -1
|
||||||
})
|
// })
|
||||||
|
// .map(p => {
|
||||||
|
|
||||||
|
// return {
|
||||||
|
// name: p.name,
|
||||||
|
// imageUrl: p.images[0].url,
|
||||||
|
// price: p.price,
|
||||||
|
// id: p.id,
|
||||||
|
// description: p.description,
|
||||||
|
// slug: p.path || p.slug
|
||||||
|
// }
|
||||||
|
// }).sort((a, b) => {
|
||||||
|
// if (a.name > b.name) return 1
|
||||||
|
// return -1
|
||||||
|
// })
|
||||||
|
|
||||||
|
// res.statusCode = 200
|
||||||
|
// res.json(ret)
|
||||||
|
|
||||||
|
const products = (await getProducts({ search, config }))
|
||||||
.map(p => {
|
.map(p => {
|
||||||
|
|
||||||
|
const description = truncate(p.description, {
|
||||||
|
length: 500,
|
||||||
|
decodeEntities: true,
|
||||||
|
stripTags: true,
|
||||||
|
reserveLastWord: true,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: p.name,
|
name: p.name,
|
||||||
imageUrl: p.images[0].url,
|
imageUrl: p.images[0].url,
|
||||||
price: p.price,
|
price: p.price,
|
||||||
id: p.id,
|
id: p.id,
|
||||||
description: p.description,
|
description,
|
||||||
slug: p.path || p.slug
|
slug: p.path || p.slug
|
||||||
}
|
}
|
||||||
}).sort((a, b) => {
|
}).sort((a, b) => {
|
||||||
@ -59,8 +163,11 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
return -1
|
return -1
|
||||||
})
|
})
|
||||||
|
|
||||||
|
res.setHeader("Content-Type", "application/json")
|
||||||
res.statusCode = 200
|
res.statusCode = 200
|
||||||
res.json(ret)
|
res.json(products)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user