@anker-in/shopify-react 0.1.1-beta.42 → 0.1.1-beta.44

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.
@@ -548,7 +548,8 @@ var useScriptAutoFreeGift = ({
548
548
  _giveaway,
549
549
  cart,
550
550
  locale: providedLocale,
551
- lines
551
+ lines,
552
+ profile
552
553
  }) => {
553
554
  const { client, locale: contextLocale } = useShopify();
554
555
  const locale = providedLocale || contextLocale;
@@ -572,8 +573,9 @@ var useScriptAutoFreeGift = ({
572
573
  const utmCampaign = Cookies5.get("utm_campaign") || query?.utm_campaign;
573
574
  if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
574
575
  return false;
576
+ if (campaign.requireLogin && !profile?.email) return false;
575
577
  return true;
576
- }, [campaign]);
578
+ }, [campaign, profile]);
577
579
  const [upgrade_multiple, upgrade_value] = useMemo(() => {
578
580
  let upgrade_multiple2 = 1;
579
581
  let upgrade_value2 = 0;
@@ -702,63 +704,6 @@ var useScriptAutoFreeGift = ({
702
704
  giftProductsResult: finalGiftProductsResult
703
705
  };
704
706
  };
705
- function useUpdateCartAttributes({
706
- mutate,
707
- metafieldIdentifiers,
708
- disabled = false,
709
- swrOptions
710
- }) {
711
- const { client, locale, cartCookieAdapter } = useShopify();
712
- const updateAttributes = useCallback(
713
- async (_key, { arg }) => {
714
- if (disabled || !cartCookieAdapter?.getCartId(locale)) {
715
- return void 0;
716
- }
717
- const updatedCart = await updateCartAttributes(client, {
718
- ...arg,
719
- metafieldIdentifiers,
720
- cookieAdapter: cartCookieAdapter
721
- });
722
- if (updatedCart) {
723
- mutate(updatedCart);
724
- }
725
- return updatedCart;
726
- },
727
- [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
728
- );
729
- return useSWRMutation8("update-cart-attributes", updateAttributes, swrOptions);
730
- }
731
- function useHasPlusMemberInCart({
732
- memberSetting,
733
- cart
734
- }) {
735
- const { plus_monthly_product, plus_annual_product } = memberSetting || {};
736
- return useMemo(() => {
737
- if (!cart?.lineItems) {
738
- return {
739
- hasPlusMember: false,
740
- hasMonthlyPlus: false,
741
- hasAnnualPlus: false
742
- };
743
- }
744
- const monthlyPlusItem = cart.lineItems.find(
745
- (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
746
- );
747
- const annualPlusItem = cart.lineItems.find(
748
- (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
749
- );
750
- const hasMonthlyPlus = !!monthlyPlusItem;
751
- const hasAnnualPlus = !!annualPlusItem;
752
- const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
753
- return {
754
- hasPlusMember,
755
- hasMonthlyPlus,
756
- hasAnnualPlus,
757
- monthlyPlusItem,
758
- annualPlusItem
759
- };
760
- }, [cart?.lineItems, plus_monthly_product, plus_annual_product]);
761
- }
762
707
  var getReferralAttributes = () => {
763
708
  const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
764
709
  const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
@@ -775,158 +720,130 @@ var getReferralAttributes = () => {
775
720
  }
776
721
  return [];
777
722
  };
723
+ var getUserType = (customer) => {
724
+ let userInfo = Cookies5.get("userInfo");
725
+ if (userInfo) {
726
+ userInfo = JSON.parse(userInfo);
727
+ let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
728
+ userInfo.setId = arr[arr.length - 1];
729
+ }
730
+ const customerInfo = userInfo || customer;
731
+ if (!customerInfo) {
732
+ return "new_user_unlogin";
733
+ }
734
+ if (customer) {
735
+ const { orders = {} } = customer;
736
+ if (orders?.edges?.length === 1) {
737
+ return "old_user_orders_once";
738
+ } else if (orders?.edges?.length > 1) {
739
+ return "old_user_orders_twice";
740
+ }
741
+ }
742
+ return "new_user_login";
743
+ };
744
+ function getCartAttributes({
745
+ profile,
746
+ customer,
747
+ cart,
748
+ memberType,
749
+ currentUrl = ""
750
+ }) {
751
+ const userType = getUserType(customer);
752
+ const memberAttributes = [
753
+ {
754
+ key: "_token",
755
+ value: profile?.token
756
+ },
757
+ {
758
+ key: "_member_type",
759
+ value: memberType ?? String(profile?.memberType || 0)
760
+ },
761
+ {
762
+ key: "_user_type",
763
+ value: userType
764
+ },
765
+ {
766
+ key: "_is_login",
767
+ value: profile?.token ? "true" : "false"
768
+ }
769
+ ];
770
+ if (profile?.token) {
771
+ memberAttributes.push({
772
+ key: "_login_user",
773
+ value: "1"
774
+ });
775
+ }
776
+ const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
777
+ const functionAttributes = [
778
+ {
779
+ key: CUSTOMER_ATTRIBUTE_KEY,
780
+ value: JSON.stringify({
781
+ discount_code: discountCodes,
782
+ user_tags: customer?.tags || []
783
+ })
784
+ }
785
+ ];
786
+ const presellAttributes = [
787
+ {
788
+ key: "_presale",
789
+ value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
790
+ }
791
+ ];
792
+ const weightAttributes = [
793
+ {
794
+ key: "_weight",
795
+ value: cart?.lineItems.reduce((acc, item) => {
796
+ return new Decimal2(acc).plus(item.variant.weight ?? 0).toNumber();
797
+ }, 0).toString()
798
+ },
799
+ {
800
+ key: "_app_source_name",
801
+ value: "dtc"
802
+ }
803
+ ];
804
+ const trackingAttributes = [
805
+ {
806
+ key: "utm_params",
807
+ value: currentUrl
808
+ }
809
+ ];
810
+ const commonAttributes = [
811
+ ...memberAttributes,
812
+ ...functionAttributes,
813
+ ...presellAttributes,
814
+ ...weightAttributes,
815
+ ...trackingAttributes,
816
+ ...getReferralAttributes()
817
+ ].filter((item) => item?.value);
818
+ const extraAttributesInCart = cart?.customAttributes?.filter(
819
+ (item) => !commonAttributes.some((attr) => attr.key === item.key)
820
+ ) || [];
821
+ return [...commonAttributes, ...extraAttributesInCart].filter((item) => item?.value);
822
+ }
778
823
  var useCartAttributes = ({
779
824
  profile,
780
825
  customer,
781
826
  cart,
782
- memberSetting
827
+ memberType
783
828
  }) => {
784
829
  const [currentUrl, setCurrentUrl] = useState("");
785
- const { hasPlusMember } = useHasPlusMemberInCart({
786
- memberSetting,
787
- cart
788
- });
789
830
  useEffect(() => {
790
831
  setCurrentUrl(window.location.href);
791
832
  }, []);
792
- const userType = useMemo(() => {
793
- let userInfo = Cookies5.get("userInfo");
794
- if (userInfo) {
795
- userInfo = JSON.parse(userInfo);
796
- let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
797
- userInfo.setId = arr[arr.length - 1];
798
- }
799
- const customerInfo = userInfo || customer;
800
- if (!customerInfo) {
801
- return "new_user_unlogin";
802
- }
803
- if (customer) {
804
- const { orders = {} } = customer;
805
- if (orders?.edges?.length === 1) {
806
- return "old_user_orders_once";
807
- } else if (orders?.edges?.length > 1) {
808
- return "old_user_orders_twice";
809
- }
810
- }
811
- return "new_user_login";
812
- }, [customer]);
813
- const memberAttributes = useMemo(() => {
814
- const attributes = [
815
- {
816
- key: "_token",
817
- value: profile?.token
818
- //是否登录
819
- },
820
- {
821
- key: "_member_type",
822
- value: hasPlusMember ? "2" : profile?.memberType
823
- //:0(游客),1(普通会员),2(付费会员)
824
- },
825
- {
826
- key: "_user_type",
827
- value: userType
828
- // n
829
- },
830
- {
831
- key: "_is_login",
832
- value: profile?.token ? "true" : "false"
833
- }
834
- ];
835
- if (profile?.token) {
836
- attributes.push({
837
- key: "_login_user",
838
- value: "1"
839
- });
840
- }
841
- return attributes;
842
- }, [profile?.memberType, profile?.token, userType, hasPlusMember]);
843
- const functionAttributes = useMemo(() => {
844
- const hasFunctionEnvAttribute = cart?.lineItems.some(
845
- (item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
846
- );
847
- const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
848
- return hasFunctionEnvAttribute ? [
849
- {
850
- key: "_discounts_function_env",
851
- value: JSON.stringify({
852
- discount_code: discountCodes,
853
- user_tags: customer?.tags || []
854
- })
855
- }
856
- ] : [];
857
- }, [cart, customer]);
858
- const presellAttributes = useMemo(() => {
859
- return [
860
- {
861
- key: "_presale",
862
- value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
863
- }
864
- ];
865
- }, [cart]);
866
- const weightAttributes = useMemo(() => {
867
- return [
868
- {
869
- key: "_weight",
870
- value: cart?.lineItems.reduce((acc, item) => {
871
- return new Decimal2(acc).plus(item.variant.weight ?? 0).toNumber();
872
- }, 0).toString()
873
- },
874
- {
875
- key: "_app_source_name",
876
- value: "dtc"
877
- }
878
- ];
879
- }, [cart]);
880
- const trackingAttributes = useMemo(() => {
881
- return [
882
- {
883
- key: "utm_params",
884
- value: currentUrl
885
- }
886
- ];
887
- }, [currentUrl]);
888
- const commonAttributes = useMemo(
889
- () => [
890
- ...memberAttributes,
891
- ...functionAttributes,
892
- ...presellAttributes,
893
- ...weightAttributes,
894
- ...trackingAttributes,
895
- ...getReferralAttributes()
896
- ].filter((item) => item?.value),
897
- [memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
898
- );
899
- const extraAttributesInCart = useMemo(() => {
900
- const commonAttributeKeys = [
901
- // member attributes
902
- "_token",
903
- "_member_type",
904
- "_user_type",
905
- "_is_login",
906
- "_login_user",
907
- // function attributes
908
- "_discounts_function_env",
909
- // presell attributes
910
- "_presale",
911
- // weight attributes
912
- "_weight",
913
- "_app_source_name",
914
- // tracking attributes
915
- "utm_params",
916
- // referral attributes
917
- "_invite_code",
918
- "_play_mode_id",
919
- "_popup"
920
- ];
921
- return cart?.customAttributes?.filter((item) => !commonAttributeKeys.includes(item.key)) || [];
922
- }, [cart]);
833
+ const attributes = useMemo(() => {
834
+ return getCartAttributes({
835
+ profile,
836
+ customer,
837
+ cart,
838
+ memberType,
839
+ currentUrl
840
+ });
841
+ }, [profile, customer, cart, memberType, currentUrl]);
923
842
  return useMemo(
924
843
  () => ({
925
- attributes: [...commonAttributes, ...extraAttributesInCart].filter(
926
- (item) => item?.value
927
- )
844
+ attributes
928
845
  }),
929
- [commonAttributes, extraAttributesInCart]
846
+ [attributes]
930
847
  );
931
848
  };
932
849
  var useUpdateLineCodeAmountAttributes = ({
@@ -1044,28 +961,13 @@ var useUpdateLineCodeAmountAttributes = ({
1044
961
  }, [loading, setLoadingState]);
1045
962
  };
1046
963
  var createInitialValue = () => ({
1047
- zipCode: "",
1048
964
  plusMemberMetafields: {},
1049
- setZipCode: () => {
1050
- },
1051
- allowNextDayDelivery: false,
1052
- setAllowNextDayDelivery: () => {
1053
- },
1054
- allowThirdDayDelivery: false,
1055
- setAllowThirdDayDelivery: () => {
1056
- },
1057
965
  selectedPlusMemberMode: "free",
1058
966
  setSelectedPlusMemberMode: () => {
1059
967
  },
1060
- showAreaCheckModal: false,
1061
- setShowAreaCheckModal: () => {
1062
- },
1063
968
  selectedShippingMethod: void 0,
1064
969
  setSelectedShippingMethod: () => {
1065
970
  },
1066
- showTip: false,
1067
- setShowTip: () => {
1068
- },
1069
971
  showMoreShippingMethod: false,
1070
972
  setShowMoreShippingMethod: () => {
1071
973
  },
@@ -1081,18 +983,79 @@ var createInitialValue = () => ({
1081
983
  isLoadingCoupon: false
1082
984
  },
1083
985
  selectedPlusMemberVariant: void 0,
1084
- monthlyVariant: void 0,
1085
- annualVariant: void 0,
1086
986
  showPlusMemberBenefit: false,
1087
987
  setShowPlusMemberBenefit: () => {
1088
988
  },
1089
- deleteMarginBottom: false,
1090
- setDeleteMarginBottom: () => {
1091
- },
1092
- profile: void 0,
1093
- locale: void 0
989
+ profile: void 0
1094
990
  });
1095
991
  createContext(createInitialValue());
992
+ function hasPlusMemberInCart({
993
+ memberSetting,
994
+ cart
995
+ }) {
996
+ const { plus_monthly_product, plus_annual_product } = memberSetting || {};
997
+ if (!cart?.lineItems) {
998
+ return {
999
+ hasPlusMember: false,
1000
+ hasMonthlyPlus: false,
1001
+ hasAnnualPlus: false
1002
+ };
1003
+ }
1004
+ const monthlyPlusItem = cart.lineItems.find(
1005
+ (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
1006
+ );
1007
+ const annualPlusItem = cart.lineItems.find(
1008
+ (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
1009
+ );
1010
+ const hasMonthlyPlus = !!monthlyPlusItem;
1011
+ const hasAnnualPlus = !!annualPlusItem;
1012
+ const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
1013
+ return {
1014
+ hasPlusMember,
1015
+ hasMonthlyPlus,
1016
+ hasAnnualPlus,
1017
+ monthlyPlusItem,
1018
+ annualPlusItem
1019
+ };
1020
+ }
1021
+ function useHasPlusMemberInCart({
1022
+ memberSetting,
1023
+ cart
1024
+ }) {
1025
+ return useMemo(
1026
+ () => hasPlusMemberInCart({
1027
+ memberSetting,
1028
+ cart
1029
+ }),
1030
+ [memberSetting, cart]
1031
+ );
1032
+ }
1033
+ function useUpdateCartAttributes({
1034
+ mutate,
1035
+ metafieldIdentifiers,
1036
+ disabled = false,
1037
+ swrOptions
1038
+ }) {
1039
+ const { client, locale, cartCookieAdapter } = useShopify();
1040
+ const updateAttributes = useCallback(
1041
+ async (_key, { arg }) => {
1042
+ if (disabled) {
1043
+ return void 0;
1044
+ }
1045
+ const updatedCart = await updateCartAttributes(client, {
1046
+ ...arg,
1047
+ metafieldIdentifiers,
1048
+ cookieAdapter: cartCookieAdapter
1049
+ });
1050
+ if (updatedCart) {
1051
+ mutate(updatedCart);
1052
+ }
1053
+ return updatedCart;
1054
+ },
1055
+ [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1056
+ );
1057
+ return useSWRMutation8("update-cart-attributes", updateAttributes, swrOptions);
1058
+ }
1096
1059
  var CartContext = createContext(null);
1097
1060
  function CartProvider({
1098
1061
  children,
@@ -1140,7 +1103,17 @@ function CartProvider({
1140
1103
  metafieldIdentifiers,
1141
1104
  disabled: isCartLoading
1142
1105
  });
1143
- const { attributes } = useCartAttributes({ profile, customer, cart, memberSetting });
1106
+ console.log("isCartLoading", isCartLoading);
1107
+ const { hasPlusMember } = useHasPlusMemberInCart({
1108
+ memberSetting,
1109
+ cart
1110
+ });
1111
+ const { attributes } = useCartAttributes({
1112
+ profile,
1113
+ customer,
1114
+ cart,
1115
+ memberType: hasPlusMember ? "2" : profile?.memberType
1116
+ });
1144
1117
  useRequest(
1145
1118
  () => {
1146
1119
  const newAttributes = [...attributes];
@@ -1203,7 +1176,8 @@ function CartProvider({
1203
1176
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
1204
1177
  campaign: gradientGiftsConfig || null,
1205
1178
  _giveaway: CUSTOMER_SCRIPT_GIFT_KEY,
1206
- cart
1179
+ cart,
1180
+ profile
1207
1181
  });
1208
1182
  const formattedScriptGifts = useMemo(() => {
1209
1183
  return formatScriptAutoFreeGift({
@@ -1270,6 +1244,8 @@ function CartProvider({
1270
1244
  removeCustomAttributes,
1271
1245
  setCustomAttributes,
1272
1246
  locale,
1247
+ profile,
1248
+ customer,
1273
1249
  isCodeChanging,
1274
1250
  setIsCodeChanging,
1275
1251
  autoFreeGiftConfig,
@@ -1285,7 +1261,8 @@ function CartProvider({
1285
1261
  scriptAutoFreeGiftResult,
1286
1262
  setScriptAutoFreeGift,
1287
1263
  giftNeedAddToCartLines,
1288
- metafieldIdentifiers
1264
+ metafieldIdentifiers,
1265
+ memberSetting
1289
1266
  }),
1290
1267
  [
1291
1268
  cart,
@@ -1309,7 +1286,10 @@ function CartProvider({
1309
1286
  scriptAutoFreeGiftResult,
1310
1287
  setScriptAutoFreeGift,
1311
1288
  giftNeedAddToCartLines,
1312
- metafieldIdentifiers
1289
+ metafieldIdentifiers,
1290
+ customer,
1291
+ profile,
1292
+ memberSetting
1313
1293
  ]
1314
1294
  );
1315
1295
  return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });