From 21b0200205e816332de845ae7d7fe2ac33966b6c Mon Sep 17 00:00:00 2001 From: lytrankieio123 Date: Mon, 4 Oct 2021 11:14:38 +0700 Subject: [PATCH] :raising_hand: test getStaticProps with revalidate --- pages/index.tsx | 51 ++++++++++++++++++++++++++++--- pages/test.tsx | 79 +++++++++++++++++++++++++++---------------------- 2 files changed, 91 insertions(+), 39 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index 3fa86079d..aabbb1f94 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,19 +1,29 @@ +import commerce from '@lib/api/commerce'; +import { GetStaticPropsContext } from 'next'; import { Layout } from 'src/components/common'; import { FeaturedProductsCarousel, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home'; import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice'; -export default function Home() { +interface Props { + products: any +} +export default function Home({ products }: Props) { return ( <> +

+ TOTAL: {products?.length} +

+ + {JSON.stringify(products[0])} - - + + - + {/* // todo: uncomment @@ -22,4 +32,37 @@ export default function Home() { ) } + +export async function getStaticProps({ + preview, + locale, + locales, +}: GetStaticPropsContext) { + const config = { locale, locales } + const productsPromise = commerce.getAllProducts({ + // const productsPromise = commerce.getAllFacets({ + variables: { + first: 70, + // filter: { + // name: { + // contains: 'ca' + // } + // } + }, + config, + preview, + // Saleor provider only + ...({ featured: true } as any), + }) + + const { products } = await productsPromise + + + return { + props: { products }, + revalidate: 20, + } +} + + Home.Layout = Layout diff --git a/pages/test.tsx b/pages/test.tsx index 452b42173..6244c3dd6 100644 --- a/pages/test.tsx +++ b/pages/test.tsx @@ -1,42 +1,51 @@ -import { QueryFacetsArgs } from '@framework/schema' -import { useMemo, useState } from 'react' -import { Layout } from 'src/components/common' -import { useFacets } from 'src/components/hooks/facets' - -export default function Test() { - const [keyword, setKeyword] = useState('c') - - const optionsFilter = useMemo(() => { - console.log("change options") - return { - options: { - filter: { - name: { - contains: keyword - } - } - } - } as QueryFacetsArgs - }, [keyword]) - - const { items, totalItems } = useFacets(optionsFilter) - - const changeQuery = () => { - setKeyword('ca') - } +import commerce from '@lib/api/commerce'; +import { GetStaticPropsContext } from 'next'; +import { Layout } from 'src/components/common'; +interface Props { + products: any +} +export default function Home({ products }: Props) { return ( <> -
Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore, praesentium.
-
- total: {totalItems} -
-
- ITEMS: {JSON.stringify(items)} -
- +

+ TOTAL: {products?.length} +

+ {JSON.stringify(products[0])} ) } -Test.Layout = Layout + +export async function getServerSideProps({ + preview, + locale, + locales, +}: GetStaticPropsContext) { + const config = { locale, locales } + const productsPromise = commerce.getAllProducts({ + // const productsPromise = commerce.getAllFacets({ + variables: { + first: 70, + // filter: { + // name: { + // contains: 'ca' + // } + // } + }, + config, + preview, + // Saleor provider only + ...({ featured: true } as any), + }) + + const { products } = await productsPromise + + + return { + props: { products }, + } +} + + +Home.Layout = Layout