@anker-in/shopify-react 1.3.0-beta.1 → 1.3.0-beta.3

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.
@@ -2,8 +2,8 @@ import * as swr_mutation from 'swr/mutation';
2
2
  import { SWRMutationConfiguration } from 'swr/mutation';
3
3
  import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
4
4
  import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
5
- import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-CM5QrlnE.mjs';
6
- export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-CM5QrlnE.mjs';
5
+ import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.mjs';
6
+ export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.mjs';
7
7
  import * as swr from 'swr';
8
8
  import swr__default, { SWRConfiguration } from 'swr';
9
9
  import * as swr__internal from 'swr/_internal';
@@ -2,8 +2,8 @@ import * as swr_mutation from 'swr/mutation';
2
2
  import { SWRMutationConfiguration } from 'swr/mutation';
3
3
  import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
4
4
  import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
5
- import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-CM5QrlnE.js';
6
- export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-CM5QrlnE.js';
5
+ import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.js';
6
+ export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.js';
7
7
  import * as swr from 'swr';
8
8
  import swr__default, { SWRConfiguration } from 'swr';
9
9
  import * as swr__internal from 'swr/_internal';
@@ -671,6 +671,177 @@ var useScriptAutoFreeGift = ({
671
671
  giftProductsResult: finalGiftProductsResult
672
672
  };
673
673
  };
