diff --git a/CHANGELOG.md b/CHANGELOG.md index e34beeb..65cf7ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/react/AddToCartButton.tsx b/react/AddToCartButton.tsx index 654ec97..561275e 100644 --- a/react/AddToCartButton.tsx +++ b/react/AddToCartButton.tsx @@ -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', @@ -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) @@ -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() @@ -198,7 +202,7 @@ function AddToCartButton(props: Props) { return } - addItems(skuItems, { + const addItemsPromise = addItems(skuItems, { marketingData: { ...utmParams, ...utmiParams }, ...options, }) @@ -220,6 +224,8 @@ function AddToCartButton(props: Props) { push(pixelEvent) if (isOneClickBuy) { + await addItemsPromise + if ( major > 0 && (!customOneClickBuyLink || customOneClickBuyLink === checkoutURL)