From 91ea63bff8eede2b48dcf472b2185f7367cb1e43 Mon Sep 17 00:00:00 2001 From: Reza Babaei Date: Sat, 18 Sep 2021 00:48:20 +0300 Subject: [PATCH] save before rebase --- .vscode/launch.json | 12 +++++ .../api/operations/get-all-products.ts | 44 ++++++++++++------- .../api/utils/fetch-graphql-api.ts | 36 +++++++++++---- .../utils/handle-fetch-response.ts | 2 +- pages/index.tsx | 40 ++++++++++------- pages/product/[slug].tsx | 2 +- 6 files changed, 94 insertions(+), 42 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..0f59d942e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "attach", + "name": "Attach to application", + "skipFiles": ["/**"], + "port": 9229 + } + ] +} diff --git a/framework/woocommerce/api/operations/get-all-products.ts b/framework/woocommerce/api/operations/get-all-products.ts index cdc2510d9..f2192b60e 100644 --- a/framework/woocommerce/api/operations/get-all-products.ts +++ b/framework/woocommerce/api/operations/get-all-products.ts @@ -41,25 +41,35 @@ export default function getAllProductsOperation({ } = {}): Promise { const { fetch, locale } = commerce.getConfig(config) - const { data } = await fetch< - GetAllProductsQuery, - GetAllProductsQueryVariables - >( - query, - { variables }, - { - ...(locale && { - headers: { - 'Accept-Language': locale, - }, - }), + try { + const { data } = await fetch< + GetAllProductsQuery, + GetAllProductsQueryVariables + >( + query, + { variables }, + { + ...(locale && { + headers: { + 'Accept-Language': locale, + }, + }), + } + ) + console.log({ data }) + return { + products: [], } - ) - return { - products: data.products.edges.map(({ node }) => - normalizeProduct(node as WooCommerceProduct) - ), + // return { + // products: data?.products?.edges + // ? data.products.edges.map(({ node }) => + // normalizeProduct(node as WooCommerceProduct) + // ) + // : [], + // } + } catch (e) { + throw e } } diff --git a/framework/woocommerce/api/utils/fetch-graphql-api.ts b/framework/woocommerce/api/utils/fetch-graphql-api.ts index b07c5ec77..23fa05855 100644 --- a/framework/woocommerce/api/utils/fetch-graphql-api.ts +++ b/framework/woocommerce/api/utils/fetch-graphql-api.ts @@ -10,6 +10,22 @@ const fetchGraphqlApi: GraphQLFetcher = async ( fetchOptions ) => { try { + console.log({ + resss: { + API_URL, + ...fetchOptions, + method: 'POST', + headers: { + ...fetchOptions?.headers, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query, + variables, + }), + }, + }) + const res = await fetch(API_URL, { ...fetchOptions, method: 'POST', @@ -26,18 +42,22 @@ const fetchGraphqlApi: GraphQLFetcher = async ( const { data, errors, status } = await res.json() if (errors) { - throw getError(errors, status) + console.log({ errors: errors[0].extensions }) + console.log(getError(errors, status)) } return { data, res } } catch (err) { - throw getError( - [ - { - message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`, - }, - ], - 500 + console.log({ err }) + console.log( + getError( + [ + { + message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`, + }, + ], + 500 + ) ) } } diff --git a/framework/woocommerce/utils/handle-fetch-response.ts b/framework/woocommerce/utils/handle-fetch-response.ts index 91d362d7d..09bd110e1 100644 --- a/framework/woocommerce/utils/handle-fetch-response.ts +++ b/framework/woocommerce/utils/handle-fetch-response.ts @@ -1,7 +1,7 @@ import { FetcherError } from '@commerce/utils/errors' export function getError(errors: any[] | null, status: number) { - errors = errors ?? [{ message: 'Failed to fetch Shopify API' }] + errors = errors ?? [{ message: 'Failed to fetch WooCommerce API' }] return new FetcherError({ errors, status }) } diff --git a/pages/index.tsx b/pages/index.tsx index 620150359..08f537588 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,3 +1,4 @@ +import { useEffect } from 'react' import commerce from '@lib/api/commerce' import { Layout } from '@components/common' import { ProductCard } from '@components/product' @@ -11,26 +12,35 @@ export async function getStaticProps({ locales, }: GetStaticPropsContext) { const config = { locale, locales } - const productsPromise = commerce.getAllProducts({ + const productsPromise = await commerce.getAllProducts({ variables: { first: 6 }, config, preview, - // Saleor provider only - ...({ featured: true } as any), + // // Saleor provider only + // ...({ featured: true } as any), + }) + // // const pagesPromise = commerce.getAllPages({ config, preview }) + // // const siteInfoPromise = commerce.getSiteInfo({ config, preview }) + // // const { products } = await productsPromise + // // const { pages } = await pagesPromise + // // const { categories, brands } = await siteInfoPromise + console.log({ + query: { + variables: { first: 6 }, + config, + preview, + // // Saleor provider only + // ...({ featured: true } as any), + }, + productsPromise, }) - // const pagesPromise = commerce.getAllPages({ config, preview }) - // const siteInfoPromise = commerce.getSiteInfo({ config, preview }) - // const { products } = await productsPromise - // const { pages } = await pagesPromise - // const { categories, brands } = await siteInfoPromise - return { - // props: { - // products, - // categories, - // brands, - // pages, - // }, + props: { + products: [], + // categories, + // brands, + // pages, + }, revalidate: 60, } } diff --git a/pages/product/[slug].tsx b/pages/product/[slug].tsx index bb1ecbee3..7dab6cbc3 100644 --- a/pages/product/[slug].tsx +++ b/pages/product/[slug].tsx @@ -23,7 +23,7 @@ export async function getStaticProps({ preview, }) - const allProductsPromise = commerce.getAllProducts({ + const allProductsPromise = await commerce.getAllProducts({ variables: { first: 4 }, config, preview,