UNPKG

@shopify/hydrogen-react

Version:

React components, hooks, and utilities for creating custom Shopify storefronts

50 lines (49 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const React = require("react"); const CartProvider = require("./CartProvider.js"); const BaseButton = require("./BaseButton.js"); function BuyNowButton(props) { const { cartCreate, checkoutUrl } = CartProvider.useCart(); const [loading, setLoading] = React.useState(false); const { quantity, variantId, sellingPlanId, onClick, attributes, children, ...passthroughProps } = props; React.useEffect(() => { if (loading && checkoutUrl) { window.location.href = checkoutUrl; } }, [loading, checkoutUrl]); const handleBuyNow = React.useCallback(() => { setLoading(true); cartCreate({ lines: [ { quantity: quantity ?? 1, merchandiseId: variantId, attributes, sellingPlanId } ] }); }, [cartCreate, quantity, variantId, attributes, sellingPlanId]); return /* @__PURE__ */ jsxRuntime.jsx( BaseButton.BaseButton, { disabled: loading ?? passthroughProps.disabled, ...passthroughProps, onClick, defaultOnClick: handleBuyNow, children } ); } exports.BuyNowButton = BuyNowButton; //# sourceMappingURL=BuyNowButton.js.map