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

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.
@@ -671,6 +671,145 @@ 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
+ } = options;
721
+ const [isRemoving, setIsRemoving] = react.useState(false);
722
+ const { cart } = useCartContext();
723
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
724
+ const giftsToRemove = react.useMemo(() => {
725
+ if (!cart?.lineItems) {
726
+ return [];
727
+ }
728
+ return cart.lineItems.filter((item) => {
729
+ if (removeFunctionGifts) {
730
+ const functionAttr = item.customAttributes?.find(
731
+ (attr) => attr.key === "_discounts_function_env"
732
+ )?.value;
733
+ if (functionAttr) {
734
+ try {
735
+ const functionAttrObj = JSON.parse(functionAttr);
736
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
737
+ return true;
738
+ }
739
+ } catch (error) {
740
+ console.error("Failed to parse _discounts_function_env:", error);
741
+ }
742
+ }
743
+ }
744
+ if (removeScriptGifts) {
745
+ const scriptGiftAttr = item.customAttributes?.find(
746
+ (attr) => attr.key === "_giveaway_gradient_gifts"
747
+ );
748
+ if (scriptGiftAttr) {
749
+ return true;
750
+ }
751
+ }
752
+ if (isGiftLineItem && isGiftLineItem(item)) {
753
+ return true;
754
+ }
755
+ return false;
756
+ });
757
+ }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
758
+ react.useEffect(() => {
759
+ if (isRemoving || giftsToRemove.length === 0) {
760
+ return;
761
+ }
762
+ const performRemoval = async () => {
763
+ setIsRemoving(true);
764
+ try {
765
+ await removeCartLines2({
766
+ lineIds: giftsToRemove.map((item) => item.id)
767
+ });
768
+ } catch (error) {
769
+ console.error("Failed to remove free gifts:", error);
770
+ } finally {
771
+ setIsRemoving(false);
772
+ }
773
+ };
774
+ performRemoval();
775
+ }, [
776
+ isRemoving,
777
+ giftsToRemove,
778
+ removeCartLines2
779
+ ]);
780
+ return {
781
+ isRemoving
782
+ };
783
+ }
784
+ function isFunctionGift(line) {
785
+ const functionAttr = line.customAttributes?.find(
786
+ (attr) => attr.key === "_discounts_function_env"
787
+ )?.value;
788
+ if (!functionAttr) {
789
+ return false;
790
+ }
791
+ try {
792
+ const functionAttrObj = JSON.parse(functionAttr);
793
+ return Boolean(
794
+ functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
795
+ );
796
+ } catch {
797
+ return false;
798
+ }
799
+ }
800
+ function isScriptGift(line) {
801
+ return line.customAttributes?.some(
802
+ (attr) => attr.key === "_giveaway_gradient_gifts"
803
+ ) ?? false;
804
+ }
805
+ function isBuyGetGift(line) {
806
+ return line.customAttributes?.some(
807
+ (attr) => attr.key === "_freegift_related_handlesku"
808
+ ) ?? false;
809
+ }
810
+ function isAnyGift(line) {
811
+ return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
812
+ }
674
813
  var CartContext = react.createContext(null);
675
814
  function useCartContext(options) {
676
815
  const context = react.useContext(CartContext);
@@ -1081,145 +1220,6 @@ var getLinesWithAttributes = ({
1081
1220
  return functionLine;
1082
1221
  });
1083
1222
  };
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
1223
  function useCalcGiftsFromLines({
1224
1224
  lines,
1225
1225
  customer,