Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent modal to open when it fails to add an item to cart #1053

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {useCurrency} from '../../hooks'
import {Skeleton as ImageGallerySkeleton} from '../../components/image-gallery'
import {HideOnDesktop, HideOnMobile} from '../../components/responsive'
import QuantityPicker from '../../components/quantity-picker'
import {useToast} from '../../hooks/use-toast'
import {API_ERROR_MESSAGE} from '../../constants'

const ProductViewHeader = ({name, price, currency, category, productType}) => {
const intl = useIntl()
Expand Down Expand Up @@ -167,6 +169,14 @@ const ProductView = forwardRef(
})
}

const showToast = useToast()
const showError = () => {
showToast({
title: intl.formatMessage(API_ERROR_MESSAGE),
status: 'error'
})
}

const handleCartItem = async () => {
const hasValidSelection = validateAndShowError()

Expand All @@ -179,12 +189,18 @@ const ProductView = forwardRef(
await updateCart(variant, quantity)
return
}
const itemsAdded = await addToCart(variant, quantity)

onAddToCartModalOpen({
product,
itemsAdded
})
try {
const itemsAdded = await addToCart(variant, quantity)
// only show the modal when an item is successfully added to the cart
if (itemsAdded) {
onAddToCartModalOpen({
product,
itemsAdded
})
}
} catch (e) {
showError()
}
}

const handleWishlistItem = async () => {
Expand Down