mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
✨ feat: get featured product base on facet Featured and Discount
:%s
This commit is contained in:
@@ -6,15 +6,16 @@ import { GetStaticPropsContext } from 'next';
|
|||||||
import { Layout } from 'src/components/common';
|
import { Layout } from 'src/components/common';
|
||||||
import { FeaturedProductsCarousel, FreshProducts, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home';
|
import { FeaturedProductsCarousel, FreshProducts, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home';
|
||||||
import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice';
|
import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice';
|
||||||
import { getAllFeaturedFacetId, getAllFeaturedFacetValue, getFreshFacetId } from 'src/utils/funtion.utils';
|
import { CODE_FACET_DISCOUNT, CODE_FACET_FEATURED } from 'src/utils/constanst.utils';
|
||||||
|
import { getAllFacetValueIdsByParentCode, getAllFacetValuesForFeatuedProducts, getFreshFacetId } from 'src/utils/funtion.utils';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
featuredFacetsValue: FacetValue[],
|
featuredAndDiscountFacetsValue: FacetValue[],
|
||||||
freshProducts: ProductCard[],
|
freshProducts: ProductCard[],
|
||||||
featuredProducts: ProductCard[],
|
featuredProducts: ProductCard[],
|
||||||
|
|
||||||
}
|
}
|
||||||
export default function Home({ featuredFacetsValue,
|
export default function Home({ featuredAndDiscountFacetsValue,
|
||||||
freshProducts, featuredProducts }: Props) {
|
freshProducts, featuredProducts }: Props) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -25,7 +26,7 @@ export default function Home({ featuredFacetsValue,
|
|||||||
<HomeCollection />
|
<HomeCollection />
|
||||||
<HomeVideo />
|
<HomeVideo />
|
||||||
<HomeSpice />
|
<HomeSpice />
|
||||||
<FeaturedProductsCarousel data={featuredProducts} featuredFacetsValue={featuredFacetsValue} />
|
<FeaturedProductsCarousel data={featuredProducts} featuredFacetsValue={featuredAndDiscountFacetsValue} />
|
||||||
<HomeCTA />
|
<HomeCTA />
|
||||||
<HomeRecipe />
|
<HomeRecipe />
|
||||||
<HomeSubscribe />
|
<HomeSubscribe />
|
||||||
@@ -48,11 +49,12 @@ export async function getStaticProps({
|
|||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
const featuredFacetsValue = getAllFeaturedFacetValue(facets)
|
const featuredAndDiscountFacetsValue = getAllFacetValuesForFeatuedProducts(facets)
|
||||||
|
|
||||||
|
|
||||||
|
// fresh products
|
||||||
const freshProductvariables: ProductVariables = {}
|
const freshProductvariables: ProductVariables = {}
|
||||||
const freshFacetId = getFreshFacetId(facets)
|
const freshFacetId = getFreshFacetId(facets)
|
||||||
|
|
||||||
if (freshFacetId) {
|
if (freshFacetId) {
|
||||||
freshProductvariables.facetValueIds = [freshFacetId]
|
freshProductvariables.facetValueIds = [freshFacetId]
|
||||||
}
|
}
|
||||||
@@ -62,15 +64,19 @@ export async function getStaticProps({
|
|||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
|
||||||
const allFeaturedFacetId = getAllFeaturedFacetId(facets)
|
// featured products
|
||||||
|
const allFeaturedFacetIds = getAllFacetValueIdsByParentCode(facets, CODE_FACET_FEATURED)
|
||||||
|
const allDiscountFacetIds = getAllFacetValueIdsByParentCode(facets, CODE_FACET_DISCOUNT)
|
||||||
|
const facetValueIdsForFeaturedProducts = [...allFeaturedFacetIds, ...allDiscountFacetIds]
|
||||||
const featuredProductsPromise = commerce.getAllProducts({
|
const featuredProductsPromise = commerce.getAllProducts({
|
||||||
variables: {
|
variables: {
|
||||||
facetValueIds: allFeaturedFacetId
|
facetValueIds: facetValueIdsForFeaturedProducts
|
||||||
},
|
},
|
||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rs = await Promise.all([
|
const rs = await Promise.all([
|
||||||
freshProductsPromise,
|
freshProductsPromise,
|
||||||
@@ -79,10 +85,9 @@ export async function getStaticProps({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
facets,
|
featuredAndDiscountFacetsValue,
|
||||||
featuredFacetsValue,
|
|
||||||
freshProducts: freshFacetId ? rs[0].products : [],
|
freshProducts: freshFacetId ? rs[0].products : [],
|
||||||
featuredProducts: rs[1].products
|
featuredProducts: facetValueIdsForFeaturedProducts.length > 0 ? rs[1].products : []
|
||||||
},
|
},
|
||||||
revalidate: 60,
|
revalidate: 60,
|
||||||
}
|
}
|
||||||
|
@@ -112,9 +112,9 @@ export const BRAND = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export const CODE_FACET_FEATURED = 'featured'
|
export const CODE_FACET_FEATURED = 'featured'
|
||||||
|
export const CODE_FACET_DISCOUNT = 'discount'
|
||||||
export const CODE_FACET_FEATURED_VARIANT = {
|
export const CODE_FACET_FEATURED_VARIANT = {
|
||||||
FRESH: 'fresh',
|
FRESH: 'fresh',
|
||||||
BEST_SELLERS: 'best-sellers'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const FEATURED = [
|
export const FEATURED = [
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { Facet } from "@commerce/types/facet";
|
import { Facet } from "@commerce/types/facet";
|
||||||
import { FacetValue } from './../../framework/vendure/schema.d';
|
import { FacetValue } from './../../framework/vendure/schema.d';
|
||||||
import { CODE_FACET_FEATURED, CODE_FACET_FEATURED_VARIANT } from "./constanst.utils";
|
import { CODE_FACET_DISCOUNT, CODE_FACET_FEATURED, CODE_FACET_FEATURED_VARIANT } from "./constanst.utils";
|
||||||
|
|
||||||
export function isMobile() {
|
export function isMobile() {
|
||||||
return window.innerWidth < 768
|
return window.innerWidth < 768
|
||||||
@@ -30,16 +30,21 @@ export function getFreshFacetId(facets: Facet[]) {
|
|||||||
return freshFacetValue?.id
|
return freshFacetValue?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllFeaturedFacetId(facets: Facet[]) {
|
export function getAllFacetValueIdsByParentCode(facets: Facet[], code: string) {
|
||||||
const featuredFacet = facets.find((item: Facet) => item.code === CODE_FACET_FEATURED)
|
const featuredFacet = facets.find((item: Facet) => item.code === code)
|
||||||
const rs = featuredFacet?.values.map((item: FacetValue) => item.id)
|
const rs = featuredFacet?.values.map((item: FacetValue) => item.id)
|
||||||
|
|
||||||
return rs
|
return rs || []
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllFeaturedFacetValue(facets: Facet[]) {
|
export function getAllFacetValuesForFeatuedProducts(facets: Facet[]) {
|
||||||
const featuredFacet = facets.find((item: Facet) => item.code === CODE_FACET_FEATURED)
|
const facetsRs = facets.filter((item: Facet) => item.code === CODE_FACET_FEATURED || item.code === CODE_FACET_DISCOUNT)
|
||||||
return featuredFacet?.values
|
let rs = [] as FacetValue[]
|
||||||
|
facetsRs.map((item: Facet) => {
|
||||||
|
rs = rs.concat(item.values)
|
||||||
|
return null
|
||||||
|
})
|
||||||
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFacetNamesFromIds(facets: FacetValue[], ids?: string[]): string {
|
export function getFacetNamesFromIds(facets: FacetValue[], ids?: string[]): string {
|
||||||
|
Reference in New Issue
Block a user