feat: get all collection

:%s
This commit is contained in:
lytrankieio123
2021-10-05 10:30:39 +07:00
parent a8955000a5
commit 1546d5df4b
17 changed files with 230 additions and 38 deletions

View File

@@ -1,28 +1,31 @@
import { ProductCard } from '@commerce/types/product';
import { ProductVariables } from '@framework/api/operations/get-all-products';
import { FacetValue } from '@framework/schema';
import { Collection, FacetValue } from '@framework/schema';
import commerce from '@lib/api/commerce';
import { GetStaticPropsContext } from 'next';
import { Layout } from 'src/components/common';
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 { CODE_FACET_DISCOUNT, CODE_FACET_FEATURED } from 'src/utils/constanst.utils';
import { getAllFacetValueIdsByParentCode, getAllFacetValuesForFeatuedProducts, getFreshFacetId } from 'src/utils/funtion.utils';
import { getAllFacetValueIdsByParentCode, getAllFacetValuesForFeatuedProducts, getAllPromies, getFreshFacetId } from 'src/utils/funtion.utils';
import { PromiseWithKey } from 'src/utils/types.utils';
interface Props {
featuredAndDiscountFacetsValue: FacetValue[],
freshProducts: ProductCard[],
featuredProducts: ProductCard[],
collections: Collection[]
}
export default function Home({ featuredAndDiscountFacetsValue,
freshProducts, featuredProducts }: Props) {
freshProducts, featuredProducts,
collections }: Props) {
return (
<>
<HomeBanner />
<HomeFeature />
<HomeCategories />
<FreshProducts data={freshProducts} />
<FreshProducts data={freshProducts} collections={collections} />
<HomeCollection />
<HomeVideo />
<HomeSpice />
@@ -44,13 +47,15 @@ export async function getStaticProps({
locales,
}: GetStaticPropsContext) {
const config = { locale, locales }
let promisesWithKey = [] as PromiseWithKey[]
let props = {} as any
const { facets } = await commerce.getAllFacets({
variables: {},
config,
preview,
})
const featuredAndDiscountFacetsValue = getAllFacetValuesForFeatuedProducts(facets)
props.featuredAndDiscountFacetsValue = getAllFacetValuesForFeatuedProducts(facets)
// fresh products
const freshProductvariables: ProductVariables = {}
@@ -63,6 +68,8 @@ export async function getStaticProps({
config,
preview,
})
promisesWithKey.push({ key: 'freshProducts', promise: freshProductsPromise, keyResult: 'products' })
// featured products
const allFeaturedFacetIds = getAllFacetValueIdsByParentCode(facets, CODE_FACET_FEATURED)
@@ -75,28 +82,33 @@ export async function getStaticProps({
config,
preview,
})
promisesWithKey.push({ key: 'featuredProducts', promise: featuredProductsPromise, keyResult: 'products' })
// collection
const collectionsPromise = commerce.getAllCollections({
variables: {},
config,
preview,
})
promisesWithKey.push({ key: 'collections', promise: collectionsPromise, keyResult: 'collections' })
try {
const rs = await Promise.all([
freshProductsPromise,
featuredProductsPromise,
])
const promises = getAllPromies(promisesWithKey)
const rs = await Promise.all(promises)
promisesWithKey.map((item, index) => {
props[item.key] = item.keyResult ? rs[index][item.keyResult] : rs[index]
return null
})
return {
props: {
featuredAndDiscountFacetsValue,
freshProducts: freshFacetId ? rs[0].products : [],
featuredProducts: facetValueIdsForFeaturedProducts.length > 0 ? rs[1].products : []
},
props,
revalidate: 60,
}
} catch (err) {
}
}