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

[BUG] linesRemove and linesAdd do not support promises #2520

Open
Issam-AB opened this issue Jun 2, 2023 · 0 comments
Open

[BUG] linesRemove and linesAdd do not support promises #2520

Issam-AB opened this issue Jun 2, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@Issam-AB
Copy link

Issam-AB commented Jun 2, 2023

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
@Issam-AB Issam-AB added the bug Something isn't working label Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant