@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.
@@ -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 { e as PlusMemberSettingsMetafields } from '../types-DntkHhf8.mjs';
7
+ import { e as PlusMemberSettingsMetafields } from '../types-CMMWxyUF.mjs';
8
8
 
9
9
  interface ShopifyContextValue {
10
10
  client: ShopifyClient;
@@ -102,6 +102,12 @@ interface CartContextValue {
102
102
  variant: HasMetafieldsIdentifier[];
103
103
  product: HasMetafieldsIdentifier[];
104
104
  };
105
+ /** User profile */
106
+ profile?: any;
107
+ /** Customer data */
108
+ customer?: any;
109
+ /** Plus member settings */
110
+ memberSetting?: PlusMemberSettingsMetafields;
105
111
  }
106
112
  interface CartProviderProps {
107
113
  children: React__default.ReactNode;
@@ -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 { e as PlusMemberSettingsMetafields } from '../types-DntkHhf8.js';
7
+ import { e as PlusMemberSettingsMetafields } from '../types-CMMWxyUF.js';
8
8
 
9
9
  interface ShopifyContextValue {
10
10
  client: ShopifyClient;
@@ -102,6 +102,12 @@ interface CartContextValue {
102
102
  variant: HasMetafieldsIdentifier[];
103
103
  product: HasMetafieldsIdentifier[];
104
104
  };
105
+ /** User profile */
106
+ profile?: any;
107
+ /** Customer data */
108
+ customer?: any;
109
+ /** Plus member settings */
110
+ memberSetting?: PlusMemberSettingsMetafields;
105
111
  }
106
112
  interface CartProviderProps {
107
113
  children: React__default.ReactNode;
@@ -557,7 +557,8 @@ var useScriptAutoFreeGift = ({
557
557
  _giveaway,
558
558
  cart,
559
559
  locale: providedLocale,
560
- lines
560
+ lines,
561
+ profile
561
562
  }) => {
562
563
  const { client, locale: contextLocale } = useShopify();
563
564
  const locale = providedLocale || contextLocale;
@@ -581,8 +582,9 @@ var useScriptAutoFreeGift = ({
581
582
  const utmCampaign = Cookies5__default.default.get("utm_campaign") || query?.utm_campaign;
582
583
  if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
583
584
  return false;
585
+ if (campaign.requireLogin && !profile?.email) return false;
584
586
  return true;
585
- }, [campaign]);
587
+ }, [campaign, profile]);
586
588
  const [upgrade_multiple, upgrade_value] = react.useMemo(() => {
587
589
  let upgrade_multiple2 = 1;
588
590
  let upgrade_value2 = 0;
@@ -711,63 +713,6 @@ var useScriptAutoFreeGift = ({
711
713
  giftProductsResult: finalGiftProductsResult
712
714
  };
713
715
  };
714
- function useUpdateCartAttributes({
715
- mutate,
716
- metafieldIdentifiers,
717
- disabled = false,
718
- swrOptions
719
- }) {
720
- const { client, locale, cartCookieAdapter } = useShopify();
721
- const updateAttributes = react.useCallback(
722
- async (_key, { arg }) => {
723
- if (disabled || !cartCookieAdapter?.getCartId(locale)) {
724
- return void 0;
725
- }
726
- const updatedCart = await shopifySdk.updateCartAttributes(client, {
727
- ...arg,
728
- metafieldIdentifiers,
729
- cookieAdapter: cartCookieAdapter
730
- });
731
- if (updatedCart) {
732
- mutate(updatedCart);
733
- }
734
- return updatedCart;
735
- },
736
- [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
737
- );
738
- return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
739
- }
740
- function useHasPlusMemberInCart({
741
- memberSetting,
742
- cart
743
- }) {
744
- const { plus_monthly_product, plus_annual_product } = memberSetting || {};
745
- return react.useMemo(() => {
746
- if (!cart?.lineItems) {
747
- return {
748
- hasPlusMember: false,
749
- hasMonthlyPlus: false,
750
- hasAnnualPlus: false
751
- };
752
- }
753
- const monthlyPlusItem = cart.lineItems.find(
754
- (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
755
- );
756
- const annualPlusItem = cart.lineItems.find(
757
- (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
758
- );
759
- const hasMonthlyPlus = !!monthlyPlusItem;
760
- const hasAnnualPlus = !!annualPlusItem;
761
- const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
762
- return {
763
- hasPlusMember,
764
- hasMonthlyPlus,
765
- hasAnnualPlus,
766
- monthlyPlusItem,
767
- annualPlusItem
768
- };
769
- }, [cart?.lineItems, plus_monthly_product, plus_annual_product]);
770
- }
771
716
  var getReferralAttributes = () => {
772
717
  const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
773
718
  const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
@@ -784,158 +729,130 @@ var getReferralAttributes = () => {
784
729
  }
785
730
  return [];
786
731
  };
732
+ var getUserType = (customer) => {
733
+ let userInfo = Cookies5__default.default.get("userInfo");
734
+ if (userInfo) {
735
+ userInfo = JSON.parse(userInfo);
736
+ let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
737
+ userInfo.setId = arr[arr.length - 1];
738
+ }
739
+ const customerInfo = userInfo || customer;
740
+ if (!customerInfo) {
741
+ return "new_user_unlogin";
742
+ }
743
+ if (customer) {
744
+ const { orders = {} } = customer;
745
+ if (orders?.edges?.length === 1) {
746
+ return "old_user_orders_once";
747
+ } else if (orders?.edges?.length > 1) {
748
+ return "old_user_orders_twice";
749
+ }
750
+ }
751
+ return "new_user_login";
752
+ };
753
+ function getCartAttributes({
754
+ profile,
755
+ customer,
756
+ cart,
757
+ memberType,
758
+ currentUrl = ""
759
+ }) {
760
+ const userType = getUserType(customer);
761
+ const memberAttributes = [
762
+ {
763
+ key: "_token",
764
+ value: profile?.token
765
+ },
766
+ {
767
+ key: "_member_type",
768
+ value: memberType ?? String(profile?.memberType || 0)
769
+ },
770
+ {
771
+ key: "_user_type",
772
+ value: userType
773
+ },
774
+ {
775
+ key: "_is_login",
776
+ value: profile?.token ? "true" : "false"
777
+ }
778
+ ];
779
+ if (profile?.token) {
780
+ memberAttributes.push({
781
+ key: "_login_user",
782
+ value: "1"
783
+ });
784
+ }
785
+ const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
786
+ const functionAttributes = [
787
+ {
788
+ key: CUSTOMER_ATTRIBUTE_KEY,
789
+ value: JSON.stringify({
790
+ discount_code: discountCodes,
791
+ user_tags: customer?.tags || []
792
+ })
793
+ }
794
+ ];
795
+ const presellAttributes = [
796
+ {
797
+ key: "_presale",
798
+ value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
799
+ }
800
+ ];
801
+ const weightAttributes = [
802
+ {
803
+ key: "_weight",
804
+ value: cart?.lineItems.reduce((acc, item) => {
805
+ return new Decimal2__default.default(acc).plus(item.variant.weight ?? 0).toNumber();
806
+ }, 0).toString()
807
+ },
808
+ {
809
+ key: "_app_source_name",
810
+ value: "dtc"
811
+ }
812
+ ];
813
+ const trackingAttributes = [
814
+ {
815
+ key: "utm_params",
816
+ value: currentUrl
817
+ }
818
+ ];
819
+ const commonAttributes = [
820
+ ...memberAttributes,
821
+ ...functionAttributes,
822
+ ...presellAttributes,
823
+ ...weightAttributes,
824
+ ...trackingAttributes,
825
+ ...getReferralAttributes()
826
+ ].filter((item) => item?.value);
827
+ const extraAttributesInCart = cart?.customAttributes?.filter(
828
+ (item) => !commonAttributes.some((attr) => attr.key === item.key)
829
+ ) || [];
830
+ return [...commonAttributes, ...extraAttributesInCart].filter((item) => item?.value);
831
+ }
787
832
  var useCartAttributes = ({
788
833
  profile,
789
834
  customer,
790
835
  cart,
791
- memberSetting
836
+ memberType
792
837
  }) => {
793
838
  const [currentUrl, setCurrentUrl] = react.useState("");
794
- const { hasPlusMember } = useHasPlusMemberInCart({
795
- memberSetting,
796
- cart
797
- });
798
839
  react.useEffect(() => {
799
840
  setCurrentUrl(window.location.href);
800
841
  }, []);
801
- const userType = react.useMemo(() => {
802
- let userInfo = Cookies5__default.default.get("userInfo");
803
- if (userInfo) {
804
- userInfo = JSON.parse(userInfo);
805
- let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
806
- userInfo.setId = arr[arr.length - 1];
807
- }
808
- const customerInfo = userInfo || customer;
809
- if (!customerInfo) {
810
- return "new_user_unlogin";
811
- }
812
- if (customer) {
813
- const { orders = {} } = customer;
814
- if (orders?.edges?.length === 1) {
815
- return "old_user_orders_once";
816
- } else if (orders?.edges?.length > 1) {
817
- return "old_user_orders_twice";
818
- }
819
- }
820
- return "new_user_login";
821
- }, [customer]);
822
- const memberAttributes = react.useMemo(() => {
823
- const attributes = [
824
- {
825
- key: "_token",
826
- value: profile?.token
827
- //是否登录
828
- },
829
- {
830
- key: "_member_type",
831
- value: hasPlusMember ? "2" : profile?.memberType
832
- //:0(游客),1(普通会员),2(付费会员)
833
- },
834
- {
835
- key: "_user_type",
836
- value: userType
837
- // n
838
- },
839
- {
840
- key: "_is_login",
841
- value: profile?.token ? "true" : "false"
842
- }
843
- ];
844
- if (profile?.token) {
845
- attributes.push({
846
- key: "_login_user",
847
- value: "1"
848
- });
849
- }
850
- return attributes;
851
- }, [profile?.memberType, profile?.token, userType, hasPlusMember]);
852
- const functionAttributes = react.useMemo(() => {
853
- const hasFunctionEnvAttribute = cart?.lineItems.some(
854
- (item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
855
- );
856
- const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
857
- return hasFunctionEnvAttribute ? [
858
- {
859
- key: "_discounts_function_env",
860
- value: JSON.stringify({
861
- discount_code: discountCodes,
862
- user_tags: customer?.tags || []
863
- })
864
- }
865
- ] : [];
866
- }, [cart, customer]);
867
- const presellAttributes = react.useMemo(() => {
868
- return [
869
- {
870
- key: "_presale",
871
- value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
872
- }
873
- ];
874
- }, [cart]);
875
- const weightAttributes = react.useMemo(() => {
876
- return [
877
- {
878
- key: "_weight",
879
- value: cart?.lineItems.reduce((acc, item) => {
880
- return new Decimal2__default.default(acc).plus(item.variant.weight ?? 0).toNumber();
881
- }, 0).toString()
882
- },
883
- {
884
- key: "_app_source_name",
885
- value: "dtc"
886
- }
887
- ];
888
- }, [cart]);
889
- const trackingAttributes = react.useMemo(() => {
890
- return [
891
- {
892
- key: "utm_params",
893
- value: currentUrl
894
- }
895
- ];
896
- }, [currentUrl]);
897
- const commonAttributes = react.useMemo(
898
- () => [
899
- ...memberAttributes,
900
- ...functionAttributes,
901
- ...presellAttributes,
902
- ...weightAttributes,
903
- ...trackingAttributes,
904
- ...getReferralAttributes()
905
- ].filter((item) => item?.value),
906
- [memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
907
- );
908
- const extraAttributesInCart = react.useMemo(() => {
909
- const commonAttributeKeys = [
910
- // member attributes
911
- "_token",
912
- "_member_type",
913
- "_user_type",
914
- "_is_login",
915
- "_login_user",
916
- // function attributes
917
- "_discounts_function_env",
918
- // presell attributes
919
- "_presale",
920
- // weight attributes
921
- "_weight",
922
- "_app_source_name",
923
- // tracking attributes
924
- "utm_params",
925
- // referral attributes
926
- "_invite_code",
927
- "_play_mode_id",
928
- "_popup"
929
- ];
930
- return cart?.customAttributes?.filter((item) => !commonAttributeKeys.includes(item.key)) || [];
931
- }, [cart]);
842
+ const attributes = react.useMemo(() => {
843
+ return getCartAttributes({
844
+ profile,
845
+ customer,
846
+ cart,
847
+ memberType,
848
+ currentUrl
849
+ });
850
+ }, [profile, customer, cart, memberType, currentUrl]);
932
851
  return react.useMemo(
933
852
  () => ({
934
- attributes: [...commonAttributes, ...extraAttributesInCart].filter(
935
- (item) => item?.value
936
- )
853
+ attributes
937
854
  }),
938
- [commonAttributes, extraAttributesInCart]
855
+ [attributes]
939
856
  );
940
857
  };
941
858
  var useUpdateLineCodeAmountAttributes = ({
@@ -1053,28 +970,13 @@ var useUpdateLineCodeAmountAttributes = ({
1053
970
  }, [loading, setLoadingState]);
1054
971
  };
1055
972
  var createInitialValue = () => ({
1056
- zipCode: "",
1057
973
  plusMemberMetafields: {},
1058
- setZipCode: () => {
1059
- },
1060
- allowNextDayDelivery: false,
1061
- setAllowNextDayDelivery: () => {
1062
- },
1063
- allowThirdDayDelivery: false,
1064
- setAllowThirdDayDelivery: () => {
1065
- },
1066
974
  selectedPlusMemberMode: "free",
1067
975
  setSelectedPlusMemberMode: () => {
1068
976
  },
1069
- showAreaCheckModal: false,
1070
- setShowAreaCheckModal: () => {
1071
- },
1072
977
  selectedShippingMethod: void 0,
1073
978
  setSelectedShippingMethod: () => {
1074
979
  },
1075
- showTip: false,
1076
- setShowTip: () => {
1077
- },
1078
980
  showMoreShippingMethod: false,
1079
981
  setShowMoreShippingMethod: () => {
1080
982
  },
@@ -1090,18 +992,79 @@ var createInitialValue = () => ({
1090
992
  isLoadingCoupon: false
1091
993
  },
1092
994
  selectedPlusMemberVariant: void 0,
1093
- monthlyVariant: void 0,
1094
- annualVariant: void 0,
1095
995
  showPlusMemberBenefit: false,
1096
996
  setShowPlusMemberBenefit: () => {
1097
997
  },
1098
- deleteMarginBottom: false,
1099
- setDeleteMarginBottom: () => {
1100
- },
1101
- profile: void 0,
1102
- locale: void 0
998
+ profile: void 0
1103
999
  });
1104
1000
  react.createContext(createInitialValue());
1001
+ function hasPlusMemberInCart({
1002
+ memberSetting,
1003
+ cart
1004
+ }) {
1005
+ const { plus_monthly_product, plus_annual_product } = memberSetting || {};
1006
+ if (!cart?.lineItems) {
1007
+ return {
1008
+ hasPlusMember: false,
1009
+ hasMonthlyPlus: false,
1010
+ hasAnnualPlus: false
1011
+ };
1012
+ }
1013
+ const monthlyPlusItem = cart.lineItems.find(
1014
+ (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
1015
+ );
1016
+ const annualPlusItem = cart.lineItems.find(
1017
+ (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
1018
+ );
1019
+ const hasMonthlyPlus = !!monthlyPlusItem;
1020
+ const hasAnnualPlus = !!annualPlusItem;
1021
+ const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
1022
+ return {
1023
+ hasPlusMember,
1024
+ hasMonthlyPlus,
1025
+ hasAnnualPlus,
1026
+ monthlyPlusItem,
1027
+ annualPlusItem
1028
+ };
1029
+ }
1030
+ function useHasPlusMemberInCart({
1031
+ memberSetting,
1032
+ cart
1033
+ }) {
1034
+ return react.useMemo(
1035
+ () => hasPlusMemberInCart({
1036
+ memberSetting,
1037
+ cart
1038
+ }),
1039
+ [memberSetting, cart]
1040
+ );
1041
+ }
1042
+ function useUpdateCartAttributes({
1043
+ mutate,
1044
+ metafieldIdentifiers,
1045
+ disabled = false,
1046
+ swrOptions
1047
+ }) {
1048
+ const { client, locale, cartCookieAdapter } = useShopify();
1049
+ const updateAttributes = react.useCallback(
1050
+ async (_key, { arg }) => {
1051
+ if (disabled) {
1052
+ return void 0;
1053
+ }
1054
+ const updatedCart = await shopifySdk.updateCartAttributes(client, {
1055
+ ...arg,
1056
+ metafieldIdentifiers,
1057
+ cookieAdapter: cartCookieAdapter
1058
+ });
1059
+ if (updatedCart) {
1060
+ mutate(updatedCart);
1061
+ }
1062
+ return updatedCart;
1063
+ },
1064
+ [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1065
+ );
1066
+ return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
1067
+ }
1105
1068
  var CartContext = react.createContext(null);
1106
1069
  function CartProvider({
1107
1070
  children,
@@ -1149,7 +1112,17 @@ function CartProvider({
1149
1112
  metafieldIdentifiers,
1150
1113
  disabled: isCartLoading
1151
1114
  });
1152
- const { attributes } = useCartAttributes({ profile, customer, cart, memberSetting });
1115
+ console.log("isCartLoading", isCartLoading);
1116
+ const { hasPlusMember } = useHasPlusMemberInCart({
1117
+ memberSetting,
1118
+ cart
1119
+ });
1120
+ const { attributes } = useCartAttributes({
1121
+ profile,
1122
+ customer,
1123
+ cart,
1124
+ memberType: hasPlusMember ? "2" : profile?.memberType
1125
+ });
1153
1126
  ahooks.useRequest(
1154
1127
  () => {
1155
1128
  const newAttributes = [...attributes];
@@ -1212,7 +1185,8 @@ function CartProvider({
1212
1185
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
1213
1186
  campaign: gradientGiftsConfig || null,
1214
1187
  _giveaway: CUSTOMER_SCRIPT_GIFT_KEY,
1215
- cart
1188
+ cart,
1189
+ profile
1216
1190
  });
1217
1191
  const formattedScriptGifts = react.useMemo(() => {
1218
1192
  return formatScriptAutoFreeGift({
@@ -1279,6 +1253,8 @@ function CartProvider({
1279
1253
  removeCustomAttributes,
1280
1254
  setCustomAttributes,
1281
1255
  locale,
1256
+ profile,
1257
+ customer,
1282
1258
  isCodeChanging,
1283
1259
  setIsCodeChanging,
1284
1260
  autoFreeGiftConfig,
@@ -1294,7 +1270,8 @@ function CartProvider({
1294
1270
  scriptAutoFreeGiftResult,
1295
1271
  setScriptAutoFreeGift,
1296
1272
  giftNeedAddToCartLines,
1297
- metafieldIdentifiers
1273
+ metafieldIdentifiers,
1274
+ memberSetting
1298
1275
  }),
1299
1276
  [
1300
1277
  cart,
@@ -1318,7 +1295,10 @@ function CartProvider({
1318
1295
  scriptAutoFreeGiftResult,
1319
1296
  setScriptAutoFreeGift,
1320
1297
  giftNeedAddToCartLines,
1321
- metafieldIdentifiers
1298
+ metafieldIdentifiers,
1299
+ customer,
1300
+ profile,
1301
+ memberSetting
1322
1302
  ]
1323
1303
  );
1324
1304
  return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });