Add a filter to avoid duplicated items at same section

This commit is contained in:
Guillermo Angulo 2020-12-01 18:27:26 -03:00
parent bbcf63b892
commit 6df3a164d6

View File

@ -49,13 +49,17 @@ export async function getStaticProps({
// products, then fill them with products from the products list, this // products, then fill them with products from the products list, this
// is useful for new commerce sites that don't have a lot of products // is useful for new commerce sites that don't have a lot of products
return { return {
featured: rangeMap(6, (i) => featuredProducts[i] ?? products.shift()) featured: rangeMap(
6,
(i) => featuredProducts[i] ?? nonDuplicated(products, featuredProducts)
)
.filter(nonNullable) .filter(nonNullable)
.sort((a, b) => a.node.prices.price.value - b.node.prices.price.value) .sort((a, b) => a.node.prices.price.value - b.node.prices.price.value)
.reverse(), .reverse(),
bestSelling: rangeMap( bestSelling: rangeMap(
6, 6,
(i) => bestSellingProducts[i] ?? products.shift() (i) =>
bestSellingProducts[i] ?? nonDuplicated(products, bestSellingProducts)
).filter(nonNullable), ).filter(nonNullable),
} }
})() })()
@ -75,6 +79,16 @@ export async function getStaticProps({
const nonNullable = (v: any) => v const nonNullable = (v: any) => v
const nonDuplicated: any = (products: any[], arr: any[]) => {
if (!!products.length) {
const product = products.shift()
return !arr.some(({ node }) => node.entityId === product.node.entityId)
? product
: nonDuplicated(products, arr)
}
return
}
export default function Home({ export default function Home({
featured, featured,
bestSelling, bestSelling,