From 3112c7fc80faa1d5a62716825efad231d0136163 Mon Sep 17 00:00:00 2001 From: Loan Laux Date: Mon, 17 May 2021 15:28:04 +0400 Subject: [PATCH] WIP pages implementation & fix add to cart variant ID bug Signed-off-by: Loan Laux --- .../product/ProductView/ProductView.tsx | 2 +- .../reactioncommerce/common/get-all-pages.ts | 40 ++++++++++++------- framework/reactioncommerce/utils/normalize.ts | 4 ++ .../utils/queries/get-all-pages-query.ts | 21 ++++++---- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/components/product/ProductView/ProductView.tsx b/components/product/ProductView/ProductView.tsx index 072f6e298..16c02ca51 100644 --- a/components/product/ProductView/ProductView.tsx +++ b/components/product/ProductView/ProductView.tsx @@ -49,7 +49,7 @@ const ProductView: FC = ({ product }) => { await addItem({ productId: String(product.id), - variantId: String(selectedVariant.id), + variantId: String(selectedVariant.sku), pricing: { amount: selectedVariant.price, currencyCode: product.price.currencyCode, diff --git a/framework/reactioncommerce/common/get-all-pages.ts b/framework/reactioncommerce/common/get-all-pages.ts index b844bc2a5..b7495c7ac 100644 --- a/framework/reactioncommerce/common/get-all-pages.ts +++ b/framework/reactioncommerce/common/get-all-pages.ts @@ -1,5 +1,5 @@ import { getConfig, ReactionCommerceConfig } from '../api' -import { PageEdge } from '../schema' +import { NavigationTreeItem } from '../schema' import { getAllPagesQuery } from '../utils/queries' type Variables = { @@ -23,20 +23,32 @@ const getAllPages = async (options?: { config: ReactionCommerceConfig preview?: boolean }): Promise => { - // let { config, variables = { first: 250 } } = options ?? {} - // config = getConfig(config) - // const { locale } = config - // const { data } = await config.fetch(getAllPagesQuery, { variables }) - // - // const pages = data.pages?.edges?.map( - // ({ node: { title: name, handle, ...node } }: PageEdge) => ({ - // ...node, - // url: `/${locale}/${handle}`, - // name, - // }) - // ) + let { config, variables } = options ?? {} + config = getConfig(config) + const { locale } = config + console.log('locale', locale) + const { data } = await config.fetch(getAllPagesQuery, { + variables: { + ...variables, + shopId: config.shopId, + }, + }) - return { pages: [] } + const pages = data.shop?.defaultNavigationTree?.items?.map( + ({ + navigationItem: { + _id: id, + data: { contentForLanguage: name, url }, + }, + }: NavigationTreeItem) => ({ + id, + url, + name, + body: '', + }) + ) + + return { pages } } export default getAllPages diff --git a/framework/reactioncommerce/utils/normalize.ts b/framework/reactioncommerce/utils/normalize.ts index 9155d8c43..296832e6e 100644 --- a/framework/reactioncommerce/utils/normalize.ts +++ b/framework/reactioncommerce/utils/normalize.ts @@ -210,6 +210,10 @@ function normalizeLineItem({ } export function normalizeCustomer(viewer: Account): Customer { + if (!viewer) { + return {} + } + return { firstName: viewer.firstName ?? '', lastName: viewer.lastName ?? '', diff --git a/framework/reactioncommerce/utils/queries/get-all-pages-query.ts b/framework/reactioncommerce/utils/queries/get-all-pages-query.ts index e3aee1f10..838535634 100644 --- a/framework/reactioncommerce/utils/queries/get-all-pages-query.ts +++ b/framework/reactioncommerce/utils/queries/get-all-pages-query.ts @@ -1,11 +1,18 @@ export const getAllPagesQuery = /* GraphQL */ ` - query getAllPages($first: Int = 250) { - pages(first: $first) { - edges { - node { - id - title - handle + query getAllPages($shopId: ID!, $language: String! = "en") { + shop(id: $shopId) { + defaultNavigationTree(language: $language) { + items { + navigationItem { + _id + data { + contentForLanguage + classNames + url + isUrlRelative + shouldOpenInNewWindow + } + } } } }