@anker-in/shopify-react 1.3.0-beta.0 → 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.
@@ -1,6 +1,6 @@
1
1
  import { createContext, useMemo, useRef, useState, useEffect, useCallback, useContext } from 'react';
2
2
  import useSWRMutation from 'swr/mutation';
3
- import { getProductsByHandles, createCart, updateCartCodes, addCartLines, removeCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
3
+ import { getProductsByHandles, removeCartLines, updateCartCodes, createCart, addCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
  import Decimal3 from 'decimal.js';
@@ -662,6 +662,145 @@ var useScriptAutoFreeGift = ({
662
662
  giftProductsResult: finalGiftProductsResult
663
663
  };
664
664
  };
665
+ function useRemoveCartLines(options) {
666
+ const { client, locale, cartCookieAdapter } = useShopify();
667
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
668
+ const removeLines = useCallback(
669
+ async (_key, { arg }) => {
670
+ const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
671
+ let updatedCart = await removeCartLines(client, {
672
+ cartId,
673
+ lineIds,
674
+ metafieldIdentifiers,
675
+ cookieAdapter: cartCookieAdapter
676
+ });
677
+ if (updatedCart && autoRemoveInvalidCodes) {
678
+ const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
679
+ if (unApplicableCodes.length > 0) {
680
+ if (onCodesRemoved) {
681
+ const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
682
+ if (handledCart) {
683
+ updatedCart = handledCart;
684
+ }
685
+ } else {
686
+ updatedCart = await updateCartCodes(client, {
687
+ cartId: updatedCart.id,
688
+ discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
689
+ metafieldIdentifiers,
690
+ cookieAdapter: cartCookieAdapter
691
+ }) || updatedCart;
692
+ }
693
+ }
694
+ }
695
+ if (updatedCart) {
696
+ mutateCart(updatedCart);
697
+ }
698
+ return updatedCart;
699
+ },
700
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
701
+ );
702
+ return useSWRMutation("remove-cart-lines", removeLines, options);
703
+ }
704
+
705
+ // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
706
+ function useAutoRemoveFreeGifts(options = {}) {
707
+ const {
708
+ removeFunctionGifts = true,
709
+ removeScriptGifts = true,
710
+ isGiftLineItem
711
+ } = options;
712
+ const [isRemoving, setIsRemoving] = useState(false);
713
+ const { cart } = useCartContext();
714
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
715
+ const giftsToRemove = useMemo(() => {
716
+ if (!cart?.lineItems) {
717
+ return [];
718
+ }
719
+ return cart.lineItems.filter((item) => {
720
+ if (removeFunctionGifts) {
721
+ const functionAttr = item.customAttributes?.find(
722
+ (attr) => attr.key === "_discounts_function_env"
723
+ )?.value;
724
+ if (functionAttr) {
725
+ try {
726
+ const functionAttrObj = JSON.parse(functionAttr);
727
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
728
+ return true;
729
+ }
730
+ } catch (error) {
731
+ console.error("Failed to parse _discounts_function_env:", error);
732
+ }
733
+ }
734
+ }
735
+ if (removeScriptGifts) {
736
+ const scriptGiftAttr = item.customAttributes?.find(
737
+ (attr) => attr.key === "_giveaway_gradient_gifts"
738
+ );
739
+ if (scriptGiftAttr) {
740
+ return true;
741
+ }
742
+ }
743
+ if (isGiftLineItem && isGiftLineItem(item)) {
744
+ return true;
745
+ }
746
+ return false;
747
+ });
748
+ }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
749
+ useEffect(() => {
750
+ if (isRemoving || giftsToRemove.length === 0) {
751
+ return;
752
+ }
753
+ const performRemoval = async () => {
754
+ setIsRemoving(true);
755
+ try {
756
+ await removeCartLines2({
757
+ lineIds: giftsToRemove.map((item) => item.id)
758
+ });
759
+ } catch (error) {
760
+ console.error("Failed to remove free gifts:", error);
761
+ } finally {
762
+ setIsRemoving(false);
763
+ }
764
+ };
765
+ performRemoval();
766
+ }, [
767
+ isRemoving,
768
+ giftsToRemove,
769
+ removeCartLines2
770
+ ]);
771
+ return {
772
+ isRemoving
773
+ };
774
+ }
775
+ function isFunctionGift(line) {
776
+ const functionAttr = line.customAttributes?.find(
777
+ (attr) => attr.key === "_discounts_function_env"
778
+ )?.value;
779
+ if (!functionAttr) {
780
+ return false;
781
+ }
782
+ try {
783
+ const functionAttrObj = JSON.parse(functionAttr);
784
+ return Boolean(
785
+ functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
786
+ );
787
+ } catch {
788
+ return false;
789
+ }
790
+ }
791
+ function isScriptGift(line) {
792
+ return line.customAttributes?.some(
793
+ (attr) => attr.key === "_giveaway_gradient_gifts"
794
+ ) ?? false;
795
+ }
796
+ function isBuyGetGift(line) {
797
+ return line.customAttributes?.some(
798
+ (attr) => attr.key === "_freegift_related_handlesku"
799
+ ) ?? false;
800
+ }
801
+ function isAnyGift(line) {
802
+ return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
803
+ }
665
804
  var CartContext = createContext(null);
666
805
  function useCartContext(options) {
667
806
  const context = useContext(CartContext);
@@ -1072,145 +1211,6 @@ var getLinesWithAttributes = ({
1072
1211
  return functionLine;
1073
1212
  });
1074
1213
  };
1075
- function useRemoveCartLines(options) {
1076
- const { client, locale, cartCookieAdapter } = useShopify();
1077
- const { mutateCart, metafieldIdentifiers } = useCartContext();
1078
- const removeLines = useCallback(
1079
- async (_key, { arg }) => {
1080
- const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
1081
- let updatedCart = await removeCartLines(client, {
1082
- cartId,
1083
- lineIds,
1084
- metafieldIdentifiers,
1085
- cookieAdapter: cartCookieAdapter
1086
- });
1087
- if (updatedCart && autoRemoveInvalidCodes) {
1088
- const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1089
- if (unApplicableCodes.length > 0) {
1090
- if (onCodesRemoved) {
1091
- const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
1092
- if (handledCart) {
1093
- updatedCart = handledCart;
1094
- }
1095
- } else {
1096
- updatedCart = await updateCartCodes(client, {
1097
- cartId: updatedCart.id,
1098
- discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
1099
- metafieldIdentifiers,
1100
- cookieAdapter: cartCookieAdapter
1101
- }) || updatedCart;
1102
- }
1103
- }
1104
- }
1105
- if (updatedCart) {
1106
- mutateCart(updatedCart);
1107
- }
1108
- return updatedCart;
1109
- },
1110
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1111
- );
1112
- return useSWRMutation("remove-cart-lines", removeLines, options);
1113
- }
1114
-
1115
- // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
1116
- function useAutoRemoveFreeGifts(options = {}) {
1117
- const {
1118
- removeFunctionGifts = true,
1119
- removeScriptGifts = true,
1120
- isGiftLineItem
1121
- } = options;
1122
- const [isRemoving, setIsRemoving] = useState(false);
1123
- const { cart } = useCartContext();
1124
- const { trigger: removeCartLines2 } = useRemoveCartLines();
1125
- const giftsToRemove = useMemo(() => {
1126
- if (!cart?.lineItems) {
1127
- return [];
1128
- }
1129
- return cart.lineItems.filter((item) => {
1130
- if (removeFunctionGifts) {
1131
- const functionAttr = item.customAttributes?.find(
1132
- (attr) => attr.key === "_discounts_function_env"
1133
- )?.value;
1134
- if (functionAttr) {
1135
- try {
1136
- const functionAttrObj = JSON.parse(functionAttr);
1137
- if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
1138
- return true;
1139
- }
1140
- } catch (error) {
1141
- console.error("Failed to parse _discounts_function_env:", error);
1142
- }
1143
- }
1144
- }
1145
- if (removeScriptGifts) {
1146
- const scriptGiftAttr = item.customAttributes?.find(
1147
- (attr) => attr.key === "_giveaway_gradient_gifts"
1148
- );
1149
- if (scriptGiftAttr) {
1150
- return true;
1151
- }
1152
- }
1153
- if (isGiftLineItem && isGiftLineItem(item)) {
1154
- return true;
1155
- }
1156
- return false;
1157
- });
1158
- }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
1159
- useEffect(() => {
1160
- if (isRemoving || giftsToRemove.length === 0) {
1161
- return;
1162
- }
1163
- const performRemoval = async () => {
1164
- setIsRemoving(true);
1165
- try {
1166
- await removeCartLines2({
1167
- lineIds: giftsToRemove.map((item) => item.id)
1168
- });
1169
- } catch (error) {
1170
- console.error("Failed to remove free gifts:", error);
1171
- } finally {
1172
- setIsRemoving(false);
1173
- }
1174
- };
1175
- performRemoval();
1176
- }, [
1177
- isRemoving,
1178
- giftsToRemove,
1179
- removeCartLines2
1180
- ]);
1181
- return {
1182
- isRemoving
1183
- };
1184
- }
1185
- function isFunctionGift(line) {
1186
- const functionAttr = line.customAttributes?.find(
1187
- (attr) => attr.key === "_discounts_function_env"
1188
- )?.value;
1189
- if (!functionAttr) {
1190
- return false;
1191
- }
1192
- try {
1193
- const functionAttrObj = JSON.parse(functionAttr);
1194
- return Boolean(
1195
- functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
1196
- );
1197
- } catch {
1198
- return false;
1199
- }
1200
- }
1201
- function isScriptGift(line) {
1202
- return line.customAttributes?.some(
1203
- (attr) => attr.key === "_giveaway_gradient_gifts"
1204
- ) ?? false;
1205
- }
1206
- function isBuyGetGift(line) {
1207
- return line.customAttributes?.some(
1208
- (attr) => attr.key === "_freegift_related_handlesku"
1209
- ) ?? false;
1210
- }
1211
- function isAnyGift(line) {
1212
- return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
1213
- }
1214
1214
  function useCalcGiftsFromLines({
1215
1215
  lines,
1216
1216
  customer,