674
+ function useRemoveCartLines(options) {
675
+ const { client, locale, cartCookieAdapter } = useShopify();
676
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
677
+ const removeLines = react.useCallback(
678
+ async (_key, { arg }) => {
679
+ const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
680
+ let updatedCart = await shopifySdk.removeCartLines(client, {
681
+ cartId,
682
+ lineIds,
683
+ metafieldIdentifiers,
684
+ cookieAdapter: cartCookieAdapter
685
+ });
686
+ if (updatedCart && autoRemoveInvalidCodes) {
687
+ const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
688
+ if (unApplicableCodes.length > 0) {
689
+ if (onCodesRemoved) {
690
+ const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
691
+ if (handledCart) {
692
+ updatedCart = handledCart;
693
+ }
694
+ } else {
695
+ updatedCart = await shopifySdk.updateCartCodes(client, {
696
+ cartId: updatedCart.id,
697
+ discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
698
+ metafieldIdentifiers,
699
+ cookieAdapter: cartCookieAdapter
700
+ }) || updatedCart;
701
+ }
702
+ }
703
+ }
704
+ if (updatedCart) {
705
+ mutateCart(updatedCart);
706
+ }
707
+ return updatedCart;
708
+ },
709
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
710
+ );
711
+ return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
712
+ }
713
+
714
+ // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
715
+ function useAutoRemoveFreeGifts(options = {}) {
716
+ const {
717
+ removeFunctionGifts = true,
718
+ removeScriptGifts = true,
719
+ isGiftLineItem,
720
+ runOnlyOnceAfterInit = false,
721
+ initDelay = 500
722
+ } = options;
723
+ const [isRemoving, setIsRemoving] = react.useState(false);
724
+ const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
725
+ const [isFinished, setIsFinished] = react.useState(false);
726
+ const { cart } = useCartContext();
727
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
728
+ const giftsToRemove = react.useMemo(() => {
729
+ if (!cart?.lineItems) {
730
+ return [];
731
+ }
732
+ return cart.lineItems.filter((item) => {
733
+ if (removeFunctionGifts) {
734
+ const functionAttr = item.customAttributes?.find(
735
+ (attr) => attr.key === "_discounts_function_env"
736
+ )?.value;
737
+ if (functionAttr) {
738
+ try {
739
+ const functionAttrObj = JSON.parse(functionAttr);
740
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
741
+ return true;
742
+ }
743
+ } catch (error) {
744
+ console.error("Failed to parse _discounts_function_env:", error);
745
+ }
746
+ }
747
+ }
748
+ if (removeScriptGifts) {
749
+ const scriptGiftAttr = item.customAttributes?.find(
750
+ (attr) => attr.key === "_giveaway_gradient_gifts"
751
+ );
752
+ if (scriptGiftAttr) {
753
+ return true;
754
+ }
755
+ }
756
+ if (isGiftLineItem && isGiftLineItem(item)) {
757
+ return true;
758
+ }
759
+ return false;
760
+ });
761
+ }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
762
+ ahooks.useDebounceEffect(
763
+ () => {
764
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
765
+ return;
766
+ }
767
+ if (!cart?.lineItems?.length) {
768
+ return;
769
+ }
770
+ setIsInitialized(true);
771
+ if (giftsToRemove.length === 0) {
772
+ setIsFinished(true);
773
+ }
774
+ },
775
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
776
+ {
777
+ trailing: true,
778
+ wait: initDelay
779
+ }
780
+ );
781
+ react.useEffect(() => {
782
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
783
+ return;
784
+ }
785
+ if (isRemoving || giftsToRemove.length === 0) {
786
+ return;
787
+ }
788
+ const performRemoval = async () => {
789
+ setIsRemoving(true);
790
+ try {
791
+ await removeCartLines2({
792
+ lineIds: giftsToRemove.map((item) => item.id)
793
+ });
794
+ } catch (error) {
795
+ console.error("Failed to remove free gifts:", error);
796
+ } finally {
797
+ setIsRemoving(false);
798
+ if (runOnlyOnceAfterInit) {
799
+ setIsFinished(true);
800
+ }
801
+ }
802
+ };
803
+ performRemoval();
804
+ }, [
805
+ runOnlyOnceAfterInit,
806
+ isInitialized,
807
+ isFinished,
808
+ isRemoving,
809
+ giftsToRemove,
810
+ removeCartLines2
811
+ ]);
812
+ return {
813
+ isRemoving
814
+ };
815
+ }
816
+ function isFunctionGift(line) {
817
+ const functionAttr = line.customAttributes?.find(
818
+ (attr) => attr.key === "_discounts_function_env"
819
+ )?.value;
820
+ if (!functionAttr) {
821
+ return false;
822
+ }
823
+ try {
824
+ const functionAttrObj = JSON.parse(functionAttr);
825
+ return Boolean(
826
+ functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
827
+ );
828
+ } catch {
829
+ return false;
830
+ }
831
+ }
832
+ function isScriptGift(line) {
833
+ return line.customAttributes?.some(
834
+ (attr) => attr.key === "_giveaway_gradient_gifts"
835
+ ) ?? false;
836
+ }
837
+ function isBuyGetGift(line) {
838
+ return line.customAttributes?.some(
839
+ (attr) => attr.key === "_freegift_related_handlesku"
840
+ ) ?? false;
841
+ }
842
+ function isAnyGift(line) {
843
+ return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
844
+ }
674
845
  var CartContext = react.createContext(null);
675
846
  function useCartContext(options) {
676
847
  const context = react.useContext(CartContext);
@@ -1081,145 +1252,6 @@ var getLinesWithAttributes = ({
1081
1252
  return functionLine;
1082
1253
  });
1083
1254
  };
