Skip to content

Commit

Permalink
fix: checkout link is disabled when cart is emptied
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Dec 7, 2022
1 parent 15836be commit d1bc54a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
11 changes: 10 additions & 1 deletion packages/drop-in/src/apis/commercelayer/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getKeyForCart } from '#apis/storage'
import { pDebounce } from '#utils/promise'
import type { Order } from '@commercelayer/sdk'
import Cookies from 'js-cookie'
import memoize from 'lodash/memoize'

/**
* Create a draft order.
Expand Down Expand Up @@ -117,7 +118,7 @@ export async function _getCart(): Promise<Order | null> {
return order
}

export const getCart = pDebounce(_getCart, { wait: 50, maxWait: 100 })
export const getCart = memoize(pDebounce(_getCart, { wait: 10, maxWait: 50 }))

export interface TriggerCartUpdateEvent {
/** Order */
Expand Down Expand Up @@ -150,6 +151,10 @@ export interface TriggerHostedCartUpdateEvent {
* @param iframeId iFrame ID who triggered the event
*/
export async function triggerHostedCartUpdate(iframeId: string): Promise<void> {
if (getCart.cache.clear !== undefined) {
getCart.cache.clear()
}

const order = await getCart()

if (order !== null) {
Expand All @@ -176,6 +181,10 @@ export async function addItem(sku: string, quantity: number): Promise<void> {
_update_quantity: true
})

if (getCart.cache.clear !== undefined) {
getCart.cache.clear()
}

await triggerCartUpdate()
}

Expand Down
2 changes: 1 addition & 1 deletion packages/drop-in/src/apis/commercelayer/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const _getPrices = async (skus: string[]): Promise<PriceList> => {
return prices
}

const getPrices = pDebounce(_getPrices, { wait: 50, maxWait: 100 })
const getPrices = pDebounce(_getPrices, { wait: 10, maxWait: 50 })

export const getPrice = memoize(
async (sku: string): Promise<Price | undefined> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/drop-in/src/apis/commercelayer/skus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const _getSkuIds = async (skus: string[]): Promise<SkuIdList> => {
return ids
}

const getSkuIds = pDebounce(_getSkuIds, { wait: 50, maxWait: 100 })
const getSkuIds = pDebounce(_getSkuIds, { wait: 10, maxWait: 50 })

export const getSkuId = memoize(
async (sku: string): Promise<string | undefined> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
getCheckoutUrl,
TriggerCartUpdateEvent
TriggerCartUpdateEvent,
TriggerHostedCartUpdateEvent
} from '#apis/commercelayer/cart'
import {
Component,
Expand Down Expand Up @@ -34,9 +35,27 @@ export class ClCheckoutLink implements Props {

@Listen('cartUpdate', { target: 'window' })
async cartUpdateHandler(
_event: CustomEvent<TriggerCartUpdateEvent>
event: CustomEvent<TriggerCartUpdateEvent>
): Promise<void> {
this.href = await getCheckoutUrl()
if (
this.href === undefined &&
event.detail.order.skus_count !== undefined &&
event.detail.order.skus_count > 0
) {
this.href = await getCheckoutUrl()
}
}

@Listen('hostedCartUpdate', { target: 'window' })
async hostedCartUpdateHandler(
event: CustomEvent<TriggerHostedCartUpdateEvent>
): Promise<void> {
if (
event.detail.order.skus_count === undefined ||
event.detail.order.skus_count === 0
) {
this.href = undefined
}
}

render(): JSX.Element {
Expand Down
4 changes: 2 additions & 2 deletions packages/drop-in/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<path stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7.813 18h15.7a1.988 1.988 0 0 0 1.962-1.637L27 8H6"/>
</svg>
<cl-cart-count aria-hidden="true"></cl-cart-count>
<cl-cart></cl-cart>
<cl-cart open-on-add></cl-cart>
</cl-cart-link>

<cl-cart-link target="_blank" class="ml-4">
Expand All @@ -83,7 +83,7 @@
Checkout
</cl-checkout-link>

<cl-cart type="mini" open-on-add></cl-cart>
<!-- <cl-cart type="mini" open-on-add></cl-cart> -->
</div>
</nav>

Expand Down

0 comments on commit d1bc54a

Please sign in to comment.