Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/plugin-ecommerce/src/react/provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const defaultContext: EcommerceContextType = {
initiatePayment: async () => {},
isLoading: false,
paymentMethods: [],
refreshCart: async () => {},
removeItem: async () => {},
setCurrency: () => {},
updateAddress: async () => {},
Expand Down Expand Up @@ -249,6 +250,12 @@ export const EcommerceProvider: React.FC<ContextProps> = ({
[baseAPIURL, cartQuery, cartsSlug, cartSecret],
)

const refreshCart = useCallback<EcommerceContextType['refreshCart']>(async () => {
if (!cartID) {return}
const updatedCart = await getCart(cartID)
setCart(updatedCart)
}, [cartID, getCart])

const deleteCart = useCallback(
async (cartID: DefaultDocumentIDType) => {
// Build query params with secret if provided
Expand Down Expand Up @@ -895,6 +902,7 @@ export const EcommerceProvider: React.FC<ContextProps> = ({
initiatePayment,
isLoading,
paymentMethods,
refreshCart,
removeItem,
selectedPaymentMethod,
setCurrency,
Expand Down Expand Up @@ -957,8 +965,7 @@ export const useCurrency = () => {
}

export function useCart<T extends CartsCollection>() {
const { addItem, cart, clearCart, decrementItem, incrementItem, isLoading, removeItem } =
useEcommerce()
const { addItem, cart, clearCart, decrementItem, incrementItem, refreshCart, isLoading, removeItem } = useEcommerce()

if (!addItem) {
throw new Error('useCart must be used within an EcommerceProvider')
Expand All @@ -970,6 +977,7 @@ export function useCart<T extends CartsCollection>() {
clearCart,
decrementItem,
incrementItem,
refreshCart,
isLoading,
removeItem,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-ecommerce/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,10 @@ export type EcommerceContextType<T extends EcommerceCollections = EcommerceColle
*/
isLoading: boolean
paymentMethods: PaymentAdapterClient[]
/**
* Refresh the cart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little light on detail for what I would want to see from this jsdoc

*/
refreshCart: () => Promise<void>
/**
* Remove an item from the cart by its index ID.
*/
Expand Down