@anker-in/shopify-react 1.2.7 → 1.2.9-beta.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/hooks/index.d.mts +23 -6
- package/dist/hooks/index.d.ts +23 -6
- package/dist/hooks/index.js +139 -171
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +141 -173
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -61
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -63
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +1 -3
- package/dist/provider/index.d.ts +1 -3
- package/dist/provider/index.js +7 -173
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +9 -175
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-C0UyuPrG.d.mts → types-DfR13pDe.d.mts} +5 -12
- package/dist/{types-C0UyuPrG.d.ts → types-DfR13pDe.d.ts} +5 -12
- package/package.json +17 -17
|
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
|
|
|
4
4
|
import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.mjs';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { SWRConfiguration } from 'swr';
|
|
7
|
-
import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-
|
|
7
|
+
import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-DfR13pDe.mjs';
|
|
8
8
|
import 'decimal.js';
|
|
9
9
|
|
|
10
10
|
interface ShopifyContextValue {
|
|
@@ -117,8 +117,6 @@ interface CartContextValue {
|
|
|
117
117
|
appContext?: AppContext;
|
|
118
118
|
/** Auto remove free gifts options */
|
|
119
119
|
autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
|
|
120
|
-
/** Whether auto removing free gifts is in progress */
|
|
121
|
-
isAutoRemovingFreeGifts: boolean;
|
|
122
120
|
}
|
|
123
121
|
interface CartProviderProps {
|
|
124
122
|
children: React__default.ReactNode;
|
package/dist/provider/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
|
|
|
4
4
|
import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.js';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { SWRConfiguration } from 'swr';
|
|
7
|
-
import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-
|
|
7
|
+
import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-DfR13pDe.js';
|
|
8
8
|
import 'decimal.js';
|
|
9
9
|
|
|
10
10
|
interface ShopifyContextValue {
|
|
@@ -117,8 +117,6 @@ interface CartContextValue {
|
|
|
117
117
|
appContext?: AppContext;
|
|
118
118
|
/** Auto remove free gifts options */
|
|
119
119
|
autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
|
|
120
|
-
/** Whether auto removing free gifts is in progress */
|
|
121
|
-
isAutoRemovingFreeGifts: boolean;
|
|
122
120
|
}
|
|
123
121
|
interface CartProviderProps {
|
|
124
122
|
children: React__default.ReactNode;
|
package/dist/provider/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var jsxRuntime = require('react/jsx-runtime');
|
|
|
7
7
|
var Decimal3 = require('decimal.js');
|
|
8
8
|
var shopifyCore = require('@anker-in/shopify-core');
|
|
9
9
|
var useSWR = require('swr');
|
|
10
|
-
var
|
|
10
|
+
var useSWRMutation8 = require('swr/mutation');
|
|
11
11
|
var ahooks = require('ahooks');
|
|
12
12
|
|
|
13
13
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -15,7 +15,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
15
15
|
var Cookies5__default = /*#__PURE__*/_interopDefault(Cookies5);
|
|
16
16
|
var Decimal3__default = /*#__PURE__*/_interopDefault(Decimal3);
|
|
17
17
|
var useSWR__default = /*#__PURE__*/_interopDefault(useSWR);
|
|
18
|
-
var
|
|
18
|
+
var useSWRMutation8__default = /*#__PURE__*/_interopDefault(useSWRMutation8);
|
|
19
19
|
|
|
20
20
|
// src/provider/context.ts
|
|
21
21
|
var ShopifyContext = react.createContext(null);
|
|
@@ -759,148 +759,6 @@ var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
|
|
|
759
759
|
};
|
|
760
760
|
}
|
|
761
761
|
};
|
|
762
|
-
function useRemoveCartLines(options) {
|
|
763
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
764
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
765
|
-
const removeLines = react.useCallback(
|
|
766
|
-
async (_key, { arg }) => {
|
|
767
|
-
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
768
|
-
let updatedCart = await shopifySdk.removeCartLines(client, {
|
|
769
|
-
cartId,
|
|
770
|
-
lineIds,
|
|
771
|
-
metafieldIdentifiers,
|
|
772
|
-
cookieAdapter: cartCookieAdapter
|
|
773
|
-
});
|
|
774
|
-
if (updatedCart && autoRemoveInvalidCodes) {
|
|
775
|
-
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
776
|
-
if (unApplicableCodes.length > 0) {
|
|
777
|
-
if (onCodesRemoved) {
|
|
778
|
-
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
779
|
-
if (handledCart) {
|
|
780
|
-
updatedCart = handledCart;
|
|
781
|
-
}
|
|
782
|
-
} else {
|
|
783
|
-
updatedCart = await shopifySdk.updateCartCodes(client, {
|
|
784
|
-
cartId: updatedCart.id,
|
|
785
|
-
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
786
|
-
metafieldIdentifiers,
|
|
787
|
-
cookieAdapter: cartCookieAdapter
|
|
788
|
-
}) || updatedCart;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
if (updatedCart) {
|
|
793
|
-
mutateCart(updatedCart);
|
|
794
|
-
}
|
|
795
|
-
return updatedCart;
|
|
796
|
-
},
|
|
797
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
798
|
-
);
|
|
799
|
-
return useSWRMutation6__default.default("remove-cart-lines", removeLines, options);
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
803
|
-
function useAutoRemoveFreeGifts(options = {}) {
|
|
804
|
-
const {
|
|
805
|
-
removeFunctionGifts = true,
|
|
806
|
-
removeScriptGifts = true,
|
|
807
|
-
isGiftLineItem,
|
|
808
|
-
runOnlyOnceAfterInit = false,
|
|
809
|
-
initDelay = 500
|
|
810
|
-
} = options;
|
|
811
|
-
const [isRemoving, setIsRemoving] = react.useState(false);
|
|
812
|
-
const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
|
|
813
|
-
const [isFinished, setIsFinished] = react.useState(false);
|
|
814
|
-
const { cart } = useCartContext();
|
|
815
|
-
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
816
|
-
const giftsToRemove = react.useMemo(() => {
|
|
817
|
-
if (!cart?.lineItems) {
|
|
818
|
-
return [];
|
|
819
|
-
}
|
|
820
|
-
return cart.lineItems.filter((item) => {
|
|
821
|
-
if (removeFunctionGifts) {
|
|
822
|
-
const functionAttr = item.customAttributes?.find(
|
|
823
|
-
(attr) => attr.key === "_discounts_function_env"
|
|
824
|
-
)?.value;
|
|
825
|
-
if (functionAttr) {
|
|
826
|
-
try {
|
|
827
|
-
const functionAttrObj = JSON.parse(functionAttr);
|
|
828
|
-
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
829
|
-
return true;
|
|
830
|
-
}
|
|
831
|
-
} catch (error) {
|
|
832
|
-
console.error("Failed to parse _discounts_function_env:", error);
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
if (removeScriptGifts) {
|
|
837
|
-
const scriptGiftAttr = item.customAttributes?.find(
|
|
838
|
-
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
839
|
-
);
|
|
840
|
-
if (scriptGiftAttr) {
|
|
841
|
-
return true;
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
845
|
-
return true;
|
|
846
|
-
}
|
|
847
|
-
return false;
|
|
848
|
-
});
|
|
849
|
-
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
850
|
-
ahooks.useDebounceEffect(
|
|
851
|
-
() => {
|
|
852
|
-
if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
|
|
853
|
-
return;
|
|
854
|
-
}
|
|
855
|
-
if (!cart?.lineItems?.length) {
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
setIsInitialized(true);
|
|
859
|
-
if (giftsToRemove.length === 0) {
|
|
860
|
-
setIsFinished(true);
|
|
861
|
-
}
|
|
862
|
-
},
|
|
863
|
-
[runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
|
|
864
|
-
{
|
|
865
|
-
trailing: true,
|
|
866
|
-
wait: initDelay
|
|
867
|
-
}
|
|
868
|
-
);
|
|
869
|
-
react.useEffect(() => {
|
|
870
|
-
if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
|
|
871
|
-
return;
|
|
872
|
-
}
|
|
873
|
-
if (isRemoving || giftsToRemove.length === 0) {
|
|
874
|
-
return;
|
|
875
|
-
}
|
|
876
|
-
const performRemoval = async () => {
|
|
877
|
-
setIsRemoving(true);
|
|
878
|
-
try {
|
|
879
|
-
await removeCartLines2({
|
|
880
|
-
lineIds: giftsToRemove.map((item) => item.id)
|
|
881
|
-
});
|
|
882
|
-
} catch (error) {
|
|
883
|
-
console.error("Failed to remove free gifts:", error);
|
|
884
|
-
} finally {
|
|
885
|
-
setIsRemoving(false);
|
|
886
|
-
if (runOnlyOnceAfterInit) {
|
|
887
|
-
setIsFinished(true);
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
};
|
|
891
|
-
performRemoval();
|
|
892
|
-
}, [
|
|
893
|
-
runOnlyOnceAfterInit,
|
|
894
|
-
isInitialized,
|
|
895
|
-
isFinished,
|
|
896
|
-
isRemoving,
|
|
897
|
-
giftsToRemove,
|
|
898
|
-
removeCartLines2
|
|
899
|
-
]);
|
|
900
|
-
return {
|
|
901
|
-
isRemoving
|
|
902
|
-
};
|
|
903
|
-
}
|
|
904
762
|
var getReferralAttributes = () => {
|
|
905
763
|
const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
|
|
906
764
|
const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
|
|
@@ -1317,19 +1175,9 @@ function useUpdateCartAttributes({
|
|
|
1317
1175
|
},
|
|
1318
1176
|
[client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
|
|
1319
1177
|
);
|
|
1320
|
-
return
|
|
1178
|
+
return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
|
|
1321
1179
|
}
|
|
1322
1180
|
var CartContext = react.createContext(null);
|
|
1323
|
-
function AutoRemoveGiftsHandler({
|
|
1324
|
-
options,
|
|
1325
|
-
onRemovingChange
|
|
1326
|
-
}) {
|
|
1327
|
-
const { isRemoving } = useAutoRemoveFreeGifts(options);
|
|
1328
|
-
react.useEffect(() => {
|
|
1329
|
-
onRemovingChange(isRemoving);
|
|
1330
|
-
}, [isRemoving, onRemovingChange]);
|
|
1331
|
-
return null;
|
|
1332
|
-
}
|
|
1333
1181
|
function CartProvider({
|
|
1334
1182
|
children,
|
|
1335
1183
|
// swrOptions,
|
|
@@ -1357,7 +1205,6 @@ function CartProvider({
|
|
|
1357
1205
|
});
|
|
1358
1206
|
const [scriptAutoFreeGift, setScriptAutoFreeGift] = react.useState([]);
|
|
1359
1207
|
const [functionAutoFreeGift, setFunctionAutoFreeGift] = react.useState([]);
|
|
1360
|
-
const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = react.useState(false);
|
|
1361
1208
|
const {
|
|
1362
1209
|
run: fetchCart,
|
|
1363
1210
|
data: cart,
|
|
@@ -1517,9 +1364,7 @@ function CartProvider({
|
|
|
1517
1364
|
const autoRemoveFreeGiftsOptions = react.useMemo(() => {
|
|
1518
1365
|
return {
|
|
1519
1366
|
removeFunctionGifts: !!functionAutoFreeGiftConfig,
|
|
1520
|
-
removeScriptGifts: !!scriptAutoFreeGiftConfig
|
|
1521
|
-
runOnlyOnceAfterInit: true,
|
|
1522
|
-
initDelay: 500
|
|
1367
|
+
removeScriptGifts: !!scriptAutoFreeGiftConfig
|
|
1523
1368
|
};
|
|
1524
1369
|
}, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
|
|
1525
1370
|
const value = react.useMemo(
|
|
@@ -1552,8 +1397,7 @@ function CartProvider({
|
|
|
1552
1397
|
metafieldIdentifiers,
|
|
1553
1398
|
memberSetting,
|
|
1554
1399
|
appContext,
|
|
1555
|
-
autoRemoveFreeGiftsOptions
|
|
1556
|
-
isAutoRemovingFreeGifts
|
|
1400
|
+
autoRemoveFreeGiftsOptions
|
|
1557
1401
|
}),
|
|
1558
1402
|
[
|
|
1559
1403
|
cart,
|
|
@@ -1582,20 +1426,10 @@ function CartProvider({
|
|
|
1582
1426
|
profile,
|
|
1583
1427
|
memberSetting,
|
|
1584
1428
|
appContext,
|
|
1585
|
-
autoRemoveFreeGiftsOptions
|
|
1586
|
-
isAutoRemovingFreeGifts
|
|
1429
|
+
autoRemoveFreeGiftsOptions
|
|
1587
1430
|
]
|
|
1588
1431
|
);
|
|
1589
|
-
return /* @__PURE__ */ jsxRuntime.
|
|
1590
|
-
(functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1591
|
-
AutoRemoveGiftsHandler,
|
|
1592
|
-
{
|
|
1593
|
-
options: autoRemoveFreeGiftsOptions,
|
|
1594
|
-
onRemovingChange: setIsAutoRemovingFreeGifts
|
|
1595
|
-
}
|
|
1596
|
-
),
|
|
1597
|
-
children
|
|
1598
|
-
] });
|
|
1432
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });
|
|
1599
1433
|
}
|
|
1600
1434
|
function useCartContext(options) {
|
|
1601
1435
|
const context = react.useContext(CartContext);
|