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.mjs CHANGED
@@ -58,7 +58,7 @@ var PublicApiClient;
58
58
 
59
59
  // src/components/checkout/CartProvider.tsx
60
60
  import { create as create2 } from "@bufbuild/protobuf";
61
- import { createContext, useContext } from "react";
61
+ import { createContext, useContext, useEffect } from "react";
62
62
  import zod from "zod";
63
63
 
64
64
  // src/components/checkout/constants.ts
@@ -99,7 +99,7 @@ function formatCurrency(amount, currency) {
99
99
  import { create } from "zustand";
100
100
  var useCheckoutStore = create((set) => ({
101
101
  isCartVisible: false,
102
- numberOfProductsInCart: 2,
102
+ numberOfProductsInCart: 0,
103
103
  showCart: () => set((state) => ({ ...state, isCartVisible: true })),
104
104
  hideCart: () => set((state) => ({ ...state, isCartVisible: false })),
105
105
  setNumberOfProductsInCart: (numberOfProductsInCart) => set((state) => ({ ...state, numberOfProductsInCart }))
@@ -343,6 +343,9 @@ function getNewHostIfExist() {
343
343
  }
344
344
  var CartProvider = ({ children }) => {
345
345
  const queryClient = new QueryClient();
346
+ useEffect(() => {
347
+ updateNumberOfProductsInCart();
348
+ }, []);
346
349
  const projectID = getProjectID();
347
350
  if (projectID == null) {
348
351
  return /* @__PURE__ */ jsx(Fragment, {});
@@ -455,6 +458,7 @@ function addProducts(products) {
455
458
  metadata: el.metadata
456
459
  })));
457
460
  saveCheckout(checkout);
461
+ updateNumberOfProductsInCart();
458
462
  }
459
463
  function removeProductFromCheckout(ids) {
460
464
  var _a;
@@ -466,6 +470,7 @@ function removeProductFromCheckout(ids) {
466
470
  )
467
471
  };
468
472
  saveCheckout(newCheckout);
473
+ updateNumberOfProductsInCart();
469
474
  }
470
475
  function saveCheckout(model) {
471
476
  localStorage.setItem(checkoutKey, JSON.stringify(model));
@@ -573,6 +578,7 @@ var getCheckoutMethods = (projectID) => {
573
578
  const empty = structuredClone(emptyCheckout);
574
579
  empty.country = (_a = getCheckout()) == null ? void 0 : _a.country;
575
580
  saveCheckout(empty);
581
+ updateNumberOfProductsInCart();
576
582
  },
577
583
  async getAvailableCountries() {
578
584
  const result = await fetch(buildURL(projectID, "/countries"), {
@@ -654,6 +660,7 @@ var getCheckoutMethods = (projectID) => {
654
660
  return product;
655
661
  });
656
662
  saveCheckout(checkout);
663
+ updateNumberOfProductsInCart();
657
664
  },
658
665
  async redirectToNextStep() {
659
666
  const checkout = getCheckout();
@@ -1039,6 +1046,17 @@ function resolveProductDetailsFromSingleProduct(id, product) {
1039
1046
  images: (_d = variant.images) != null ? _d : product.images
1040
1047
  };
1041
1048
  }
1049
+ function updateNumberOfProductsInCart() {
1050
+ var _a, _b;
1051
+ const checkout = getCheckout();
1052
+ let numberOfProductsInCart = 0;
1053
+ if (checkout == null) {
1054
+ useCheckoutStore.setState((state) => ({ ...state, numberOfProductsInCart }));
1055
+ return;
1056
+ }
1057
+ numberOfProductsInCart = (_b = (_a = checkout.products) == null ? void 0 : _a.reduce((acc, product) => acc + product.quantity, 0)) != null ? _b : 0;
1058
+ useCheckoutStore.setState((state) => ({ ...state, numberOfProductsInCart }));
1059
+ }
1042
1060
 
1043
1061
  // src/components/checkout/ApplePay.tsx
1044
1062
  import { useQuery } from "@tanstack/react-query";
@@ -1350,7 +1368,7 @@ function Wrapper2(props) {
1350
1368
  var GooglePay_default = Wrapper2;
1351
1369
 
1352
1370
  // src/components/checkout/BitsnapCart.tsx
1353
- import { useEffect as useEffect3 } from "react";
1371
+ import { useEffect as useEffect4 } from "react";
1354
1372
  import zod2 from "zod";
1355
1373
 
1356
1374
  // src/components/checkout/CartComponent.tsx
@@ -1363,7 +1381,7 @@ import React from "react";
1363
1381
 
1364
1382
  // src/components/checkout/CountrySelector.tsx
1365
1383
  import { AnimatePresence, motion } from "framer-motion";
1366
- import { useEffect, useRef, useState } from "react";
1384
+ import { useEffect as useEffect2, useRef, useState } from "react";
1367
1385
  import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
1368
1386
  function CountrySelector({
1369
1387
  id,
@@ -1376,7 +1394,7 @@ function CountrySelector({
1376
1394
  }) {
1377
1395
  var _a;
1378
1396
  const ref = useRef(null);
1379
- useEffect(() => {
1397
+ useEffect2(() => {
1380
1398
  const mutableRef = ref;
1381
1399
  const handleClickOutside = (event) => {
1382
1400
  if (mutableRef.current && // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -1391,7 +1409,7 @@ function CountrySelector({
1391
1409
  document.removeEventListener("mousedown", handleClickOutside);
1392
1410
  };
1393
1411
  }, [ref]);
1394
- useEffect(() => {
1412
+ useEffect2(() => {
1395
1413
  if (selectedValue == null && countries.length > 0) {
1396
1414
  onChange(countries[0].code);
1397
1415
  }
@@ -1543,7 +1561,7 @@ function CountrySelector({
1543
1561
  var CountrySelector_default = CountrySelector;
1544
1562
 
1545
1563
  // src/components/checkout/SingleProduct.tsx
1546
- import { useEffect as useEffect2, useState as useState2 } from "react";
1564
+ import { useEffect as useEffect3, useState as useState2 } from "react";
1547
1565
  import { jsx as jsx6, jsxs as jsxs3 } from "react/jsx-runtime";
1548
1566
  var SingleProduct = ({ quantity, details, shouldUpdate }) => {
1549
1567
  var _a;
@@ -1567,7 +1585,7 @@ var QuantityComponent = ({
1567
1585
  }) => {
1568
1586
  const [quantityString, setQuantityString] = useState2(quantity.toString());
1569
1587
  const [quantityValue, setQuantityValue] = useState2(quantity);
1570
- useEffect2(() => {
1588
+ useEffect3(() => {
1571
1589
  shouldUpdate(quantityValue);
1572
1590
  }, [quantityValue]);
1573
1591
  function setNewQuantity(newQuantity) {
@@ -1920,9 +1938,10 @@ function BitsnapCart({
1920
1938
  projectID,
1921
1939
  children,
1922
1940
  onVisibleChange,
1923
- className
1941
+ className,
1942
+ numberOfProductsInCartOptions
1924
1943
  }) {
1925
- const { isCartVisible, showCart, hideCart } = useCheckoutStore();
1944
+ const { isCartVisible, showCart, hideCart, numberOfProductsInCart } = useCheckoutStore();
1926
1945
  function sentPostMessageToIframe(msg) {
1927
1946
  const iframes = document.querySelectorAll("iframe");
1928
1947
  iframes.forEach((iframe) => {
@@ -1930,13 +1949,13 @@ function BitsnapCart({
1930
1949
  (_a = iframe.contentWindow) == null ? void 0 : _a.postMessage(msg, "*");
1931
1950
  });
1932
1951
  }
1933
- useEffect3(() => {
1952
+ useEffect4(() => {
1934
1953
  onVisibleChange == null ? void 0 : onVisibleChange(isCartVisible);
1935
1954
  }, [isCartVisible]);
1936
- useEffect3(() => {
1955
+ useEffect4(() => {
1937
1956
  setProjectID(projectID);
1938
1957
  }, [projectID]);
1939
- useEffect3(() => {
1958
+ useEffect4(() => {
1940
1959
  if (!("cart" in window)) {
1941
1960
  const projectID2 = getProjectID();
1942
1961
  if (projectID2 == null) {
@@ -2021,7 +2040,7 @@ function BitsnapCart({
2021
2040
  }
2022
2041
  });
2023
2042
  }
2024
- useEffect3(() => {
2043
+ useEffect4(() => {
2025
2044
  var _a;
2026
2045
  setupEventListener();
2027
2046
  if (typeof window !== "undefined") {
@@ -2037,31 +2056,57 @@ function BitsnapCart({
2037
2056
  }
2038
2057
  }, []);
2039
2058
  return /* @__PURE__ */ jsxs6(Fragment6, { children: [
2040
- /* @__PURE__ */ jsx10(
2059
+ /* @__PURE__ */ jsxs6(
2041
2060
  "button",
2042
2061
  {
2043
2062
  onClick: () => isCartVisible ? hideCart() : showCart(),
2044
- className: ["bitsnap-checkout", className != null ? className : "relative rounded-full hover:bg-neutral-300 transition p-1"].join(" "),
2045
- children: children ? /* @__PURE__ */ jsx10(Fragment6, { children }) : /* @__PURE__ */ jsxs6(
2046
- "svg",
2047
- {
2048
- xmlns: "http://www.w3.org/2000/svg",
2049
- width: "24",
2050
- height: "24",
2051
- viewBox: "0 0 24 24",
2052
- fill: "none",
2053
- stroke: "currentColor",
2054
- strokeWidth: "2",
2055
- strokeLinecap: "round",
2056
- strokeLinejoin: "round",
2057
- className: "lucide lucide-shopping-cart",
2058
- children: [
2059
- /* @__PURE__ */ jsx10("circle", { cx: "8", cy: "21", r: "1" }),
2060
- /* @__PURE__ */ jsx10("circle", { cx: "19", cy: "21", r: "1" }),
2061
- /* @__PURE__ */ jsx10("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" })
2062
- ]
2063
- }
2064
- )
2063
+ style: { position: "relative" },
2064
+ className: ["bitsnap-checkout", className != null ? className : "rounded-full hover:bg-neutral-300 transition p-1"].join(" "),
2065
+ children: [
2066
+ children ? /* @__PURE__ */ jsx10(Fragment6, { children }) : /* @__PURE__ */ jsxs6(
2067
+ "svg",
2068
+ {
2069
+ xmlns: "http://www.w3.org/2000/svg",
2070
+ width: "24",
2071
+ height: "24",
2072
+ viewBox: "0 0 24 24",
2073
+ fill: "none",
2074
+ stroke: "currentColor",
2075
+ strokeWidth: "2",
2076
+ strokeLinecap: "round",
2077
+ strokeLinejoin: "round",
2078
+ className: "lucide lucide-shopping-cart",
2079
+ children: [
2080
+ /* @__PURE__ */ jsx10("circle", { cx: "8", cy: "21", r: "1" }),
2081
+ /* @__PURE__ */ jsx10("circle", { cx: "19", cy: "21", r: "1" }),
2082
+ /* @__PURE__ */ jsx10("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" })
2083
+ ]
2084
+ }
2085
+ ),
2086
+ (numberOfProductsInCartOptions == null ? void 0 : numberOfProductsInCartOptions.isVisible) != false && numberOfProductsInCart > 0 && /* @__PURE__ */ jsx10(
2087
+ "div",
2088
+ {
2089
+ style: {
2090
+ position: "absolute",
2091
+ bottom: -5,
2092
+ right: -5,
2093
+ background: "black",
2094
+ color: "white",
2095
+ borderRadius: "50%",
2096
+ width: "22px",
2097
+ height: "22px",
2098
+ display: "flex",
2099
+ alignItems: "center",
2100
+ justifyContent: "center",
2101
+ fontSize: "13px",
2102
+ fontWeight: 600,
2103
+ boxShadow: "0 2px 8px rgba(0, 0, 0, 0.25)",
2104
+ ...numberOfProductsInCartOptions == null ? void 0 : numberOfProductsInCartOptions.style
2105
+ },
2106
+ children: numberOfProductsInCart
2107
+ }
2108
+ )
2109
+ ]
2065
2110
  }
2066
2111
  ),
2067
2112
  /* @__PURE__ */ jsx10(