diff --git a/framework/shopify/auth/use-login.tsx b/framework/shopify/auth/use-login.tsx index 384599fe9..75f067c3a 100644 --- a/framework/shopify/auth/use-login.tsx +++ b/framework/shopify/auth/use-login.tsx @@ -1,53 +1,13 @@ import { useCallback } from 'react' -import type { HookFetcher } from '@commerce/utils/types' -import { CommerceError } from '@commerce/utils/errors' -import useCommerceLogin from '@commerce/use-login' -import type { LoginBody } from '../api/customers/login' -import useCustomer from '../customer/use-customer' -const defaultOpts = { - query: '/api/bigcommerce/customers/login', -} - -export type LoginInput = LoginBody - -export const fetcher: HookFetcher = ( - options, - { email, password }, - fetch -) => { - if (!(email && password)) { - throw new CommerceError({ - message: - 'A first name, last name, email and password are required to login', - }) +export function emptyHook() { + const useEmptyHook = async (options = {}) => { + return useCallback(async function () { + return Promise.resolve() + }, []) } - return fetch({ - ...defaultOpts, - ...options, - body: { email, password }, - }) + return useEmptyHook } -export function extendHook(customFetcher: typeof fetcher) { - const useLogin = () => { - const { revalidate } = useCustomer() - const fn = useCommerceLogin(defaultOpts, customFetcher) - - return useCallback( - async function login(input: LoginInput) { - const data = await fn(input) - await revalidate() - return data - }, - [fn] - ) - } - - useLogin.extend = extendHook - - return useLogin -} - -export default extendHook(fetcher) +export default emptyHook diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index b0e9444bf..313bffde7 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -10,7 +10,6 @@ import checkoutLineItemAddMutation from '../utils/mutations/checkout-line-item-a import getCheckoutId from '@framework/utils/get-checkout-id' import { checkoutToCart } from './utils' import { AddCartItemBody, CartItemBody } from '@framework/types' -import { AddItemBody } from '../types' import { MutationCheckoutLineItemsAddArgs } from '@framework/schema' const defaultOpts = { diff --git a/framework/shopify/cart/utils/checkout-to-cart.ts b/framework/shopify/cart/utils/checkout-to-cart.ts index c7f0e2783..b38738e2f 100644 --- a/framework/shopify/cart/utils/checkout-to-cart.ts +++ b/framework/shopify/cart/utils/checkout-to-cart.ts @@ -1,18 +1,19 @@ import { Cart } from '@commerce/types' import { CommerceError, ValidationError } from '@commerce/utils/errors' -import { Checkout, CheckoutLineItemEdge, Maybe } from '@framework/schema' +import { normalizeCart } from '@framework/lib/normalize' +import { Checkout, Maybe, UserError } from '@framework/schema' -const checkoutToCart = (checkoutResponse?: any): Maybe => { +const checkoutToCart = (checkoutResponse?: { + checkout: Checkout + userErrors?: UserError[] +}): Maybe => { if (!checkoutResponse) { throw new CommerceError({ message: 'Missing checkout details from response cart Response', }) } - const { - checkout, - userErrors, - }: { checkout?: Checkout; userErrors?: any[] } = checkoutResponse + const { checkout, userErrors } = checkoutResponse if (userErrors && userErrors.length) { throw new ValidationError({ @@ -26,32 +27,7 @@ const checkoutToCart = (checkoutResponse?: any): Maybe => { }) } - return { - ...checkout, - currency: { code: checkout.currencyCode }, - lineItems: checkout.lineItems?.edges.map( - ({ - node: { id, title: name, quantity, variant }, - }: CheckoutLineItemEdge) => ({ - id, - checkoutUrl: checkout.webUrl, - variantId: variant?.id, - productId: id, - name, - quantity, - discounts: [], - path: '', - variant: { - id: variant?.id, - image: { - url: variant?.image?.src, - altText: variant?.title, - }, - price: variant?.price, - }, - }) - ), - } + return normalizeCart(checkout) } export default checkoutToCart diff --git a/framework/shopify/lib/normalize.ts b/framework/shopify/lib/normalize.ts index 027694f20..e2670cb3b 100644 --- a/framework/shopify/lib/normalize.ts +++ b/framework/shopify/lib/normalize.ts @@ -64,7 +64,7 @@ export function normalizeProduct(productNode: ShopifyProduct): any { } = productNode return { - id: { $set: String(id) }, + id, name, vendor, description, @@ -73,7 +73,7 @@ export function normalizeProduct(productNode: ShopifyProduct): any { price: money(priceRange?.minVariantPrice), images: normalizeProductImages(images), variants: variants ? normalizeProductVariants(variants) : null, - options: options ? options.map((o) => normalizeProductOption) : [], + options: options ? options.map((o) => normalizeProductOption(o)) : [], ...rest, } } @@ -98,23 +98,25 @@ export function normalizeCart(data: Checkout): Cart { } } -function normalizeLineItem({ node: item }: CheckoutLineItemEdge): LineItem { +function normalizeLineItem({ + node: { id, title, variant, quantity, ...item }, +}: CheckoutLineItemEdge): LineItem { return { - id: item.id, - variantId: String(item.variant?.id), - productId: String(item.variant?.id), - name: item.title, - quantity: item.quantity, + id, + variantId: String(variant?.id), + productId: String(variant?.id), + name: title, + quantity: quantity, variant: { - id: String(item.variant?.id), - sku: item.variant?.sku ?? '', - name: item.title, + id: String(variant?.id), + sku: variant?.sku ?? '', + name: title, image: { - url: item.variant?.image?.originalSrc, + url: variant?.image?.originalSrc, }, - requiresShipping: item.variant?.requiresShipping ?? false, - price: item.variant?.price, - listPrice: item.variant?.compareAtPrice, + requiresShipping: variant?.requiresShipping ?? false, + price: variant?.price, + listPrice: variant?.compareAtPrice, }, path: '', discounts: item.discountAllocations.map(({ value }: any) => ({ diff --git a/framework/shopify/product/get-product.ts b/framework/shopify/product/get-product.ts index ba4856520..84e74c611 100644 --- a/framework/shopify/product/get-product.ts +++ b/framework/shopify/product/get-product.ts @@ -11,25 +11,23 @@ type Variables = { slug: string } -type Options = { - variables: Variables - config: ShopifyConfig - preview?: boolean -} - type ReturnType = { product: any } -const getProduct = async (options: Options): Promise => { - let { config, variables = { first: 250 } } = options ?? {} +const getProduct = async (options: { + variables: Variables + config: ShopifyConfig + preview?: boolean +}): Promise => { + let { config, variables } = options ?? {} config = getConfig(config) const { data }: GraphQLFetcherResult = await config.fetch(getProductQuery, { variables, }) - const product = data?.productByHandle?.product + const product = data?.productByHandle return { product: product ? normalizeProduct(product) : null, diff --git a/framework/shopify/utils/get-checkout-id.ts b/framework/shopify/utils/get-checkout-id.ts index 5ec9bb1ea..4f5b4f7b3 100644 --- a/framework/shopify/utils/get-checkout-id.ts +++ b/framework/shopify/utils/get-checkout-id.ts @@ -2,7 +2,9 @@ import Cookies from 'js-cookie' import { SHOPIFY_CHECKOUT_COOKIE } from '..' const getCheckoutId = (id?: string) => { - return id ?? Cookies.get(SHOPIFY_CHECKOUT_COOKIE) + const checkoutID = id ?? Cookies.get(SHOPIFY_CHECKOUT_COOKIE) + console.log(checkoutID) + return checkoutID } export default getCheckoutId