Skip to content

Commit

Permalink
Merge pull request #6 from commercelayer/fix-checkout-link
Browse files Browse the repository at this point in the history
Checkout link is disabled when cart is emptied
  • Loading branch information
marcomontalbano authored Dec 7, 2022
2 parents a4d5c64 + d1bc54a commit c308bc1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
5 changes: 4 additions & 1 deletion packages/docs/stories/introduction.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Canvas, Meta, Story, ArgsTable } from '@storybook/addon-docs'
import { linkTo } from '@storybook/addon-links'
import { meta, Basic } from './introduction.stories.ts';

<Meta title={meta.title} />
<Meta
title={meta.title}
argTypes={meta.argTypes}
/>

# Drop-in library

Expand Down
15 changes: 12 additions & 3 deletions packages/docs/stories/introduction.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { codes } from './assets/constants'
type Args = GlobalArgs & {}

export const meta: Meta<Args> = {
title: 'Introduction'
title: 'Introduction',
argTypes: {
'Use minicart.css': {
table: {
disable: false
}
}
}
}

export const Basic: StoryFn<Args> = () => {
Expand All @@ -27,7 +34,8 @@ export const Basic: StoryFn<Args> = () => {
<path stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M23 23H8.725L5.238 3.825A1 1 0 0 0 4.261 3H2M10 28a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5ZM23 28a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z"/>
<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></cl-cart-count>
<cl-cart-count hide-when-empty></cl-cart-count>
<cl-cart open-on-add></cl-cart>
</cl-cart-link>
</div>
</nav>
Expand Down Expand Up @@ -133,7 +141,8 @@ export const Basic: StoryFn<Args> = () => {
}

Basic.args = {
'Use drop-in.css': true
'Use drop-in.css': true,
'Use minicart.css': true
}

// Basic.parameters = {
Expand Down
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 c308bc1

Please sign in to comment.