forked from crowetic/commerce
fixes
This commit is contained in:
parent
67ed55d114
commit
32184ecdbd
@ -1,53 +1,13 @@
|
|||||||
import { useCallback } from 'react'
|
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 = {
|
export function emptyHook() {
|
||||||
query: '/api/bigcommerce/customers/login',
|
const useEmptyHook = async (options = {}) => {
|
||||||
}
|
return useCallback(async function () {
|
||||||
|
return Promise.resolve()
|
||||||
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',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
return useEmptyHook
|
||||||
...defaultOpts,
|
|
||||||
...options,
|
|
||||||
body: { email, password },
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export default emptyHook
|
||||||
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)
|
|
||||||
|
@ -10,7 +10,6 @@ import checkoutLineItemAddMutation from '../utils/mutations/checkout-line-item-a
|
|||||||
import getCheckoutId from '@framework/utils/get-checkout-id'
|
import getCheckoutId from '@framework/utils/get-checkout-id'
|
||||||
import { checkoutToCart } from './utils'
|
import { checkoutToCart } from './utils'
|
||||||
import { AddCartItemBody, CartItemBody } from '@framework/types'
|
import { AddCartItemBody, CartItemBody } from '@framework/types'
|
||||||
import { AddItemBody } from '../types'
|
|
||||||
import { MutationCheckoutLineItemsAddArgs } from '@framework/schema'
|
import { MutationCheckoutLineItemsAddArgs } from '@framework/schema'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
import { Cart } from '@commerce/types'
|
import { Cart } from '@commerce/types'
|
||||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
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) {
|
if (!checkoutResponse) {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
message: 'Missing checkout details from response cart Response',
|
message: 'Missing checkout details from response cart Response',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const { checkout, userErrors } = checkoutResponse
|
||||||
checkout,
|
|
||||||
userErrors,
|
|
||||||
}: { checkout?: Checkout; userErrors?: any[] } = checkoutResponse
|
|
||||||
|
|
||||||
if (userErrors && userErrors.length) {
|
if (userErrors && userErrors.length) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
@ -26,32 +27,7 @@ const checkoutToCart = (checkoutResponse?: any): Maybe<Cart> => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return normalizeCart(checkout)
|
||||||
...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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkoutToCart
|
export default checkoutToCart
|
||||||
|
@ -64,7 +64,7 @@ export function normalizeProduct(productNode: ShopifyProduct): any {
|
|||||||
} = productNode
|
} = productNode
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: { $set: String(id) },
|
id,
|
||||||
name,
|
name,
|
||||||
vendor,
|
vendor,
|
||||||
description,
|
description,
|
||||||
@ -73,7 +73,7 @@ export function normalizeProduct(productNode: ShopifyProduct): any {
|
|||||||
price: money(priceRange?.minVariantPrice),
|
price: money(priceRange?.minVariantPrice),
|
||||||
images: normalizeProductImages(images),
|
images: normalizeProductImages(images),
|
||||||
variants: variants ? normalizeProductVariants(variants) : null,
|
variants: variants ? normalizeProductVariants(variants) : null,
|
||||||
options: options ? options.map((o) => normalizeProductOption) : [],
|
options: options ? options.map((o) => normalizeProductOption(o)) : [],
|
||||||
...rest,
|
...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 {
|
return {
|
||||||
id: item.id,
|
id,
|
||||||
variantId: String(item.variant?.id),
|
variantId: String(variant?.id),
|
||||||
productId: String(item.variant?.id),
|
productId: String(variant?.id),
|
||||||
name: item.title,
|
name: title,
|
||||||
quantity: item.quantity,
|
quantity: quantity,
|
||||||
variant: {
|
variant: {
|
||||||
id: String(item.variant?.id),
|
id: String(variant?.id),
|
||||||
sku: item.variant?.sku ?? '',
|
sku: variant?.sku ?? '',
|
||||||
name: item.title,
|
name: title,
|
||||||
image: {
|
image: {
|
||||||
url: item.variant?.image?.originalSrc,
|
url: variant?.image?.originalSrc,
|
||||||
},
|
},
|
||||||
requiresShipping: item.variant?.requiresShipping ?? false,
|
requiresShipping: variant?.requiresShipping ?? false,
|
||||||
price: item.variant?.price,
|
price: variant?.price,
|
||||||
listPrice: item.variant?.compareAtPrice,
|
listPrice: variant?.compareAtPrice,
|
||||||
},
|
},
|
||||||
path: '',
|
path: '',
|
||||||
discounts: item.discountAllocations.map(({ value }: any) => ({
|
discounts: item.discountAllocations.map(({ value }: any) => ({
|
||||||
|
@ -11,25 +11,23 @@ type Variables = {
|
|||||||
slug: string
|
slug: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options = {
|
|
||||||
variables: Variables
|
|
||||||
config: ShopifyConfig
|
|
||||||
preview?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReturnType = {
|
type ReturnType = {
|
||||||
product: any
|
product: any
|
||||||
}
|
}
|
||||||
|
|
||||||
const getProduct = async (options: Options): Promise<ReturnType> => {
|
const getProduct = async (options: {
|
||||||
let { config, variables = { first: 250 } } = options ?? {}
|
variables: Variables
|
||||||
|
config: ShopifyConfig
|
||||||
|
preview?: boolean
|
||||||
|
}): Promise<ReturnType> => {
|
||||||
|
let { config, variables } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const { data }: GraphQLFetcherResult = await config.fetch(getProductQuery, {
|
const { data }: GraphQLFetcherResult = await config.fetch(getProductQuery, {
|
||||||
variables,
|
variables,
|
||||||
})
|
})
|
||||||
|
|
||||||
const product = data?.productByHandle?.product
|
const product = data?.productByHandle
|
||||||
|
|
||||||
return {
|
return {
|
||||||
product: product ? normalizeProduct(product) : null,
|
product: product ? normalizeProduct(product) : null,
|
||||||
|
@ -2,7 +2,9 @@ import Cookies from 'js-cookie'
|
|||||||
import { SHOPIFY_CHECKOUT_COOKIE } from '..'
|
import { SHOPIFY_CHECKOUT_COOKIE } from '..'
|
||||||
|
|
||||||
const getCheckoutId = (id?: string) => {
|
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
|
export default getCheckoutId
|
||||||
|
Loading…
x
Reference in New Issue
Block a user