Skip to content

Commit

Permalink
fix: isOneClickBuy button only redirects users after confirmation tha…
Browse files Browse the repository at this point in the history
…t the item was added
  • Loading branch information
Ícaro committed Jul 9, 2021
1 parent 1514788 commit dddba9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Now, when `isOneClickBuy` is enabled, the users are only redirected when there's confirmation that the item was added to the cart

## [0.26.5] - 2021-06-17

### Fixed
Expand Down
14 changes: 10 additions & 4 deletions react/AddToCartButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ interface Props {
// We apply a fake loading to accidental consecutive clicks on the button
const FAKE_LOADING_DURATION = 500

function getFakeLoadingDuration(isOneClickBuy: boolean) {
return isOneClickBuy ? FAKE_LOADING_DURATION * 10 : FAKE_LOADING_DURATION
}

const CSS_HANDLES = [
'buttonText',
'buttonDataContainer',
Expand Down Expand Up @@ -151,10 +155,10 @@ function AddToCartButton(props: Props) {
if (isFakeLoading) {
currentTimers.loading = window.setTimeout(
() => setFakeLoading(false),
FAKE_LOADING_DURATION
getFakeLoadingDuration(isOneClickBuy)
)
}
}, [isFakeLoading])
}, [isFakeLoading, isOneClickBuy])

const resolveToastMessage = (success: boolean) => {
if (!success) return translateMessage(messages.error)
Expand All @@ -172,7 +176,7 @@ function AddToCartButton(props: Props) {
showToast({ message, action })
}

const handleAddToCart: React.MouseEventHandler = event => {
const handleAddToCart = async (event: React.MouseEvent) => {
if (onClickEventPropagation === 'disabled') {
event.stopPropagation()
event.preventDefault()
Expand All @@ -198,7 +202,7 @@ function AddToCartButton(props: Props) {
return
}

addItems(skuItems, {
const addItemsPromise = addItems(skuItems, {
marketingData: { ...utmParams, ...utmiParams },
...options,
})
Expand All @@ -220,6 +224,8 @@ function AddToCartButton(props: Props) {
push(pixelEvent)

if (isOneClickBuy) {
await addItemsPromise

if (
major > 0 &&
(!customOneClickBuyLink || customOneClickBuyLink === checkoutURL)
Expand Down

0 comments on commit dddba9a

Please sign in to comment.