@rebuy/rebuy-hydrogen 3.0.0-beta.19 → 3.0.0-beta.20
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/README.md +60 -35
- package/dist/index.css +27 -27
- package/dist/index.css.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +470 -32
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +468 -30
- package/dist/index.mjs.map +4 -4
- package/dist/providers/RebuyHydrogenContextProvider.d.ts.map +1 -1
- package/dist/smart-cart/constants.d.ts +1 -0
- package/dist/smart-cart/constants.d.ts.map +1 -1
- package/dist/smart-cart/hooks/useCartItemFeatures.d.ts.map +1 -1
- package/dist/types/rebuyCustom.d.ts +1 -0
- package/dist/types/rebuyCustom.d.ts.map +1 -1
- package/dist/utils/createContextParameters.d.ts.map +1 -1
- package/dist/widgets/RebuyGiftWithPurchase/RebuyGiftWithPurchase.d.ts +11 -0
- package/dist/widgets/RebuyGiftWithPurchase/RebuyGiftWithPurchase.d.ts.map +1 -0
- package/dist/widgets/RebuyGiftWithPurchase/index.d.ts +3 -0
- package/dist/widgets/RebuyGiftWithPurchase/index.d.ts.map +1 -0
- package/dist/widgets/RebuyGiftWithPurchase/store/giftWithPurchaseStore.d.ts +31 -0
- package/dist/widgets/RebuyGiftWithPurchase/store/giftWithPurchaseStore.d.ts.map +1 -0
- package/dist/widgets/RebuyGiftWithPurchase/types.d.ts +12 -0
- package/dist/widgets/RebuyGiftWithPurchase/types.d.ts.map +1 -0
- package/dist/widgets/RebuyGiftWithPurchase/utils.d.ts +35 -0
- package/dist/widgets/RebuyGiftWithPurchase/utils.d.ts.map +1 -0
- package/dist/widgets/RebuyWidget/RebuyWidget.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -2672,6 +2672,11 @@ var createContextParameters = ({
|
|
2672
2672
|
legacyCartCostValue = legacyCartCost;
|
2673
2673
|
legacyCartIdValue = legacyCartId;
|
2674
2674
|
}
|
2675
|
+
let shopifyCustomerId;
|
2676
|
+
if (solidifiedHydrogenCart?.buyerIdentity?.customer?.id) {
|
2677
|
+
const customerId = Utilities.getIdFromGraphUrl(solidifiedHydrogenCart.buyerIdentity.customer.id, "Customer");
|
2678
|
+
shopifyCustomerId = customerId?.toString();
|
2679
|
+
}
|
2675
2680
|
const contextParametersOutput = {
|
2676
2681
|
cache_key: cacheKey,
|
2677
2682
|
cart: void 0,
|
@@ -2692,6 +2697,7 @@ var createContextParameters = ({
|
|
2692
2697
|
hydrogenReactCartError,
|
2693
2698
|
hydrogenReactCartStatus,
|
2694
2699
|
isHydrogenReact,
|
2700
|
+
shopify_customer_id: shopifyCustomerId,
|
2695
2701
|
time: queryObject.time ?? "",
|
2696
2702
|
url: windowUrl
|
2697
2703
|
};
|
@@ -2717,7 +2723,7 @@ var createContextParameters = ({
|
|
2717
2723
|
product_id: line.merchandise?.product?.id ? Utilities.getIdFromGraphUrl(line.merchandise.product.id, "Product")?.toString() || "" : "",
|
2718
2724
|
properties: "",
|
2719
2725
|
quantity: line.quantity,
|
2720
|
-
variant_id: line.merchandise?.id ? Utilities.getIdFromGraphUrl(line.merchandise.
|
2726
|
+
variant_id: line.merchandise?.id ? Utilities.getIdFromGraphUrl(line.merchandise.id, "ProductVariant")?.toString() || "" : ""
|
2721
2727
|
};
|
2722
2728
|
if (line.attributes?.length) {
|
2723
2729
|
const validLineAttributes = line.attributes.filter(
|
@@ -2754,8 +2760,11 @@ var RebuyHydrogenContext = ({
|
|
2754
2760
|
} = useRebuyConfig();
|
2755
2761
|
const warningLogged2 = useRef3(false);
|
2756
2762
|
const location = useLocation();
|
2757
|
-
const queryObject = Utilities2.queryStringToObject(location.search);
|
2758
|
-
const windowUrl =
|
2763
|
+
const queryObject = useMemo3(() => Utilities2.queryStringToObject(location.search), [location.search]);
|
2764
|
+
const windowUrl = useMemo3(
|
2765
|
+
() => `${shop}${location.pathname}${location.search}`,
|
2766
|
+
[shop, location.pathname, location.search]
|
2767
|
+
);
|
2759
2768
|
const contextParameters = useMemo3(
|
2760
2769
|
() => createContextParameters({
|
2761
2770
|
cacheKey: rebuyConfig?.shop?.cache_key,
|
@@ -3913,6 +3922,10 @@ var useCartItemFeatures = ({ config, line }) => {
|
|
3913
3922
|
const shouldHideQuantitySelector = useMemo6(() => {
|
3914
3923
|
if (isTieredProgressBarGiftItem(line)) return true;
|
3915
3924
|
if (isCartItemHiddenByStandardLogic(line)) return true;
|
3925
|
+
const hasHideQuantitySelectorAttribute = line.attributes?.some(
|
3926
|
+
(attr) => attr.key === "_r_gwp_hide_qty" /* HIDE_QUANTITY_SELECTOR_GWP_ITEM */ && attr.value === "true"
|
3927
|
+
);
|
3928
|
+
if (hasHideQuantitySelectorAttribute) return true;
|
3916
3929
|
return false;
|
3917
3930
|
}, [line]);
|
3918
3931
|
const isBmsmGloballyEnabled = useMemo6(() => isBMSMEnabled(config), [config]);
|
@@ -9561,9 +9574,433 @@ var RebuyDynamicBundleProducts = (props) => {
|
|
9561
9574
|
] });
|
9562
9575
|
};
|
9563
9576
|
|
9577
|
+
// src/widgets/RebuyGiftWithPurchase/RebuyGiftWithPurchase.tsx
|
9578
|
+
import { RebuyClient as RebuyClient4 } from "@rebuy/rebuy";
|
9579
|
+
import { useCallback as useCallback16, useContext as useContext6, useEffect as useEffect25, useRef as useRef14, useState as useState24 } from "react";
|
9580
|
+
import { useFetcher as useFetcher6 } from "react-router";
|
9581
|
+
|
9582
|
+
// src/widgets/RebuyGiftWithPurchase/store/giftWithPurchaseStore.ts
|
9583
|
+
import { create as create2 } from "zustand";
|
9584
|
+
import { subscribeWithSelector } from "zustand/middleware";
|
9585
|
+
var initialState = {
|
9586
|
+
lastSyncedGiftsByWidget: {},
|
9587
|
+
lastSyncTimestampByWidget: {},
|
9588
|
+
pendingAdditionsByWidget: {},
|
9589
|
+
syncStatusByWidget: {}
|
9590
|
+
};
|
9591
|
+
var useGiftWithPurchaseStore = create2()(
|
9592
|
+
subscribeWithSelector((set, get) => ({
|
9593
|
+
...initialState,
|
9594
|
+
clearPendingAddition: (widgetId, variantId) => {
|
9595
|
+
rebuyDebugLog.log(
|
9596
|
+
"rebuy-widget" /* WIDGET */,
|
9597
|
+
`[GWP Store] Clearing pending addition for widget ${widgetId}: ${variantId}`
|
9598
|
+
);
|
9599
|
+
set((state) => {
|
9600
|
+
const currentPending = state.pendingAdditionsByWidget[widgetId] || [];
|
9601
|
+
const newPending = currentPending.filter((id) => id !== variantId);
|
9602
|
+
return {
|
9603
|
+
pendingAdditionsByWidget: {
|
9604
|
+
...state.pendingAdditionsByWidget,
|
9605
|
+
[widgetId]: newPending
|
9606
|
+
}
|
9607
|
+
};
|
9608
|
+
});
|
9609
|
+
},
|
9610
|
+
getLastSyncedGifts: (widgetId) => {
|
9611
|
+
return get().lastSyncedGiftsByWidget[widgetId] || [];
|
9612
|
+
},
|
9613
|
+
getPendingAdditions: (widgetId) => {
|
9614
|
+
return get().pendingAdditionsByWidget[widgetId] || [];
|
9615
|
+
},
|
9616
|
+
getSyncStatus: (widgetId) => {
|
9617
|
+
return get().syncStatusByWidget[widgetId] || "idle";
|
9618
|
+
},
|
9619
|
+
reset: (widgetId) => {
|
9620
|
+
rebuyDebugLog.log("rebuy-widget" /* WIDGET */, `[GWP Store] Resetting state for widget ${widgetId}`);
|
9621
|
+
set((state) => {
|
9622
|
+
const newSyncStatus = { ...state.syncStatusByWidget };
|
9623
|
+
const newLastSyncedGifts = { ...state.lastSyncedGiftsByWidget };
|
9624
|
+
const newLastSyncTimestamp = { ...state.lastSyncTimestampByWidget };
|
9625
|
+
const newPendingAdditions = { ...state.pendingAdditionsByWidget };
|
9626
|
+
delete newSyncStatus[widgetId];
|
9627
|
+
delete newLastSyncedGifts[widgetId];
|
9628
|
+
delete newLastSyncTimestamp[widgetId];
|
9629
|
+
delete newPendingAdditions[widgetId];
|
9630
|
+
return {
|
9631
|
+
lastSyncedGiftsByWidget: newLastSyncedGifts,
|
9632
|
+
lastSyncTimestampByWidget: newLastSyncTimestamp,
|
9633
|
+
pendingAdditionsByWidget: newPendingAdditions,
|
9634
|
+
syncStatusByWidget: newSyncStatus
|
9635
|
+
};
|
9636
|
+
});
|
9637
|
+
},
|
9638
|
+
resetAll: () => {
|
9639
|
+
rebuyDebugLog.log("rebuy-widget" /* WIDGET */, "[GWP Store] Resetting all state");
|
9640
|
+
set(initialState);
|
9641
|
+
},
|
9642
|
+
setLastSyncedGifts: (widgetId, giftIds) => {
|
9643
|
+
rebuyDebugLog.log(
|
9644
|
+
"rebuy-widget" /* WIDGET */,
|
9645
|
+
`[GWP Store] Setting last synced gifts for widget ${widgetId}:`,
|
9646
|
+
giftIds
|
9647
|
+
);
|
9648
|
+
set((state) => ({
|
9649
|
+
lastSyncedGiftsByWidget: {
|
9650
|
+
...state.lastSyncedGiftsByWidget,
|
9651
|
+
[widgetId]: giftIds
|
9652
|
+
}
|
9653
|
+
}));
|
9654
|
+
},
|
9655
|
+
setPendingAdditions: (widgetId, variantIds) => {
|
9656
|
+
rebuyDebugLog.log(
|
9657
|
+
"rebuy-widget" /* WIDGET */,
|
9658
|
+
`[GWP Store] Setting pending additions for widget ${widgetId}:`,
|
9659
|
+
variantIds
|
9660
|
+
);
|
9661
|
+
set((state) => ({
|
9662
|
+
pendingAdditionsByWidget: {
|
9663
|
+
...state.pendingAdditionsByWidget,
|
9664
|
+
[widgetId]: variantIds
|
9665
|
+
}
|
9666
|
+
}));
|
9667
|
+
},
|
9668
|
+
setSyncStatus: (widgetId, status) => {
|
9669
|
+
rebuyDebugLog.log("rebuy-widget" /* WIDGET */, `[GWP Store] Setting sync status for widget ${widgetId} to ${status}`);
|
9670
|
+
set((state) => ({
|
9671
|
+
// Update timestamp when starting a sync
|
9672
|
+
lastSyncTimestampByWidget: status === "syncing" ? { ...state.lastSyncTimestampByWidget, [widgetId]: Date.now() } : state.lastSyncTimestampByWidget,
|
9673
|
+
syncStatusByWidget: {
|
9674
|
+
...state.syncStatusByWidget,
|
9675
|
+
[widgetId]: status
|
9676
|
+
}
|
9677
|
+
}));
|
9678
|
+
},
|
9679
|
+
shouldSync: (widgetId) => {
|
9680
|
+
const state = get();
|
9681
|
+
const status = state.syncStatusByWidget[widgetId] || "idle";
|
9682
|
+
if (status !== "idle") {
|
9683
|
+
rebuyDebugLog.log(
|
9684
|
+
"rebuy-widget" /* WIDGET */,
|
9685
|
+
`[GWP Store] shouldSync = false for widget ${widgetId}: status is ${status}`
|
9686
|
+
);
|
9687
|
+
return false;
|
9688
|
+
}
|
9689
|
+
const lastSync = state.lastSyncTimestampByWidget[widgetId];
|
9690
|
+
if (lastSync && Date.now() - lastSync < 2e3) {
|
9691
|
+
rebuyDebugLog.log(
|
9692
|
+
"rebuy-widget" /* WIDGET */,
|
9693
|
+
`[GWP Store] shouldSync = false for widget ${widgetId}: synced ${Date.now() - lastSync}ms ago`
|
9694
|
+
);
|
9695
|
+
return false;
|
9696
|
+
}
|
9697
|
+
return true;
|
9698
|
+
}
|
9699
|
+
}))
|
9700
|
+
);
|
9701
|
+
|
9702
|
+
// src/widgets/RebuyGiftWithPurchase/utils.ts
|
9703
|
+
var prepareGiftItems = (variantGids, widgetId, settings = null) => {
|
9704
|
+
return variantGids.map((variantGid) => {
|
9705
|
+
const attributes = [
|
9706
|
+
{ key: "_widget_id", value: String(widgetId) },
|
9707
|
+
{ key: "_attribution", value: "Rebuy Gift with Purchase" /* GIFT_WITH_PURCHASE */ }
|
9708
|
+
];
|
9709
|
+
if (settings?.hide_quantity_selector) {
|
9710
|
+
attributes.push({ key: "_r_gwp_hide_qty" /* HIDE_QUANTITY_SELECTOR_GWP_ITEM */, value: "true" });
|
9711
|
+
}
|
9712
|
+
return {
|
9713
|
+
attributes,
|
9714
|
+
merchandiseId: variantGid,
|
9715
|
+
quantity: 1
|
9716
|
+
};
|
9717
|
+
});
|
9718
|
+
};
|
9719
|
+
var getGiftDeclineKey = (widgetId, productId, cartToken) => {
|
9720
|
+
const token = cartToken || "default";
|
9721
|
+
return `rebuy_gwp_${widgetId}_${token}_${productId}`;
|
9722
|
+
};
|
9723
|
+
var isGiftDeclined = (widgetId, productId, cartToken) => {
|
9724
|
+
const key = getGiftDeclineKey(widgetId, productId, cartToken);
|
9725
|
+
return sessionStorage.getItem(key) === "declined";
|
9726
|
+
};
|
9727
|
+
var hasCartActions = (actions) => {
|
9728
|
+
return Boolean(actions?.linesAdd && actions?.linesRemove);
|
9729
|
+
};
|
9730
|
+
|
9731
|
+
// src/widgets/RebuyGiftWithPurchase/RebuyGiftWithPurchase.tsx
|
9732
|
+
var RebuyGiftWithPurchase = (props) => {
|
9733
|
+
const { widgetId } = props;
|
9734
|
+
const { apiKey, loadingStatus: configLoadingStatus, rebuyConfig } = useRebuyConfig();
|
9735
|
+
const rebuyContext = useContext6(RebuyContext);
|
9736
|
+
const contextParams = rebuyContext?.contextParameters;
|
9737
|
+
const hydrogenCart = contextParams?.hydrogenCart;
|
9738
|
+
const cartToken = hydrogenCart?.id;
|
9739
|
+
const isHydrogenReact = contextParams?.isHydrogenReact;
|
9740
|
+
const hydrogenReactCartActions = contextParams?.hydrogenReactCartActions;
|
9741
|
+
const cartFetcher = useFetcher6();
|
9742
|
+
const [settings, setSettings] = useState24(null);
|
9743
|
+
const [products, setProducts] = useState24([]);
|
9744
|
+
const [loading, setLoading] = useState24(true);
|
9745
|
+
const _syncStatus = useGiftWithPurchaseStore((state) => state.getSyncStatus(String(widgetId)));
|
9746
|
+
const shouldSync = useGiftWithPurchaseStore((state) => state.shouldSync);
|
9747
|
+
const setSyncStatus = useGiftWithPurchaseStore((state) => state.setSyncStatus);
|
9748
|
+
const setLastSyncedGifts = useGiftWithPurchaseStore((state) => state.setLastSyncedGifts);
|
9749
|
+
const setPendingAdditions = useGiftWithPurchaseStore((state) => state.setPendingAdditions);
|
9750
|
+
const _clearPendingAddition = useGiftWithPurchaseStore((state) => state.clearPendingAddition);
|
9751
|
+
const clientRef = useRef14(null);
|
9752
|
+
const isMountedRef = useRef14(true);
|
9753
|
+
const lastCartHashRef = useRef14("");
|
9754
|
+
const cacheKey = rebuyConfig?.shop?.cache_key || contextParams?.cache_key || "";
|
9755
|
+
useEffect25(() => {
|
9756
|
+
if (!apiKey || configLoadingStatus !== "success" || clientRef.current) return;
|
9757
|
+
const client = new RebuyClient4(apiKey);
|
9758
|
+
if (contextParams) {
|
9759
|
+
const filteredParams = filterContextForWidgetEndpoints(contextParams, void 0);
|
9760
|
+
client.setContextParameters(filteredParams);
|
9761
|
+
}
|
9762
|
+
clientRef.current = client;
|
9763
|
+
}, [apiKey, configLoadingStatus, contextParams]);
|
9764
|
+
useEffect25(() => {
|
9765
|
+
if (!widgetId || !clientRef.current || configLoadingStatus !== "success") return;
|
9766
|
+
const fetchSettings = async () => {
|
9767
|
+
try {
|
9768
|
+
const params = { id: widgetId };
|
9769
|
+
if (cacheKey) {
|
9770
|
+
params.cache_key = cacheKey;
|
9771
|
+
}
|
9772
|
+
const response = await clientRef.current.getShieldedAsset("/api/v1/widgets/settings", params);
|
9773
|
+
const parseResult = widgetSettingsSchema.safeParse(response?.data);
|
9774
|
+
if (!parseResult.success) {
|
9775
|
+
throw new Error(`Invalid settings data: ${parseResult.error.message}`);
|
9776
|
+
}
|
9777
|
+
const fetchedSettings = parseResult.data;
|
9778
|
+
if (fetchedSettings.type !== "gift_with_purchase") {
|
9779
|
+
throw new Error(
|
9780
|
+
`Widget ${widgetId} is not a gift_with_purchase widget (type: ${fetchedSettings.type})`
|
9781
|
+
);
|
9782
|
+
}
|
9783
|
+
setSettings(fetchedSettings);
|
9784
|
+
} catch (e) {
|
9785
|
+
console.error(`[RebuyGiftWithPurchase] Error fetching settings for widget ${widgetId}:`, e);
|
9786
|
+
setSettings(null);
|
9787
|
+
} finally {
|
9788
|
+
setLoading(false);
|
9789
|
+
}
|
9790
|
+
};
|
9791
|
+
fetchSettings();
|
9792
|
+
}, [widgetId, cacheKey, configLoadingStatus]);
|
9793
|
+
useEffect25(() => {
|
9794
|
+
if (!settings || !clientRef.current || !contextParams?.cart) return;
|
9795
|
+
const cartHash = JSON.stringify(
|
9796
|
+
contextParams.cart.items?.map((item) => ({
|
9797
|
+
product_id: item.product_id,
|
9798
|
+
quantity: item.quantity,
|
9799
|
+
variant_id: item.variant_id
|
9800
|
+
})) || []
|
9801
|
+
);
|
9802
|
+
if (cartHash === lastCartHashRef.current) return;
|
9803
|
+
lastCartHashRef.current = cartHash;
|
9804
|
+
const fetchProducts = async () => {
|
9805
|
+
try {
|
9806
|
+
const cartItems = contextParams.cart?.items || [];
|
9807
|
+
const productIds = cartItems.map((item) => item.product_id).filter(Boolean);
|
9808
|
+
const variantIds = cartItems.map((item) => item.variant_id).filter(Boolean);
|
9809
|
+
const params = {
|
9810
|
+
cache_key: cacheKey,
|
9811
|
+
key: apiKey,
|
9812
|
+
limit: settings.limit || 8
|
9813
|
+
};
|
9814
|
+
if (productIds.length > 0) {
|
9815
|
+
params.shopify_product_ids = productIds.join(",");
|
9816
|
+
}
|
9817
|
+
if (variantIds.length > 0) {
|
9818
|
+
params.shopify_variant_ids = variantIds.join(",");
|
9819
|
+
}
|
9820
|
+
params.cart_token = contextParams.cart?.token;
|
9821
|
+
params.cart_subtotal = contextParams.cart?.subtotal;
|
9822
|
+
params.cart_item_count = contextParams.cart?.item_count;
|
9823
|
+
params.cart_line_count = contextParams.cart?.line_count;
|
9824
|
+
params.cart = {
|
9825
|
+
attributes: contextParams.cart?.attributes ? encodeURIComponent(JSON.stringify(contextParams.cart.attributes)) : "",
|
9826
|
+
item_count: contextParams.cart?.item_count || 0,
|
9827
|
+
items: cartItems.map((item) => ({
|
9828
|
+
product_id: item.product_id,
|
9829
|
+
properties: item.properties ? encodeURIComponent(JSON.stringify(item.properties)) : "",
|
9830
|
+
quantity: item.quantity,
|
9831
|
+
variant_id: item.variant_id
|
9832
|
+
})),
|
9833
|
+
line_count: contextParams.cart?.line_count || 0,
|
9834
|
+
notes: contextParams.cart?.note || "",
|
9835
|
+
subtotal: contextParams.cart?.subtotal || 0,
|
9836
|
+
token: contextParams.cart?.token || ""
|
9837
|
+
};
|
9838
|
+
let endpoint = settings.endpoint;
|
9839
|
+
if (endpoint && endpoint.startsWith("/") && !endpoint.startsWith("/api/v1/")) {
|
9840
|
+
if (endpoint.startsWith("/custom") || endpoint.startsWith("/rules")) {
|
9841
|
+
endpoint = `/api/v1${endpoint}`;
|
9842
|
+
}
|
9843
|
+
}
|
9844
|
+
const { data } = await clientRef.current.getStorefrontData(endpoint, params);
|
9845
|
+
const fetchedProducts = Array.isArray(data) ? data : [];
|
9846
|
+
setProducts(fetchedProducts);
|
9847
|
+
} catch (e) {
|
9848
|
+
console.error(`[RebuyGiftWithPurchase] Error fetching products:`, e);
|
9849
|
+
if (isMountedRef.current) {
|
9850
|
+
setProducts([]);
|
9851
|
+
}
|
9852
|
+
}
|
9853
|
+
};
|
9854
|
+
fetchProducts();
|
9855
|
+
}, [settings, apiKey, cacheKey, contextParams?.cart]);
|
9856
|
+
const manageGifts = useCallback16(() => {
|
9857
|
+
const widgetIdStr = String(widgetId);
|
9858
|
+
if (!shouldSync(widgetIdStr)) return;
|
9859
|
+
if (!hydrogenCart) return;
|
9860
|
+
setSyncStatus(widgetIdStr, "syncing");
|
9861
|
+
try {
|
9862
|
+
const cartLines = hydrogenCart.lines?.nodes || hydrogenCart.lines?.edges?.map((edge) => edge.node) || [];
|
9863
|
+
const hasInputProducts = (contextParams?.cart?.items?.length || 0) > 0;
|
9864
|
+
const allowGiftsWithoutPurchase = settings?.allow_gifts_without_purchase !== false;
|
9865
|
+
const giftLinesFromWidget = cartLines.filter(
|
9866
|
+
(line) => line.attributes?.some((attr) => attr.key === "_widget_id" && attr.value === String(widgetId))
|
9867
|
+
);
|
9868
|
+
const rebuyCartItems = contextParams?.cart?.items || [];
|
9869
|
+
const existingGiftVariantIds = /* @__PURE__ */ new Set();
|
9870
|
+
for (const item of rebuyCartItems) {
|
9871
|
+
if (item.attributes?.some((attr) => attr.key === "_widget_id" && attr.value === String(widgetId))) {
|
9872
|
+
existingGiftVariantIds.add(String(item.variant_id));
|
9873
|
+
}
|
9874
|
+
}
|
9875
|
+
const hasOnlyGifts = cartLines.length > 0 && cartLines.length === giftLinesFromWidget.length;
|
9876
|
+
const shouldRemoveAllGifts = !hasInputProducts || products.length === 0 || hasOnlyGifts && !allowGiftsWithoutPurchase;
|
9877
|
+
if (shouldRemoveAllGifts) {
|
9878
|
+
if (giftLinesFromWidget.length > 0) {
|
9879
|
+
const lineIdsToRemove = giftLinesFromWidget.map((line) => line.id);
|
9880
|
+
if (isHydrogenReact && hasCartActions(hydrogenReactCartActions)) {
|
9881
|
+
hydrogenReactCartActions.linesRemove(lineIdsToRemove);
|
9882
|
+
} else {
|
9883
|
+
cartFetcher.submit(
|
9884
|
+
{
|
9885
|
+
action: "cartLinesRemove",
|
9886
|
+
data: { lineIds: lineIdsToRemove }
|
9887
|
+
},
|
9888
|
+
{
|
9889
|
+
action: "/cart",
|
9890
|
+
encType: "application/json",
|
9891
|
+
method: "POST"
|
9892
|
+
}
|
9893
|
+
);
|
9894
|
+
}
|
9895
|
+
}
|
9896
|
+
return;
|
9897
|
+
}
|
9898
|
+
const giftsToEnsure = /* @__PURE__ */ new Map();
|
9899
|
+
for (const giftProduct of products) {
|
9900
|
+
let variantId;
|
9901
|
+
const productWithEdges = giftProduct;
|
9902
|
+
if (productWithEdges.variants?.nodes?.[0]?.id) {
|
9903
|
+
variantId = String(productWithEdges.variants.nodes[0].id);
|
9904
|
+
} else if (productWithEdges.variants?.edges?.[0]?.node?.id) {
|
9905
|
+
variantId = String(productWithEdges.variants.edges[0].node.id);
|
9906
|
+
}
|
9907
|
+
if (variantId) {
|
9908
|
+
giftsToEnsure.set(variantId, String(giftProduct.id));
|
9909
|
+
}
|
9910
|
+
}
|
9911
|
+
const variantsToAdd = [];
|
9912
|
+
for (const [variantId, productId] of giftsToEnsure) {
|
9913
|
+
const inHydrogenCart = giftLinesFromWidget.some((line) => line.merchandise?.id === variantId);
|
9914
|
+
const inRebuyCart = existingGiftVariantIds.has(variantId);
|
9915
|
+
const alreadyInCart = inHydrogenCart || inRebuyCart;
|
9916
|
+
if (!hasInputProducts && !allowGiftsWithoutPurchase) {
|
9917
|
+
continue;
|
9918
|
+
}
|
9919
|
+
if (!alreadyInCart && !isGiftDeclined(String(widgetId), productId, cartToken)) {
|
9920
|
+
variantsToAdd.push(variantId);
|
9921
|
+
}
|
9922
|
+
}
|
9923
|
+
if (variantsToAdd.length > 0) {
|
9924
|
+
setPendingAdditions(widgetIdStr, variantsToAdd);
|
9925
|
+
const itemsToAdd = prepareGiftItems(variantsToAdd, String(widgetId), settings);
|
9926
|
+
if (isHydrogenReact && hasCartActions(hydrogenReactCartActions)) {
|
9927
|
+
hydrogenReactCartActions.linesAdd(itemsToAdd);
|
9928
|
+
} else {
|
9929
|
+
cartFetcher.submit(
|
9930
|
+
{
|
9931
|
+
action: "cartLinesAdd",
|
9932
|
+
data: { lines: itemsToAdd }
|
9933
|
+
},
|
9934
|
+
{
|
9935
|
+
action: "/cart",
|
9936
|
+
encType: "application/json",
|
9937
|
+
method: "POST"
|
9938
|
+
}
|
9939
|
+
);
|
9940
|
+
}
|
9941
|
+
}
|
9942
|
+
const validVariantIds = new Set(Array.from(giftsToEnsure.keys()));
|
9943
|
+
const linesToRemove = giftLinesFromWidget.filter((line) => {
|
9944
|
+
const lineVariantId = line.merchandise?.id;
|
9945
|
+
return lineVariantId && !validVariantIds.has(lineVariantId);
|
9946
|
+
});
|
9947
|
+
if (linesToRemove.length > 0) {
|
9948
|
+
const lineIdsToRemove = linesToRemove.map((line) => line.id);
|
9949
|
+
if (isHydrogenReact && hasCartActions(hydrogenReactCartActions)) {
|
9950
|
+
hydrogenReactCartActions.linesRemove(lineIdsToRemove);
|
9951
|
+
} else {
|
9952
|
+
cartFetcher.submit(
|
9953
|
+
{
|
9954
|
+
action: "cartLinesRemove",
|
9955
|
+
data: { lineIds: lineIdsToRemove }
|
9956
|
+
},
|
9957
|
+
{
|
9958
|
+
action: "/cart",
|
9959
|
+
encType: "application/json",
|
9960
|
+
method: "POST"
|
9961
|
+
}
|
9962
|
+
);
|
9963
|
+
}
|
9964
|
+
}
|
9965
|
+
const currentGiftIds = Array.from(giftsToEnsure.values());
|
9966
|
+
setLastSyncedGifts(widgetIdStr, currentGiftIds);
|
9967
|
+
} catch (error) {
|
9968
|
+
console.error("[RebuyGiftWithPurchase] Error managing gifts:", error);
|
9969
|
+
} finally {
|
9970
|
+
setTimeout(() => setSyncStatus(widgetIdStr, "idle"), 1e3);
|
9971
|
+
}
|
9972
|
+
}, [
|
9973
|
+
hydrogenCart,
|
9974
|
+
products,
|
9975
|
+
widgetId,
|
9976
|
+
cartToken,
|
9977
|
+
isHydrogenReact,
|
9978
|
+
hydrogenReactCartActions,
|
9979
|
+
cartFetcher,
|
9980
|
+
contextParams?.cart?.items,
|
9981
|
+
shouldSync,
|
9982
|
+
setSyncStatus,
|
9983
|
+
setPendingAdditions,
|
9984
|
+
setLastSyncedGifts,
|
9985
|
+
settings
|
9986
|
+
]);
|
9987
|
+
useEffect25(() => {
|
9988
|
+
if (!loading && settings && hydrogenCart) {
|
9989
|
+
const timer = setTimeout(manageGifts, 500);
|
9990
|
+
return () => clearTimeout(timer);
|
9991
|
+
}
|
9992
|
+
}, [products, hydrogenCart, loading, settings, manageGifts]);
|
9993
|
+
useEffect25(() => {
|
9994
|
+
return () => {
|
9995
|
+
isMountedRef.current = false;
|
9996
|
+
};
|
9997
|
+
}, []);
|
9998
|
+
return null;
|
9999
|
+
};
|
10000
|
+
|
9564
10001
|
// src/widgets/RebuyProductAddOns/RebuyProductAddOns.tsx
|
9565
10002
|
import { Money as Money5 } from "@shopify/hydrogen-react";
|
9566
|
-
import { useCallback as
|
10003
|
+
import { useCallback as useCallback17, useEffect as useEffect26, useState as useState25 } from "react";
|
9567
10004
|
|
9568
10005
|
// src/widgets/RebuyProductAddOns/RebuyProductAddOnCard.tsx
|
9569
10006
|
import { Image as Image3 } from "@shopify/hydrogen";
|
@@ -9640,11 +10077,11 @@ var RebuyProductAddOns = (props) => {
|
|
9640
10077
|
const { isHydrogenReact, product, products } = useRebuyWidget();
|
9641
10078
|
const productTitleClean = customTitle.replace("{product_title}", product?.title ?? "");
|
9642
10079
|
const withProductTextClean = withProductText.replace("{product_title}", product?.title ?? "");
|
9643
|
-
const [addedItems, setAddedItems] =
|
9644
|
-
const [subtotalWithProduct, setSubtotalWithProduct] =
|
9645
|
-
const [subtotalWithOutProduct, setSubtotalWithOutProduct] =
|
10080
|
+
const [addedItems, setAddedItems] = useState25(products);
|
10081
|
+
const [subtotalWithProduct, setSubtotalWithProduct] = useState25();
|
10082
|
+
const [subtotalWithOutProduct, setSubtotalWithOutProduct] = useState25();
|
9646
10083
|
const convertedProduct = convertToRebuyProduct(isHydrogenReact || false, product);
|
9647
|
-
|
10084
|
+
useEffect26(() => {
|
9648
10085
|
let initialTotal = 0;
|
9649
10086
|
let currencyCode = "USD";
|
9650
10087
|
products.map((product2) => {
|
@@ -9674,7 +10111,7 @@ var RebuyProductAddOns = (props) => {
|
|
9674
10111
|
setAddedItems(products);
|
9675
10112
|
}
|
9676
10113
|
}, [products, product, isHydrogenReact, includeMainProduct]);
|
9677
|
-
const handleChange =
|
10114
|
+
const handleChange = useCallback17(
|
9678
10115
|
(event, product2) => {
|
9679
10116
|
const newProducts = [...products];
|
9680
10117
|
const productIndex = newProducts.findIndex((p) => p.id === product2.id);
|
@@ -9693,7 +10130,7 @@ var RebuyProductAddOns = (props) => {
|
|
9693
10130
|
},
|
9694
10131
|
[products]
|
9695
10132
|
);
|
9696
|
-
|
10133
|
+
useEffect26(() => {
|
9697
10134
|
let total = 0;
|
9698
10135
|
let currencyCode = "USD";
|
9699
10136
|
addedItems.forEach((item) => {
|
@@ -9801,13 +10238,13 @@ var RebuyProductRecommendations = (props) => {
|
|
9801
10238
|
};
|
9802
10239
|
|
9803
10240
|
// src/widgets/RebuyProductViewed/RebuyProductViewed.tsx
|
9804
|
-
import { RebuyClient as
|
10241
|
+
import { RebuyClient as RebuyClient5 } from "@rebuy/rebuy";
|
9805
10242
|
import * as Utilities6 from "@rebuy/rebuy/utilities";
|
9806
|
-
import { useEffect as
|
10243
|
+
import { useEffect as useEffect27, useMemo as useMemo22, useState as useState26 } from "react";
|
9807
10244
|
|
9808
10245
|
// src/zustandStores/productViewed.ts
|
9809
|
-
import { create as
|
9810
|
-
var useProductViewedStore =
|
10246
|
+
import { create as create3 } from "zustand";
|
10247
|
+
var useProductViewedStore = create3((set) => ({
|
9811
10248
|
customerId: null,
|
9812
10249
|
setCustomerId: (customerId) => set({ customerId }),
|
9813
10250
|
setUuid: (uuid) => set({ uuid }),
|
@@ -9818,10 +10255,10 @@ var useProductViewedStore = create2((set) => ({
|
|
9818
10255
|
var RebuyProductViewed = ({ ...props }) => {
|
9819
10256
|
const { apiKey: apiKeyFromContext } = useRebuyConfig();
|
9820
10257
|
const { customerId, product, productHandle, productId } = props;
|
9821
|
-
const [event, setEvent] =
|
9822
|
-
const [Rebuy, setRebuy] =
|
10258
|
+
const [event, setEvent] = useState26(null);
|
10259
|
+
const [Rebuy, setRebuy] = useState26(null);
|
9823
10260
|
const shopifyProductId = product?.id ?? productId ?? null;
|
9824
|
-
const [initialized, setInitialized] =
|
10261
|
+
const [initialized, setInitialized] = useState26(false);
|
9825
10262
|
const { setCustomerId, setUuid } = useProductViewedStore();
|
9826
10263
|
const request = useMemo22(() => {
|
9827
10264
|
const request2 = {
|
@@ -9837,7 +10274,7 @@ var RebuyProductViewed = ({ ...props }) => {
|
|
9837
10274
|
}
|
9838
10275
|
return request2;
|
9839
10276
|
}, [productHandle, shopifyProductId]);
|
9840
|
-
|
10277
|
+
useEffect27(() => {
|
9841
10278
|
const recordView = async () => {
|
9842
10279
|
if (!Rebuy) {
|
9843
10280
|
return;
|
@@ -9856,7 +10293,7 @@ var RebuyProductViewed = ({ ...props }) => {
|
|
9856
10293
|
}
|
9857
10294
|
};
|
9858
10295
|
if (!initialized && apiKeyFromContext) {
|
9859
|
-
const client = new
|
10296
|
+
const client = new RebuyClient5(apiKeyFromContext);
|
9860
10297
|
setRebuy(client);
|
9861
10298
|
setInitialized(true);
|
9862
10299
|
return;
|
@@ -9871,9 +10308,9 @@ var RebuyProductViewed = ({ ...props }) => {
|
|
9871
10308
|
};
|
9872
10309
|
|
9873
10310
|
// src/widgets/RebuyRecentlyViewedProducts/RebuyRecentlyViewedProducts.tsx
|
9874
|
-
import { RebuyClient as
|
10311
|
+
import { RebuyClient as RebuyClient6 } from "@rebuy/rebuy";
|
9875
10312
|
import { flattenConnection as flattenConnection5 } from "@shopify/hydrogen";
|
9876
|
-
import { useContext as
|
10313
|
+
import { useContext as useContext7, useEffect as useEffect28, useMemo as useMemo23, useState as useState27 } from "react";
|
9877
10314
|
|
9878
10315
|
// src/widgets/RebuyRecentlyViewedProducts/RebuyRecentlyViewedProducts.module.css
|
9879
10316
|
var result27 = { "rebuy-recently-viewed-products-container": "RebuyRecentlyViewedProducts_rebuy-recently-viewed-products-container", "product-grid": "RebuyRecentlyViewedProducts_product-grid" };
|
@@ -9894,16 +10331,16 @@ var RebuyRecentlyViewedProducts = (props) => {
|
|
9894
10331
|
} = props;
|
9895
10332
|
const { customerId, uuid } = useProductViewedStore();
|
9896
10333
|
const { apiKey, loadingStatus: configLoadingStatus, rebuyConfig } = useRebuyConfig();
|
9897
|
-
const rebuyContext =
|
9898
|
-
const [rebuyApiClient, setRebuyApiClient] =
|
9899
|
-
const [initialized, setInitialized] =
|
9900
|
-
const [products, setProducts] =
|
9901
|
-
const [metadata, setMetadata] =
|
10334
|
+
const rebuyContext = useContext7(RebuyContext);
|
10335
|
+
const [rebuyApiClient, setRebuyApiClient] = useState27(null);
|
10336
|
+
const [initialized, setInitialized] = useState27(false);
|
10337
|
+
const [products, setProducts] = useState27([]);
|
10338
|
+
const [metadata, setMetadata] = useState27();
|
9902
10339
|
const rebuyConfigKey = rebuyConfig?.shop?.cache_key;
|
9903
10340
|
const endpoint = customerId && customerId !== "" ? `/api/v1/products/viewed?key=${rebuyConfigKey}&customer_id=${customerId}` : `/api/v1/products/viewed?key=${rebuyConfigKey}&uuid=${uuid}`;
|
9904
|
-
|
10341
|
+
useEffect28(() => {
|
9905
10342
|
if (!rebuyApiClient && apiKey && configLoadingStatus === "success") {
|
9906
|
-
const client = new
|
10343
|
+
const client = new RebuyClient6(apiKey);
|
9907
10344
|
if (rebuyContext?.contextParameters) {
|
9908
10345
|
const filteredParams = filterContextForWidgetEndpoints(rebuyContext.contextParameters, endpoint);
|
9909
10346
|
client.setContextParameters(filteredParams);
|
@@ -9915,7 +10352,7 @@ var RebuyRecentlyViewedProducts = (props) => {
|
|
9915
10352
|
setInitialized(true);
|
9916
10353
|
}
|
9917
10354
|
}, [apiKey, rebuyApiClient, configLoadingStatus, options, rebuyContext, uuid, endpoint]);
|
9918
|
-
|
10355
|
+
useEffect28(() => {
|
9919
10356
|
if (rebuyApiClient && rebuyContext?.contextParameters) {
|
9920
10357
|
const filteredParams = filterContextForWidgetEndpoints(rebuyContext.contextParameters, endpoint);
|
9921
10358
|
rebuyApiClient.setContextParameters(filteredParams);
|
@@ -9932,7 +10369,7 @@ var RebuyRecentlyViewedProducts = (props) => {
|
|
9932
10369
|
}
|
9933
10370
|
return req;
|
9934
10371
|
}, [limit, uuid, endpoint]);
|
9935
|
-
|
10372
|
+
useEffect28(() => {
|
9936
10373
|
let isMounted = true;
|
9937
10374
|
if (!rebuyApiClient || !initialized || configLoadingStatus !== "success") return;
|
9938
10375
|
const fetchData = async () => {
|
@@ -10001,6 +10438,7 @@ export {
|
|
10001
10438
|
RebuyCompleteTheLook,
|
10002
10439
|
RebuyConfigProvider,
|
10003
10440
|
RebuyDynamicBundleProducts,
|
10441
|
+
RebuyGiftWithPurchase,
|
10004
10442
|
RebuyHydrogenContextProvider,
|
10005
10443
|
RebuyHydrogenReactContextProvider,
|
10006
10444
|
RebuyProductAddOns,
|