bitsnap-react 1.2.0 → 1.2.1

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/index.js CHANGED
@@ -142,7 +142,7 @@ function formatCurrency(amount, currency) {
142
142
  var import_zustand = require("zustand");
143
143
  var useCheckoutStore = (0, import_zustand.create)((set) => ({
144
144
  isCartVisible: false,
145
- numberOfProductsInCart: 2,
145
+ numberOfProductsInCart: 0,
146
146
  showCart: () => set((state) => ({ ...state, isCartVisible: true })),
147
147
  hideCart: () => set((state) => ({ ...state, isCartVisible: false })),
148
148
  setNumberOfProductsInCart: (numberOfProductsInCart) => set((state) => ({ ...state, numberOfProductsInCart }))
@@ -386,6 +386,9 @@ function getNewHostIfExist() {
386
386
  }
387
387
  var CartProvider = ({ children }) => {
388
388
  const queryClient = new import_react_query.QueryClient();
389
+ (0, import_react.useEffect)(() => {
390
+ updateNumberOfProductsInCart();
391
+ }, []);
389
392
  const projectID = getProjectID();
390
393
  if (projectID == null) {
391
394
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
@@ -498,6 +501,7 @@ function addProducts(products) {
498
501
  metadata: el.metadata
499
502
  })));
500
503
  saveCheckout(checkout);
504
+ updateNumberOfProductsInCart();
501
505
  }
502
506
  function removeProductFromCheckout(ids) {
503
507
  var _a;
@@ -509,6 +513,7 @@ function removeProductFromCheckout(ids) {
509
513
  )
510
514
  };
511
515
  saveCheckout(newCheckout);
516
+ updateNumberOfProductsInCart();
512
517
  }
513
518
  function saveCheckout(model) {
514
519
  localStorage.setItem(checkoutKey, JSON.stringify(model));
@@ -616,6 +621,7 @@ var getCheckoutMethods = (projectID) => {
616
621
  const empty = structuredClone(emptyCheckout);
617
622
  empty.country = (_a = getCheckout()) == null ? void 0 : _a.country;
618
623
  saveCheckout(empty);
624
+ updateNumberOfProductsInCart();
619
625
  },
620
626
  async getAvailableCountries() {
621
627
  const result = await fetch(buildURL(projectID, "/countries"), {
@@ -697,6 +703,7 @@ var getCheckoutMethods = (projectID) => {
697
703
  return product;
698
704
  });
699
705
  saveCheckout(checkout);
706
+ updateNumberOfProductsInCart();
700
707
  },
701
708
  async redirectToNextStep() {
702
709
  const checkout = getCheckout();
@@ -1082,6 +1089,17 @@ function resolveProductDetailsFromSingleProduct(id, product) {
1082
1089
  images: (_d = variant.images) != null ? _d : product.images
1083
1090
  };
1084
1091
  }
1092
+ function updateNumberOfProductsInCart() {
1093
+ var _a, _b;
1094
+ const checkout = getCheckout();
1095
+ let numberOfProductsInCart = 0;
1096
+ if (checkout == null) {
1097
+ useCheckoutStore.setState((state) => ({ ...state, numberOfProductsInCart }));
1098
+ return;
1099
+ }
1100
+ numberOfProductsInCart = (_b = (_a = checkout.products) == null ? void 0 : _a.reduce((acc, product) => acc + product.quantity, 0)) != null ? _b : 0;
1101
+ useCheckoutStore.setState((state) => ({ ...state, numberOfProductsInCart }));
1102
+ }
1085
1103
 
1086
1104
  // src/components/checkout/ApplePay.tsx
1087
1105
  var import_react_query2 = require("@tanstack/react-query");
@@ -1963,9 +1981,10 @@ function BitsnapCart({
1963
1981
  projectID,
1964
1982
  children,
1965
1983
  onVisibleChange,
1966
- className
1984
+ className,
1985
+ numberOfProductsInCartOptions
1967
1986
  }) {
1968
- const { isCartVisible, showCart, hideCart } = useCheckoutStore();
1987
+ const { isCartVisible, showCart, hideCart, numberOfProductsInCart } = useCheckoutStore();
1969
1988
  function sentPostMessageToIframe(msg) {
1970
1989
  const iframes = document.querySelectorAll("iframe");
1971
1990
  iframes.forEach((iframe) => {
@@ -2080,31 +2099,57 @@ function BitsnapCart({
2080
2099
  }
2081
2100
  }, []);
2082
2101
  return /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(import_jsx_runtime10.Fragment, { children: [
2083
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
2102
+ /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(
2084
2103
  "button",
2085
2104
  {
2086
2105
  onClick: () => isCartVisible ? hideCart() : showCart(),
2087
- className: ["bitsnap-checkout", className != null ? className : "relative rounded-full hover:bg-neutral-300 transition p-1"].join(" "),
2088
- children: children ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(import_jsx_runtime10.Fragment, { children }) : /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(
2089
- "svg",
2090
- {
2091
- xmlns: "http://www.w3.org/2000/svg",
2092
- width: "24",
2093
- height: "24",
2094
- viewBox: "0 0 24 24",
2095
- fill: "none",
2096
- stroke: "currentColor",
2097
- strokeWidth: "2",
2098
- strokeLinecap: "round",
2099
- strokeLinejoin: "round",
2100
- className: "lucide lucide-shopping-cart",
2101
- children: [
2102
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("circle", { cx: "8", cy: "21", r: "1" }),
2103
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("circle", { cx: "19", cy: "21", r: "1" }),
2104
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("path", { d: "M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12" })
2105
- ]
2106
- }
2107
- )
2106
+ style: { position: "relative" },
2107
+ className: ["bitsnap-checkout", className != null ? className : "rounded-full hover:bg-neutral-300 transition p-1"].join(" "),
2108
+ children: [
2109
+ children ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(import_jsx_runtime10.Fragment, { children }) : /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(
2110
+ "svg",
2111
+ {
2112
+ xmlns: "http://www.w3.org/2000/svg",
2113
+ width: "24",
2114
+ height: "24",
2115
+ viewBox: "0 0 24 24",
2116
+ fill: "none",
2117
+ stroke: "currentColor",
2118
+ strokeWidth: "2",
2119
+ strokeLinecap: "round",
2120
+ strokeLinejoin: "round",
2121
+ className: "lucide lucide-shopping-cart",
2122
+ children: [
2123
+ /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("circle", { cx: "8", cy: "21", r: "1" }),
2124
+ /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("circle", { cx: "19", cy: "21", r: "1" }),
2125
+ /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("path", { d: "M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12" })
2126
+ ]
2127
+ }
2128
+ ),
2129
+ (numberOfProductsInCartOptions == null ? void 0 : numberOfProductsInCartOptions.isVisible) != false && numberOfProductsInCart > 0 && /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
2130
+ "div",
2131
+ {
2132
+ style: {
2133
+ position: "absolute",
2134
+ bottom: -5,
2135
+ right: -5,
2136
+ background: "black",
2137
+ color: "white",
2138
+ borderRadius: "50%",
2139
+ width: "22px",
2140
+ height: "22px",
2141
+ display: "flex",
2142
+ alignItems: "center",
2143
+ justifyContent: "center",
2144
+ fontSize: "13px",
2145
+ fontWeight: 600,
2146
+ boxShadow: "0 2px 8px rgba(0, 0, 0, 0.25)",
2147
+ ...numberOfProductsInCartOptions == null ? void 0 : numberOfProductsInCartOptions.style
2148
+ },
2149
+ children: numberOfProductsInCart
2150
+ }
2151
+ )
2152
+ ]
2108
2153
  }
2109
2154
  ),
2110
2155
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(