mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 03:31:23 +00:00
feat: get-blog-list
This commit is contained in:
@@ -1,14 +1,72 @@
|
||||
import commerce from '@lib/api/commerce';
|
||||
import { GetStaticPropsContext } from 'next';
|
||||
import { Layout } from 'src/components/common';
|
||||
import { BlogsList, FeaturedCardBlog, BlogHeading, BlogBreadCrumb } from 'src/components/modules/blogs';
|
||||
import { BlogCardProps } from 'src/components/common/CardBlog/CardBlog';
|
||||
import { BlogBreadCrumb, BlogHeading, BlogsList, FeaturedCardBlog } from 'src/components/modules/blogs';
|
||||
import { DEFAULT_BLOG_PAGE_SIZE } from "src/utils/constanst.utils";
|
||||
import { getAllPromies } from 'src/utils/funtion.utils';
|
||||
import { PromiseWithKey } from 'src/utils/types.utils';
|
||||
|
||||
export default function BlogsPage() {
|
||||
|
||||
|
||||
interface Props {
|
||||
blogsResult: { blogs: BlogCardProps[] },
|
||||
totalItems: number
|
||||
}
|
||||
export default function BlogsPage({blogsResult,totalItems}:Props) {
|
||||
return(
|
||||
<>
|
||||
<BlogBreadCrumb />
|
||||
<BlogHeading />
|
||||
<FeaturedCardBlog />
|
||||
<BlogsList />
|
||||
<FeaturedCardBlog
|
||||
title={blogsResult.blogs[0].title}
|
||||
imgSrc={blogsResult.blogs[0].imageSrc ?? ''}
|
||||
content={blogsResult.blogs[0].description}
|
||||
/>
|
||||
<BlogsList blogList={blogsResult.blogs} total={totalItems} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
BlogsPage.Layout = Layout
|
||||
|
||||
|
||||
export async function getStaticProps({
|
||||
preview,
|
||||
locale,
|
||||
locales,
|
||||
}: GetStaticPropsContext) {
|
||||
const config = { locale, locales }
|
||||
let promisesWithKey = [] as PromiseWithKey[]
|
||||
let props = {} as any;
|
||||
|
||||
|
||||
// Blogs
|
||||
const blogsPromise = commerce.getAllBlogs({
|
||||
variables: {
|
||||
take: DEFAULT_BLOG_PAGE_SIZE
|
||||
},
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
promisesWithKey.push({ key: 'blogsResult', promise: blogsPromise })
|
||||
|
||||
|
||||
try {
|
||||
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,
|
||||
revalidate: 60
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BlogsPage.Layout = Layout
|
||||
|
@@ -10,12 +10,12 @@ import { CODE_FACET_DISCOUNT, CODE_FACET_FEATURED } from 'src/utils/constanst.ut
|
||||
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,
|
||||
@@ -99,7 +99,6 @@ export async function getStaticProps({
|
||||
})
|
||||
promisesWithKey.push({ key: 'collections', promise: collectionsPromise, keyResult: 'collections' })
|
||||
|
||||
|
||||
try {
|
||||
const promises = getAllPromies(promisesWithKey)
|
||||
const rs = await Promise.all(promises)
|
||||
|
Reference in New Issue
Block a user