1084
- function useRemoveCartLines(options) {
1085
- const { client, locale, cartCookieAdapter } = useShopify();
1086
- const { mutateCart, metafieldIdentifiers } = useCartContext();
1087
- const removeLines = react.useCallback(
1088
- async (_key, { arg }) => {
1089
- const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
1090
- let updatedCart = await shopifySdk.removeCartLines(client, {
1091
- cartId,
1092
- lineIds,
1093
- metafieldIdentifiers,
1094
- cookieAdapter: cartCookieAdapter
1095
- });
1096
- if (updatedCart && autoRemoveInvalidCodes) {
1097
- const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1098
- if (unApplicableCodes.length > 0) {
1099
- if (onCodesRemoved) {
1100
- const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
1101
- if (handledCart) {
1102
- updatedCart = handledCart;
1103
- }
1104
- } else {
1105
- updatedCart = await shopifySdk.updateCartCodes(client, {
1106
- cartId: updatedCart.id,
1107
- discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
1108
- metafieldIdentifiers,
1109
- cookieAdapter: cartCookieAdapter
1110
- }) || updatedCart;
1111
- }
1112
- }
1113
- }
1114
- if (updatedCart) {
1115
- mutateCart(updatedCart);
1116
- }
1117
- return updatedCart;
1118
- },
1119
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1120
- );
1121
- return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
1122
- }
1123
-
1124
- // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
1125
- function useAutoRemoveFreeGifts(options = {}) {
1126
- const {
1127
- removeFunctionGifts = true,
1128
- removeScriptGifts = true,
1129
- isGiftLineItem
1130
- } = options;
1131
- const [isRemoving, setIsRemoving] = react.useState(false);
1132
- const { cart } = useCartContext();
1133
- const { trigger: removeCartLines2 } = useRemoveCartLines();
1134
- const giftsToRemove = react.useMemo(() => {
1135
- if (!cart?.lineItems) {
1136
- return [];
1137
- }
1138
- return cart.lineItems.filter((item) => {
1139
- if (removeFunctionGifts) {
1140
- const functionAttr = item.customAttributes?.find(
1141
- (attr) => attr.key === "_discounts_function_env"
1142
- )?.value;
1143
- if (functionAttr) {
1144
- try {
1145
- const functionAttrObj = JSON.parse(functionAttr);
1146
- if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
1147
- return true;
1148
- }
1149
- } catch (error) {
1150
- console.error("Failed to parse _discounts_function_env:", error);
1151
- }
1152
- }
1153
- }
1154
- if (removeScriptGifts) {
1155
- const scriptGiftAttr = item.customAttributes?.find(
1156
- (attr) => attr.key === "_giveaway_gradient_gifts"
1157
- );
1158
- if (scriptGiftAttr) {
1159
- return true;
1160
- }
1161
- }
1162
- if (isGiftLineItem && isGiftLineItem(item)) {
1163
- return true;
1164
- }
1165
- return false;
1166
- });
1167
- }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
1168
- react.useEffect(() => {
1169
- if (isRemoving || giftsToRemove.length === 0) {
1170
- return;
1171
- }
1172
- const performRemoval = async () => {
1173
- setIsRemoving(true);
1174
- try {
1175
- await removeCartLines2({
1176
- lineIds: giftsToRemove.map((item) => item.id)
1177
- });
1178
- } catch (error) {
1179
- console.error("Failed to remove free gifts:", error);
1180
- } finally {
1181
- setIsRemoving(false);
1182
- }
1183
- };
1184
- performRemoval();
1185
- }, [
1186
- isRemoving,
1187
- giftsToRemove,
1188
- removeCartLines2
1189
- ]);
1190
- return {
1191
- isRemoving
1192
- };
1193
- }
1194
- function isFunctionGift(line) {
1195
- const functionAttr = line.customAttributes?.find(
1196
- (attr) => attr.key === "_discounts_function_env"
1197
- )?.value;
1198
- if (!functionAttr) {
1199
- return false;
1200
- }
1201
- try {
1202
- const functionAttrObj = JSON.parse(functionAttr);
1203
- return Boolean(
1204
- functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
1205
- );
1206
- } catch {
1207
- return false;
1208
- }
1209
- }
1210
- function isScriptGift(line) {
1211
- return line.customAttributes?.some(
1212
- (attr) => attr.key === "_giveaway_gradient_gifts"
1213
- ) ?? false;
1214
- }
1215
- function isBuyGetGift(line) {
1216
- return line.customAttributes?.some(
1217
- (attr) => attr.key === "_freegift_related_handlesku"
1218
- ) ?? false;
1219
- }
1220
- function isAnyGift(line) {
1221
- return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
1222
- }
1223
1255
  function useCalcGiftsFromLines({
1224
1256
  lines,
1225
1257
  customer,