[BUG] linesRemove and linesAdd do not support promises
Issam-AB opened this issue · comments
Issam ABOULFADL commented
Describe the bug
linesRemove and linesAdd do not support promises
const handleCustomOnClick = async (event) => {
if (performed.current) {
performed.current = false;
return;
}
event.preventDefault(); // Stop default behavior
// Check if there is an old line with handle === "kit"
const existingTableLine = lines.find(
(item) => item.merchandise.product.productType === 'Style de kit',
);
// Remove the existing line with handle === "kit"
if (existingTableLine) {
await linesRemove([existingTableLine.id]);
}
// Add the new product
const lineToAdd = selectedVariant?.id;
const newProduct = {
merchandiseId: lineToAdd,
quantity: 1,
};
// Display loading message
messageApi.open({
key,
type: 'loading',
content: 'Chargement...',
});
// Delay for a smoother transition
await new Promise((resolve) => setTimeout(resolve, 700));
// Add the new line
await linesAdd([newProduct]);
// Display success message
if (!existingTableLine) {
messageApi.open({
key,
type: 'success',
content: 'Ajouter au panier!',
duration: 3,
});
}
performed.current = true; // Prevent retriggering
buttonRef.current.click(); // Trigger button default behavior
console.log('lines', lines);
};
// btn add to lines handleCustomOnClick
<AddToCartButton
variantId={selectedVariant?.id}
quantity={qantity}
accessibleAddingToCartLabel="Ajout d'un article à votre panier"
disabled={isOutOfStock}
type="button"
onClick={handleCustomOnClick}
>
<div width="full" variant={isOutOfStock ? 'secondary' : 'primary'}>
{isOutOfStock ? null : children}
</div>
</AddToCartButton>
- Hydrogen version 3.25.0
- Node version v18.12.1