mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { useMemo } from 'react'
|
|
import { SWRHook } from '@commerce/utils/types'
|
|
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
|
import { Order } from '@commercelayer/js-sdk'
|
|
import getCredentials from '@framework/api/utils/getCredentials'
|
|
import normalizeLineItems from '../api/utils/normalizeLineItems'
|
|
|
|
export default useCart as UseCart<typeof handler>
|
|
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
query: '',
|
|
},
|
|
async fetcher() {
|
|
const id = localStorage.getItem('CL_ORDER_ID') || ''
|
|
const credentials = getCredentials()
|
|
if (id && credentials.accessToken) {
|
|
const clOrder = await Order.withCredentials(credentials)
|
|
.includes('lineItems')
|
|
.find(id, { rawResponse: true })
|
|
const attributes = clOrder.data.attributes
|
|
const lineItems = clOrder?.included
|
|
? normalizeLineItems(clOrder?.included)
|
|
: []
|
|
return {
|
|
id,
|
|
createdAt: attributes.created_at,
|
|
currency: { code: attributes.currency_code },
|
|
taxesIncluded: '',
|
|
lineItems,
|
|
lineItemsSubtotalPrice: '',
|
|
subtotalPrice: attributes.subtotal_amount_float,
|
|
totalPrice: attributes.total_amount_float,
|
|
}
|
|
}
|
|
return {
|
|
id: '',
|
|
createdAt: '',
|
|
currency: { code: '' },
|
|
taxesIncluded: '',
|
|
lineItems: [],
|
|
lineItemsSubtotalPrice: '',
|
|
subtotalPrice: 0,
|
|
totalPrice: 0,
|
|
}
|
|
},
|
|
useHook:
|
|
({ useData }) =>
|
|
() => {
|
|
const response = useData()
|
|
return useMemo(
|
|
() =>
|
|
Object.create(response, {
|
|
isEmpty: {
|
|
get() {
|
|
return response.data?.lineItems.length === 0
|
|
},
|
|
enumerable: true,
|
|
},
|
|
}),
|
|
[response]
|
|
)
|
|
},
|
|
}
|