-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc579a5
commit 98b8db7
Showing
7 changed files
with
47 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,54 @@ | ||
import {addItemToCart, clearCart, updateItemQuantity} from '@/actions/cart'; | ||
import {Cart, CartItem, Product} from '@coveo/headless-react/ssr-commerce'; | ||
import * as externalCartAPI from '@/actions/external-cart-api'; | ||
import { | ||
Cart as HeadlessCart, | ||
CartItem as HeadlessCartItem, | ||
Product as HeadlessProduct, | ||
} from '@coveo/headless-react/ssr-commerce'; | ||
|
||
type SSRCart = Omit<Cart, 'state' | 'subscribe'>; | ||
type HeadlessSSRCart = Omit<HeadlessCart, 'state' | 'subscribe'>; | ||
|
||
export async function adjustQuantity( | ||
cart: SSRCart, | ||
item: CartItem, | ||
headlessCart: HeadlessSSRCart, | ||
item: HeadlessCartItem, | ||
delta: number | ||
) { | ||
const updatedItem = { | ||
...item, | ||
quantity: item.quantity + delta, | ||
}; | ||
|
||
cart.updateItemQuantity(updatedItem); | ||
await updateItemQuantity(updatedItem); | ||
headlessCart.updateItemQuantity(updatedItem); | ||
// Update the item in the external service | ||
await externalCartAPI.updateItemQuantity(updatedItem); | ||
} | ||
|
||
export async function addToCart(cart: SSRCart, product: Product) { | ||
export async function addToCart( | ||
headlessCart: HeadlessSSRCart, | ||
product: HeadlessProduct | ||
) { | ||
const item = { | ||
name: product.ec_name!, | ||
price: product.ec_price!, | ||
productId: product.ec_product_id!, | ||
quantity: 1, | ||
}; | ||
|
||
cart.updateItemQuantity(item); | ||
await addItemToCart(item); | ||
headlessCart.updateItemQuantity(item); | ||
// Add the item to the external service | ||
await externalCartAPI.addItemToCart(item); | ||
} | ||
|
||
export async function purchase(cart: SSRCart, totalPrice: number) { | ||
cart.purchase({id: crypto.randomUUID(), revenue: totalPrice}); | ||
await clearCart(); | ||
export async function purchase( | ||
headlessCart: HeadlessSSRCart, | ||
totalPrice: number | ||
) { | ||
headlessCart.purchase({id: crypto.randomUUID(), revenue: totalPrice}); | ||
// Clear the cart in the external service | ||
await externalCartAPI.clearCart(); | ||
} | ||
|
||
export async function emptyCart(cart: SSRCart) { | ||
cart.empty(); | ||
await clearCart(); | ||
export async function emptyCart(headlessCart: HeadlessSSRCart) { | ||
headlessCart.empty(); | ||
// Clear the cart in the external service | ||
await externalCartAPI.clearCart(); | ||
} |