diff --git a/components/common/UserNav/UserNav.tsx b/components/common/UserNav/UserNav.tsx index 83422a8cf..2204345ce 100644 --- a/components/common/UserNav/UserNav.tsx +++ b/components/common/UserNav/UserNav.tsx @@ -18,7 +18,8 @@ const countItem = (count: number, item: LineItem) => count + item.quantity const UserNav: FC = ({ className }) => { const { data } = useCart() - const { data: customer } = useCustomer() + let customer; + // const { data: customer } = useCustomer() const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI() const itemsCount = data?.lineItems.reduce(countItem, 0) ?? 0 diff --git a/framework/saleor/cart/use-cart.tsx b/framework/saleor/cart/use-cart.tsx index 823e7ad1f..1abbce011 100644 --- a/framework/saleor/cart/use-cart.tsx +++ b/framework/saleor/cart/use-cart.tsx @@ -30,14 +30,15 @@ export const handler: SWRHook< checkoutId: getCheckoutId().checkoutToken, }, }) - checkout = data.node + + checkout = data; } if (checkout?.completedAt || !checkoutId) { checkout = await checkoutCreate(fetch) } - return checkoutToCart({ checkout }) + return checkoutToCart(checkout); }, useHook: ({ useData }) => (input) => { const response = useData({ diff --git a/framework/saleor/types.ts b/framework/saleor/types.ts index 762135df4..746a3217b 100644 --- a/framework/saleor/types.ts +++ b/framework/saleor/types.ts @@ -1,10 +1,10 @@ import * as Core from '@commerce/types' -import { CheckoutLineItem } from './schema' +import { CheckoutLine } from './schema' export type SaleorCheckout = { id: string webUrl: string - lineItems: CheckoutLineItem[] + lineItems: CheckoutLine[] } export type Cart = Core.Cart & { diff --git a/framework/saleor/utils/checkout-to-cart.ts b/framework/saleor/utils/checkout-to-cart.ts index 034ff11d7..f55787c51 100644 --- a/framework/saleor/utils/checkout-to-cart.ts +++ b/framework/saleor/utils/checkout-to-cart.ts @@ -2,11 +2,10 @@ import { Cart } from '../types' import { CommerceError } from '@commerce/utils/errors' import { - CheckoutLineItemsAddPayload, - CheckoutLineItemsRemovePayload, - CheckoutLineItemsUpdatePayload, - CheckoutCreatePayload, - CheckoutUserError, + CheckoutLinesAdd, + CheckoutLinesUpdate, + CheckoutCreate, + CheckoutError, Checkout, Maybe, } from '../schema' @@ -16,14 +15,13 @@ import throwUserErrors from './throw-user-errors' export type CheckoutQuery = { checkout: Checkout - checkoutUserErrors?: Array + errors?: Array } export type CheckoutPayload = - | CheckoutLineItemsAddPayload - | CheckoutLineItemsUpdatePayload - | CheckoutLineItemsRemovePayload - | CheckoutCreatePayload + | CheckoutLinesAdd + | CheckoutLinesUpdate + | CheckoutCreate | CheckoutQuery const checkoutToCart = (checkoutPayload?: Maybe): Cart => { @@ -34,7 +32,7 @@ const checkoutToCart = (checkoutPayload?: Maybe): Cart => { } const checkout = checkoutPayload?.checkout - throwUserErrors(checkoutPayload?.checkoutUserErrors) + throwUserErrors(checkoutPayload?.errors) if (!checkout) { throw new CommerceError({ diff --git a/framework/saleor/utils/mutations/checkout-line-item-update.ts b/framework/saleor/utils/mutations/checkout-line-item-update.ts index a5b9e51ff..85c5cac5b 100644 --- a/framework/saleor/utils/mutations/checkout-line-item-update.ts +++ b/framework/saleor/utils/mutations/checkout-line-item-update.ts @@ -2,15 +2,15 @@ import { checkoutDetailsFragment } from '../queries/get-checkout-query' const checkoutLineItemUpdateMutation = /* GraphQL */ ` mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineItemUpdateInput!]!) { - checkoutLineItemsUpdate(checkoutId: $checkoutId, lineItems: $lineItems) { - checkoutUserErrors { + checkoutLinesUpdate(checkoutId: $checkoutId, lineItems: $lineItems) { + errors { code field message } - # checkout { - # ${checkoutDetailsFragment} - # } + checkout { + ${checkoutDetailsFragment} + } } } ` diff --git a/framework/saleor/utils/normalize.ts b/framework/saleor/utils/normalize.ts index b439327ea..d48ad8c2f 100644 --- a/framework/saleor/utils/normalize.ts +++ b/framework/saleor/utils/normalize.ts @@ -3,12 +3,7 @@ import { Product } from '@commerce/types' import { Product as SaleorProduct, Checkout, - CheckoutLineItemEdge, - SelectedOption, - ImageConnection, - ProductVariantConnection, - MoneyV2, - ProductOption, + CheckoutLine, Money, } from '../schema' @@ -111,49 +106,46 @@ export function normalizeProduct(productNode: SaleorProduct): Product { } export function normalizeCart(checkout: Checkout): Cart { + const lines = checkout.lines as CheckoutLine[]; + const lineItems: LineItem[] = lines.length > 0 ? lines?.map(normalizeLineItem) : []; + return { id: checkout.id, customerId: '', email: '', - createdAt: checkout.createdAt, + createdAt: checkout.created, currency: { - code: checkout.totalPriceV2?.currencyCode, + code: checkout.totalPrice?.currency! }, - taxesIncluded: checkout.taxesIncluded, - lineItems: checkout.lineItems?.edges.map(normalizeLineItem), - lineItemsSubtotalPrice: +checkout.subtotalPriceV2?.amount, - subtotalPrice: +checkout.subtotalPriceV2?.amount, - totalPrice: checkout.totalPriceV2?.amount, + taxesIncluded: false, + lineItems, + lineItemsSubtotalPrice: 0, + subtotalPrice: 0, + totalPrice: checkout.totalPrice?.gross.amount!, discounts: [], } } -function normalizeLineItem({ - node: { id, title, variant, quantity, ...rest }, -}: CheckoutLineItemEdge): LineItem { +function normalizeLineItem({ id, variant, quantity }: CheckoutLine): LineItem { return { id, variantId: String(variant?.id), productId: String(variant?.id), - name: `${title}`, + name: `${variant.name}`, quantity, variant: { id: String(variant?.id), sku: variant?.sku ?? '', - name: variant?.title!, + name: variant?.name!, image: { - url: variant?.image?.originalSrc ?? '/product-img-placeholder.svg', + url: variant?.media![0].url ?? '/product-img-placeholder.svg', }, - requiresShipping: variant?.requiresShipping ?? false, - price: variant?.priceV2?.amount, - listPrice: variant?.compareAtPriceV2?.amount, + requiresShipping: false, + price: variant?.pricing?.price?.gross.amount!, + listPrice: 0 }, - path: String(variant?.product?.handle), + path: String(variant?.product?.slug), discounts: [], - options: [ - { - value: variant?.title, - }, - ], + options: [ ], } } diff --git a/framework/saleor/utils/queries/get-checkout-query.ts b/framework/saleor/utils/queries/get-checkout-query.ts index d806219ad..a054e6f9f 100644 --- a/framework/saleor/utils/queries/get-checkout-query.ts +++ b/framework/saleor/utils/queries/get-checkout-query.ts @@ -1,6 +1,37 @@ export const checkoutDetailsFragment = ` id token + created + + lines { + id + variant { + id + name + sku + product { + slug + } + media { + url + } + pricing { + price { + gross { + amount + } + } + + } + } + quantity + totalPrice { + currency + gross { + amount + } + } + } ` const getCheckoutQuery = /* GraphQL */ `