mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import type { GetCheckoutHook } from '@commerce/types/checkout'
|
|
|
|
import { useMemo } from 'react'
|
|
import { SWRHook } from '@commerce/utils/types'
|
|
import useCheckout, { UseCheckout } from '@commerce/checkout/use-checkout'
|
|
import useSubmitCheckout from './use-submit-checkout'
|
|
import { useCheckoutContext } from '@components/checkout/context'
|
|
|
|
export default useCheckout as UseCheckout<typeof handler>
|
|
|
|
export const handler: SWRHook<GetCheckoutHook> = {
|
|
fetchOptions: {
|
|
query: '_',
|
|
method: '_',
|
|
},
|
|
useHook: () =>
|
|
function useHook() {
|
|
const { cardFields, addressFields } = useCheckoutContext()
|
|
const submit = useSubmitCheckout()
|
|
|
|
// Basic validation - check that at least one field has a value.
|
|
const hasEnteredCard = Object.values(cardFields).some(
|
|
(fieldValue) => !!fieldValue
|
|
)
|
|
const hasEnteredAddress = Object.values(addressFields).some(
|
|
(fieldValue) => !!fieldValue
|
|
)
|
|
|
|
const response = useMemo(
|
|
() => ({
|
|
data: {
|
|
hasPayment: hasEnteredCard,
|
|
hasShipping: hasEnteredAddress,
|
|
},
|
|
}),
|
|
[hasEnteredCard, hasEnteredAddress]
|
|
)
|
|
|
|
return useMemo(
|
|
() =>
|
|
Object.create(response, {
|
|
submit: {
|
|
get() {
|
|
return submit
|
|
},
|
|
enumerable: true,
|
|
},
|
|
}),
|
|
[submit, response]
|
|
)
|
|
},
|
|
}
|