storenest-commerce
Version:
Complete e-commerce SDK for Storenest platform with React components, multi-language support, secure checkout, and enterprise-grade security
35 lines (34 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddToCartButton = AddToCartButton;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const StorenestProvider_1 = require("../context/StorenestProvider");
function AddToCartButton({ productId, quantity = 1, variationId, className = '', children, onSuccess, onError, disabled = false, }) {
const { addToCart, isAuthenticated, cartLoading } = (0, StorenestProvider_1.useStorenest)();
const [isAdding, setIsAdding] = (0, react_1.useState)(false);
const handleClick = async () => {
if (!isAuthenticated) {
onError?.('Please log in to add items to cart');
return;
}
setIsAdding(true);
try {
await addToCart({
productId,
quantity,
variationId,
});
onSuccess?.();
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Failed to add to cart';
onError?.(errorMessage);
}
finally {
setIsAdding(false);
}
};
const isLoading = isAdding || cartLoading;
return ((0, jsx_runtime_1.jsx)("button", { onClick: handleClick, disabled: disabled || isLoading, className: className, children: isLoading ? 'Adding...' : children || 'Add to Cart' }));
}