fix typescript

Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
Chloe 2023-03-10 10:46:05 +07:00
parent ea7714d9b6
commit 2fd671e53f
16 changed files with 78 additions and 203 deletions

View File

@ -1,14 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { UseCheckout } from '@vercel/commerce/checkout/use-checkout' export { default } from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -28,6 +28,20 @@ export interface Checkout {
* List of items in the checkout. * List of items in the checkout.
*/ */
lineItems?: LineItem[] lineItems?: LineItem[]
/**
* List of available shipping methods
*/
shippingMethods?: {
id: string
fee: string
name: string
}[]
/**
* The unique identifier of the shipping method that the customer has selected.
*/
selectedShippingMethodId?: string
} }
export interface CheckoutBody { export interface CheckoutBody {

View File

@ -1,14 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { UseCheckout } from '@vercel/commerce/checkout/use-checkout' export { default } from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,10 +1,10 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api' import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import checkoutEndpoint from '@vercel/commerce/api/endpoints/checkout' import checkoutEndpoint from '@vercel/commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '../../../types/checkout'
import type { OpenCommerceAPI } from '../..' import type { OpenCommerceAPI } from '../..'
import submitCheckout from './submit-checkout' import submitCheckout from './submit-checkout'
import getCheckout from './get-checkout' import getCheckout from './get-checkout'
import { CheckoutSchema } from '@vercel/commerce/types/checkout'
export type CheckoutAPI = GetAPISchema<OpenCommerceAPI, CheckoutSchema> export type CheckoutAPI = GetAPISchema<OpenCommerceAPI, CheckoutSchema>

View File

@ -7,11 +7,11 @@ import useCheckout, {
import useSubmitCheckout from './use-submit-checkout' import useSubmitCheckout from './use-submit-checkout'
import { useCheckoutContext } from '@components/checkout/context' import { useCheckoutContext } from '@components/checkout/context'
import { AddressFields } from '../types/customer/address' import { AddressFields } from '../types/customer/address'
import { GetCheckoutHook } from '../types/checkout'
import { FulfillmentGroup } from '../types/cart' import { FulfillmentGroup } from '../types/cart'
import { API_URL, OPENCOMMERCE_ANONYMOUS_CART_TOKEN_COOKIE } from '../const' import { API_URL, OPENCOMMERCE_ANONYMOUS_CART_TOKEN_COOKIE } from '../const'
import getAnonymousCartQuery from '../api/queries/get-anonymous-cart' import getAnonymousCartQuery from '../api/queries/get-anonymous-cart'
import { normalizeCheckout } from '../utils/normalize' import { normalizeCheckout } from '../utils/normalize'
import { GetCheckoutHook } from '@vercel/commerce/types/checkout'
export default useCheckout as UseCheckout<typeof handler> export default useCheckout as UseCheckout<typeof handler>
@ -40,23 +40,26 @@ export const handler: SWRHook<GetCheckoutHook> = {
(group) => group.type === 'shipping' (group) => group.type === 'shipping'
) )
const hasShippingMethods =
!!shippingTypeFulfillment?.availableFulfillmentOptions?.length
const shippingGroup = checkout.fulfillmentGroups.find( const shippingGroup = checkout.fulfillmentGroups.find(
(group: FulfillmentGroup) => group?.type === 'shipping' (group: FulfillmentGroup) => group?.type === 'shipping'
) )
const totalDisplayAmount = const shippingAmount =
checkout.summary.fulfillmentTotal?.displayAmount || '0' checkout.summary.fulfillmentTotal?.displayAmount || '0'
return { return {
hasPayment: true, hasPayment: true,
hasShippingMethods, selectedShippingMethodId:
shippingGroup, shippingGroup?.selectedFulfillmentOption?.fulfillmentMethod?._id,
shippingMethods: shippingGroup?.availableFulfillmentOptions
?.filter((opt) => !!opt.fulfillmentMethod)
.map(({ fulfillmentMethod, price }) => ({
name: fulfillmentMethod!.displayName,
id: fulfillmentMethod!._id,
fee: price.displayAmount,
})),
hasShipping: false, hasShipping: false,
addressId: 'addressId', addressId: 'addressId',
totalDisplayAmount,
} }
} }
@ -85,7 +88,7 @@ export const handler: SWRHook<GetCheckoutHook> = {
data: { data: {
...response.data, ...response.data,
hasShipping: hasEnteredAddress, hasShipping: hasEnteredAddress,
hasSelectedShippingMethod: !!shippingMethodId, selectedShippingMethodId: shippingMethodId,
}, },
submit: () => submit, submit: () => submit,
isEmpty: response.data.lineItems?.length ?? 0, isEmpty: response.data.lineItems?.length ?? 0,

View File

@ -28,7 +28,6 @@ export const handler: MutationHook<UpdateAddressItemHook> = {
input: { input: {
cartId, cartId,
cartToken, cartToken,
fulfillmentGroupId: item.fulfillmentGroupId,
fulfillmentMethodId: item.shippingMethodId, fulfillmentMethodId: item.shippingMethodId,
}, },
}, },
@ -44,15 +43,12 @@ export const handler: MutationHook<UpdateAddressItemHook> = {
return useCallback( return useCallback(
async function updateItem(input) { async function updateItem(input) {
const { id, ...rest } = input const { id, ...rest } = input
const fulfillmentGroupId =
checkoutData?.shippingGroup?._id || 'groupId'
await fetch({ await fetch({
input: { item: { ...rest, fulfillmentGroupId }, itemId: id }, input: { item: { ...rest }, itemId: id },
}) })
setAddressFields({ setAddressFields({
...addressFields, ...addressFields,
shippingMethodId: rest.shippingMethodId, shippingMethodId: rest.shippingMethodId,
fulfillmentGroupId,
}) })
return null return null
}, },

View File

@ -1,18 +0,0 @@
export * from '@vercel/commerce/types/checkout'
import {
GetCheckoutHook as CoreGetCheckoutHook,
Checkout,
} from '@vercel/commerce/types/checkout'
import { FulfillmentGroup } from './cart'
export type GetCheckoutHook = Omit<CoreGetCheckoutHook, 'data'> & {
data:
| (Checkout & {
hasShippingMethods?: boolean
hasSelectedShippingMethod?: boolean
totalDisplayAmount?: string
shippingGroup?: FulfillmentGroup
})
| null
}

View File

@ -4,7 +4,6 @@ export * from '@vercel/commerce/types/customer/address'
export type AddressFields = Core.AddressFields & { export type AddressFields = Core.AddressFields & {
shippingMethodId?: string shippingMethodId?: string
fulfillmentGroupId?: string
} }
export type UpdateAddressItemHook = Omit< export type UpdateAddressItemHook = Omit<

View File

@ -1,14 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { UseCheckout } from '@vercel/commerce/checkout/use-checkout' export { default } from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,16 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { export { default } from '@vercel/commerce/checkout/use-checkout'
UseCheckout,
} from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,16 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { export { default } from '@vercel/commerce/checkout/use-checkout'
UseCheckout,
} from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,19 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { export { default } from '@vercel/commerce/checkout/use-checkout'
UseCheckout,
} from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
// Provide fetchOptions for SWR cache key
fetchOptions: {
// TODO: Revise url and query
url: 'checkout',
query: 'show',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,16 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { export { default } from '@vercel/commerce/checkout/use-checkout'
UseCheckout,
} from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -1,16 +1,2 @@
import { SWRHook } from '@vercel/commerce/utils/types' export * from '@vercel/commerce/checkout/use-checkout'
import useCheckout, { export { default } from '@vercel/commerce/checkout/use-checkout'
UseCheckout,
} from '@vercel/commerce/checkout/use-checkout'
export default useCheckout as UseCheckout<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook:
({ useData }) =>
async (input) => ({}),
}

View File

@ -74,12 +74,12 @@ const CheckoutSidebarView: FC = () => {
onClick={() => setSidebarView('SHIPPING_VIEW')} onClick={() => setSidebarView('SHIPPING_VIEW')}
/> />
{checkoutData?.hasShippingMethods && ( {checkoutData?.shippingMethods?.length ? (
<ShippingMethodWidget <ShippingMethodWidget
isValid={checkoutData?.hasSelectedShippingMethod} isValid={!!checkoutData?.selectedShippingMethodId}
onClick={() => setSidebarView('SHIPPING_METHOD_VIEW')} onClick={() => setSidebarView('SHIPPING_METHOD_VIEW')}
/> />
)} ) : null}
<ul className={s.lineItemsList}> <ul className={s.lineItemsList}>
{cartData!.lineItems.map((item: any) => ( {cartData!.lineItems.map((item: any) => (
@ -106,10 +106,15 @@ const CheckoutSidebarView: FC = () => {
<span>Taxes</span> <span>Taxes</span>
<span>Calculated at checkout</span> <span>Calculated at checkout</span>
</li> </li>
{checkoutData?.hasSelectedShippingMethod ? ( {checkoutData?.selectedShippingMethodId ? (
<li className="flex justify-between py-1"> <li className="flex justify-between py-1">
<span>Shipping</span> <span>Shipping</span>
<span>{checkoutData?.totalDisplayAmount}</span> <span>
{checkoutData?.shippingMethods?.find(
(method) =>
method.id === checkoutData.selectedShippingMethodId
)?.fee ?? 0}
</span>
</li> </li>
) : ( ) : (
<li className="flex justify-between py-1"> <li className="flex justify-between py-1">

View File

@ -6,16 +6,6 @@ import { Button } from '@components/ui'
import useCheckout from '@framework/checkout/use-checkout' import useCheckout from '@framework/checkout/use-checkout'
import { useCheckoutContext } from '../context' import { useCheckoutContext } from '../context'
type FulfillmentOption = {
fulfillmentMethod?: {
_id: string
displayName: string
}
price: {
displayAmount: string
}
}
const ShippingMethod = () => { const ShippingMethod = () => {
const { setSidebarView } = useUI() const { setSidebarView } = useUI()
@ -31,13 +21,15 @@ const ShippingMethod = () => {
await updateShippingMethod({ await updateShippingMethod({
id: cart!.id, id: cart!.id,
...addressFields, ...addressFields,
...(event.target.shippingMethod.value ? {shippingMethodId: event.target.shippingMethod.value} : {}) ...(event.target.shippingMethod.value
? { shippingMethodId: event.target.shippingMethod.value }
: {}),
}) })
setSidebarView('CHECKOUT_VIEW') setSidebarView('CHECKOUT_VIEW')
} }
return checkoutData?.shippingGroup ? ( return checkoutData?.shippingMethods ? (
<form className="h-full" onSubmit={handleSubmit}> <form className="h-full" onSubmit={handleSubmit}>
<SidebarLayout handleBack={() => setSidebarView('CHECKOUT_VIEW')}> <SidebarLayout handleBack={() => setSidebarView('CHECKOUT_VIEW')}>
<div className="px-4 sm:px-6 flex-1"> <div className="px-4 sm:px-6 flex-1">
@ -45,33 +37,26 @@ const ShippingMethod = () => {
Shipping Methods Shipping Methods
</h2> </h2>
<div> <div>
{checkoutData.shippingGroup.availableFulfillmentOptions?.map( {checkoutData.shippingMethods.map((option) => (
(option: FulfillmentOption) => ( <div
<div className="flex flex-row my-3 items-center justify-between"
className="flex flex-row my-3 items-center justify-between" key={option.id}
key={option?.fulfillmentMethod?._id} >
> <fieldset className="flex flex-row items-center">
<fieldset className="flex flex-row items-center"> <input
<input name="shippingMethod"
name="shippingMethod" className="bg-black"
className="bg-black" type="radio"
type="radio" value={option.id}
value={option?.fulfillmentMethod?._id} defaultChecked={
defaultChecked={ checkoutData.selectedShippingMethodId === option.id
checkoutData.shippingGroup?.selectedFulfillmentOption }
?.fulfillmentMethod?._id === />
option?.fulfillmentMethod?._id <span className="ml-3 text-sm">{option.name}</span>
} </fieldset>
/> <span>{option.fee}</span>
<span className="ml-3 text-sm"> </div>
{option?.fulfillmentMethod?.displayName || ))}
'Shipping Method'}
</span>
</fieldset>
<span>{option?.price.displayAmount}</span>
</div>
)
)}
</div> </div>
</div> </div>
<div className="sticky z-20 bottom-0 w-full right-0 left-0 py-12 bg-accent-0 border-t border-accent-2 px-6"> <div className="sticky z-20 bottom-0 w-full right-0 left-0 py-12 bg-accent-0 border-t border-accent-2 px-6">