@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
62 lines (61 loc) • 2.21 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Money = require("./Money.js");
const flattenConnection = require("./flatten-connection.js");
const jsxRuntime = require("react/jsx-runtime");
function ProductPrice(props) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const {
priceType = "regular",
variantId,
valueType = "min",
data: product,
...passthroughProps
} = props;
if (product == null) {
throw new Error(`<ProductPrice/> requires a product as the 'data' prop`);
}
let price;
let measurement;
const variant = variantId ? (_b = flattenConnection.flattenConnection((_a = product == null ? void 0 : product.variants) != null ? _a : {}).find((variant2) => (variant2 == null ? void 0 : variant2.id) === variantId)) != null ? _b : null : null;
if (priceType === "compareAt") {
if (variantId && variant) {
if (((_c = variant.compareAtPriceV2) == null ? void 0 : _c.amount) === ((_d = variant.priceV2) == null ? void 0 : _d.amount)) {
return null;
}
price = variant.compareAtPriceV2;
} else if (valueType === "max") {
price = (_e = product == null ? void 0 : product.compareAtPriceRange) == null ? void 0 : _e.maxVariantPrice;
} else {
price = (_f = product == null ? void 0 : product.compareAtPriceRange) == null ? void 0 : _f.minVariantPrice;
}
} else {
if (variantId && variant) {
price = variant.priceV2;
if (valueType === "unit") {
price = variant.unitPrice;
measurement = variant.unitPriceMeasurement;
}
} else if (valueType === "max") {
price = (_g = product.priceRange) == null ? void 0 : _g.maxVariantPrice;
} else {
price = (_h = product.priceRange) == null ? void 0 : _h.minVariantPrice;
}
}
if (!price) {
return null;
}
if (measurement) {
return /* @__PURE__ */ jsxRuntime.jsx(Money.Money, {
...passthroughProps,
data: price,
measurement
});
}
return /* @__PURE__ */ jsxRuntime.jsx(Money.Money, {
...passthroughProps,
data: price
});
}
exports.ProductPrice = ProductPrice;
//# sourceMappingURL=ProductPrice.js.map