@cmssy/react 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.cjs CHANGED
@@ -1058,6 +1058,9 @@ function fractionDigits(currency) {
1058
1058
  function fromMinorUnits(minor, currency) {
1059
1059
  return minor / 10 ** fractionDigits(currency);
1060
1060
  }
1061
+ function toMinorUnits(amount, currency) {
1062
+ return Math.round(amount * 10 ** fractionDigits(currency));
1063
+ }
1061
1064
  function formatPrice(minor, currency) {
1062
1065
  if (!Number.isFinite(minor)) return "";
1063
1066
  const code = currency ?? "USD";
@@ -1094,8 +1097,9 @@ function matchVariant(variants, selections) {
1094
1097
  function ProductComponent({ content }) {
1095
1098
  const c = content;
1096
1099
  const { fetchProduct, addToCart } = useCart();
1097
- const [product, setProduct] = react.useState(null);
1098
- const [loading, setLoading] = react.useState(true);
1100
+ const injected = c.product ?? null;
1101
+ const [product, setProduct] = react.useState(injected);
1102
+ const [loading, setLoading] = react.useState(!injected && Boolean(c.slug));
1099
1103
  const [selections, setSelections] = react.useState({});
1100
1104
  const [adding, setAdding] = react.useState(false);
1101
1105
  const [added, setAdded] = react.useState(false);
@@ -1104,11 +1108,12 @@ function ProductComponent({ content }) {
1104
1108
  const slugField = c.slugField ?? "slug";
1105
1109
  const slug = c.slug ?? "";
1106
1110
  react.useEffect(() => {
1111
+ if (injected || !slug) return;
1107
1112
  let active = true;
1108
1113
  setLoading(true);
1109
1114
  void (async () => {
1110
1115
  try {
1111
- const result = slug ? await fetchProduct(modelSlug, { [slugField]: slug }) : null;
1116
+ const result = await fetchProduct(modelSlug, { [slugField]: slug });
1112
1117
  if (active) setProduct(result);
1113
1118
  } finally {
1114
1119
  if (active) setLoading(false);
@@ -1117,7 +1122,7 @@ function ProductComponent({ content }) {
1117
1122
  return () => {
1118
1123
  active = false;
1119
1124
  };
1120
- }, [fetchProduct, modelSlug, slugField, slug]);
1125
+ }, [injected, fetchProduct, modelSlug, slugField, slug]);
1121
1126
  const axes = react.useMemo(
1122
1127
  () => product ? deriveAxes(product.variants) : [],
1123
1128
  [product]
@@ -1137,7 +1142,7 @@ function ProductComponent({ content }) {
1137
1142
  const imageUrl = c.imageField ? data[c.imageField] : null;
1138
1143
  const hasVariants = product.variants.length > 0;
1139
1144
  const currency = data.currency ?? "USD";
1140
- const priceMinor = hasVariants ? variant?.price : Number(data[c.priceField ?? "price"] ?? 0);
1145
+ const priceMinor = hasVariants ? variant?.price : toMinorUnits(Number(data[c.priceField ?? "price"] ?? 0), currency);
1141
1146
  const showPrice = priceMinor != null && Number.isFinite(priceMinor);
1142
1147
  const allAxesSelected = axes.every((axis) => selections[axis.name]);
1143
1148
  const outOfStock = variant != null && variant.inventory != null && variant.inventory <= 0;
@@ -1415,6 +1420,7 @@ exports.formatPrice = formatPrice;
1415
1420
  exports.fractionDigits = fractionDigits;
1416
1421
  exports.fromMinorUnits = fromMinorUnits;
1417
1422
  exports.productBlock = productBlock;
1423
+ exports.toMinorUnits = toMinorUnits;
1418
1424
  exports.useCart = useCart;
1419
1425
  exports.useCmssyUser = useCmssyUser;
1420
1426
  exports.useEditBridge = useEditBridge;
package/dist/client.d.cts CHANGED
@@ -138,6 +138,7 @@ declare const checkoutBlock: BlockDefinition;
138
138
 
139
139
  declare function fractionDigits(currency: string): number;
140
140
  declare function fromMinorUnits(minor: number, currency: string): number;
141
+ declare function toMinorUnits(amount: number, currency: string): number;
141
142
  declare function formatPrice(minor: number, currency: string | null | undefined): string;
142
143
 
143
- export { type CmssyAddToCartOptions, type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyCart, CmssyCommerceProvider, type CmssyCommerceProviderProps, type CmssyCommerceState, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, CmssyOrder, CmssyProduct, type EditBridgeConfig, type EditBridgeState, type PatchMap, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, useCart, useCmssyUser, useEditBridge };
144
+ export { type CmssyAddToCartOptions, type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyCart, CmssyCommerceProvider, type CmssyCommerceProviderProps, type CmssyCommerceState, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, CmssyOrder, CmssyProduct, type EditBridgeConfig, type EditBridgeState, type PatchMap, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, toMinorUnits, useCart, useCmssyUser, useEditBridge };
package/dist/client.d.ts CHANGED
@@ -138,6 +138,7 @@ declare const checkoutBlock: BlockDefinition;
138
138
 
139
139
  declare function fractionDigits(currency: string): number;
140
140
  declare function fromMinorUnits(minor: number, currency: string): number;
141
+ declare function toMinorUnits(amount: number, currency: string): number;
141
142
  declare function formatPrice(minor: number, currency: string | null | undefined): string;
142
143
 
143
- export { type CmssyAddToCartOptions, type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyCart, CmssyCommerceProvider, type CmssyCommerceProviderProps, type CmssyCommerceState, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, CmssyOrder, CmssyProduct, type EditBridgeConfig, type EditBridgeState, type PatchMap, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, useCart, useCmssyUser, useEditBridge };
144
+ export { type CmssyAddToCartOptions, type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyCart, CmssyCommerceProvider, type CmssyCommerceProviderProps, type CmssyCommerceState, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, CmssyOrder, CmssyProduct, type EditBridgeConfig, type EditBridgeState, type PatchMap, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, toMinorUnits, useCart, useCmssyUser, useEditBridge };
package/dist/client.js CHANGED
@@ -1056,6 +1056,9 @@ function fractionDigits(currency) {
1056
1056
  function fromMinorUnits(minor, currency) {
1057
1057
  return minor / 10 ** fractionDigits(currency);
1058
1058
  }
1059
+ function toMinorUnits(amount, currency) {
1060
+ return Math.round(amount * 10 ** fractionDigits(currency));
1061
+ }
1059
1062
  function formatPrice(minor, currency) {
1060
1063
  if (!Number.isFinite(minor)) return "";
1061
1064
  const code = currency ?? "USD";
@@ -1092,8 +1095,9 @@ function matchVariant(variants, selections) {
1092
1095
  function ProductComponent({ content }) {
1093
1096
  const c = content;
1094
1097
  const { fetchProduct, addToCart } = useCart();
1095
- const [product, setProduct] = useState(null);
1096
- const [loading, setLoading] = useState(true);
1098
+ const injected = c.product ?? null;
1099
+ const [product, setProduct] = useState(injected);
1100
+ const [loading, setLoading] = useState(!injected && Boolean(c.slug));
1097
1101
  const [selections, setSelections] = useState({});
1098
1102
  const [adding, setAdding] = useState(false);
1099
1103
  const [added, setAdded] = useState(false);
@@ -1102,11 +1106,12 @@ function ProductComponent({ content }) {
1102
1106
  const slugField = c.slugField ?? "slug";
1103
1107
  const slug = c.slug ?? "";
1104
1108
  useEffect(() => {
1109
+ if (injected || !slug) return;
1105
1110
  let active = true;
1106
1111
  setLoading(true);
1107
1112
  void (async () => {
1108
1113
  try {
1109
- const result = slug ? await fetchProduct(modelSlug, { [slugField]: slug }) : null;
1114
+ const result = await fetchProduct(modelSlug, { [slugField]: slug });
1110
1115
  if (active) setProduct(result);
1111
1116
  } finally {
1112
1117
  if (active) setLoading(false);
@@ -1115,7 +1120,7 @@ function ProductComponent({ content }) {
1115
1120
  return () => {
1116
1121
  active = false;
1117
1122
  };
1118
- }, [fetchProduct, modelSlug, slugField, slug]);
1123
+ }, [injected, fetchProduct, modelSlug, slugField, slug]);
1119
1124
  const axes = useMemo(
1120
1125
  () => product ? deriveAxes(product.variants) : [],
1121
1126
  [product]
@@ -1135,7 +1140,7 @@ function ProductComponent({ content }) {
1135
1140
  const imageUrl = c.imageField ? data[c.imageField] : null;
1136
1141
  const hasVariants = product.variants.length > 0;
1137
1142
  const currency = data.currency ?? "USD";
1138
- const priceMinor = hasVariants ? variant?.price : Number(data[c.priceField ?? "price"] ?? 0);
1143
+ const priceMinor = hasVariants ? variant?.price : toMinorUnits(Number(data[c.priceField ?? "price"] ?? 0), currency);
1139
1144
  const showPrice = priceMinor != null && Number.isFinite(priceMinor);
1140
1145
  const allAxesSelected = axes.every((axis) => selections[axis.name]);
1141
1146
  const outOfStock = variant != null && variant.inventory != null && variant.inventory <= 0;
@@ -1401,4 +1406,4 @@ var checkoutBlock = defineBlock({
1401
1406
  component: CheckoutComponent
1402
1407
  });
1403
1408
 
1404
- export { CmssyAuthProvider, CmssyCommerceProvider, CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, useCart, useCmssyUser, useEditBridge };
1409
+ export { CmssyAuthProvider, CmssyCommerceProvider, CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, toMinorUnits, useCart, useCmssyUser, useEditBridge };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/react",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "React blocks, renderers, data client and editor bridge for cmssy headless sites",
5
5
  "keywords": [
6
6
  "cmssy",