mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
fixes
This commit is contained in:
parent
67ed55d114
commit
32184ecdbd
@ -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<null, LoginBody> = (
|
||||
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<null, LoginInput>(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
|
||||
|
@ -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 = {
|
||||
|
@ -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<Cart> => {
|
||||
const checkoutToCart = (checkoutResponse?: {
|
||||
checkout: Checkout
|
||||
userErrors?: UserError[]
|
||||
}): Maybe<Cart> => {
|
||||
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<Cart> => {
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -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) => ({
|
||||
|
@ -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<ReturnType> => {
|
||||
let { config, variables = { first: 250 } } = options ?? {}
|
||||
const getProduct = async (options: {
|
||||
variables: Variables
|
||||
config: ShopifyConfig
|
||||
preview?: boolean
|
||||
}): Promise<ReturnType> => {
|
||||
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,
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user