@anker-in/shopify-react 0.1.1-beta.43 → 0.1.1-beta.45

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.
@@ -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;
@@ -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,73 +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 hasPlusMemberInCart({
741
- memberSetting,
742
- cart
743
- }) {
744
- const { plus_monthly_product, plus_annual_product } = memberSetting || {};
745
- if (!cart?.lineItems) {
746
- return {
747
- hasPlusMember: false,
748
- hasMonthlyPlus: false,
749
- hasAnnualPlus: false
750
- };
751
- }
752
- const monthlyPlusItem = cart.lineItems.find(
753
- (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
754
- );
755
- const annualPlusItem = cart.lineItems.find(
756
- (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
757
- );
758
- const hasMonthlyPlus = !!monthlyPlusItem;
759
- const hasAnnualPlus = !!annualPlusItem;
760
- const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
761
- return {
762
- hasPlusMember,
763
- hasMonthlyPlus,
764
- hasAnnualPlus,
765
- monthlyPlusItem,
766
- annualPlusItem
767
- };
768
- }
769
- function useHasPlusMemberInCart({
770
- memberSetting,
771
- cart
772
- }) {
773
- return react.useMemo(
774
- () => hasPlusMemberInCart({
775
- memberSetting,
776
- cart
777
- }),
778
- [memberSetting, cart]
779
- );
780
- }
781
716
  var getReferralAttributes = () => {
782
717
  const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
783
718
  const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
@@ -794,158 +729,130 @@ var getReferralAttributes = () => {
794
729
  }
795
730
  return [];
796
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)
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
+ }
797
832
  var useCartAttributes = ({
798
833
  profile,
799
834
  customer,
800
835
  cart,
801
- memberSetting
836
+ memberType
802
837
  }) => {
803
838
  const [currentUrl, setCurrentUrl] = react.useState("");
804
- const { hasPlusMember } = useHasPlusMemberInCart({
805
- memberSetting,
806
- cart
807
- });
808
839
  react.useEffect(() => {
809
840
  setCurrentUrl(window.location.href);
810
841
  }, []);
811
- const userType = react.useMemo(() => {
812
- let userInfo = Cookies5__default.default.get("userInfo");
813
- if (userInfo) {
814
- userInfo = JSON.parse(userInfo);
815
- let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
816
- userInfo.setId = arr[arr.length - 1];
817
- }
818
- const customerInfo = userInfo || customer;
819
- if (!customerInfo) {
820
- return "new_user_unlogin";
821
- }
822
- if (customer) {
823
- const { orders = {} } = customer;
824
- if (orders?.edges?.length === 1) {
825
- return "old_user_orders_once";
826
- } else if (orders?.edges?.length > 1) {
827
- return "old_user_orders_twice";
828
- }
829
- }
830
- return "new_user_login";
831
- }, [customer]);
832
- const memberAttributes = react.useMemo(() => {
833
- const attributes = [
834
- {
835
- key: "_token",
836
- value: profile?.token
837
- //是否登录
838
- },
839
- {
840
- key: "_member_type",
841
- value: hasPlusMember ? "2" : profile?.memberType
842
- //:0(游客),1(普通会员),2(付费会员)
843
- },
844
- {
845
- key: "_user_type",
846
- value: userType
847
- // n
848
- },
849
- {
850
- key: "_is_login",
851
- value: profile?.token ? "true" : "false"
852
- }
853
- ];
854
- if (profile?.token) {
855
- attributes.push({
856
- key: "_login_user",
857
- value: "1"
858
- });
859
- }
860
- return attributes;
861
- }, [profile?.memberType, profile?.token, userType, hasPlusMember]);
862
- const functionAttributes = react.useMemo(() => {
863
- const hasFunctionEnvAttribute = cart?.lineItems.some(
864
- (item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
865
- );
866
- const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
867
- return hasFunctionEnvAttribute ? [
868
- {
869
- key: "_discounts_function_env",
870
- value: JSON.stringify({
871
- discount_code: discountCodes,
872
- user_tags: customer?.tags || []
873
- })
874
- }
875
- ] : [];
876
- }, [cart, customer]);
877
- const presellAttributes = react.useMemo(() => {
878
- return [
879
- {
880
- key: "_presale",
881
- value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
882
- }
883
- ];
884
- }, [cart]);
885
- const weightAttributes = react.useMemo(() => {
886
- return [
887
- {
888
- key: "_weight",
889
- value: cart?.lineItems.reduce((acc, item) => {
890
- return new Decimal2__default.default(acc).plus(item.variant.weight ?? 0).toNumber();
891
- }, 0).toString()
892
- },
893
- {
894
- key: "_app_source_name",
895
- value: "dtc"
896
- }
897
- ];
898
- }, [cart]);
899
- const trackingAttributes = react.useMemo(() => {
900
- return [
901
- {
902
- key: "utm_params",
903
- value: currentUrl
904
- }
905
- ];
906
- }, [currentUrl]);
907
- const commonAttributes = react.useMemo(
908
- () => [
909
- ...memberAttributes,
910
- ...functionAttributes,
911
- ...presellAttributes,
912
- ...weightAttributes,
913
- ...trackingAttributes,
914
- ...getReferralAttributes()
915
- ].filter((item) => item?.value),
916
- [memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
917
- );
918
- const extraAttributesInCart = react.useMemo(() => {
919
- const commonAttributeKeys = [
920
- // member attributes
921
- "_token",
922
- "_member_type",
923
- "_user_type",
924
- "_is_login",
925
- "_login_user",
926
- // function attributes
927
- "_discounts_function_env",
928
- // presell attributes
929
- "_presale",
930
- // weight attributes
931
- "_weight",
932
- "_app_source_name",
933
- // tracking attributes
934
- "utm_params",
935
- // referral attributes
936
- "_invite_code",
937
- "_play_mode_id",
938
- "_popup"
939
- ];
940
- return cart?.customAttributes?.filter((item) => !commonAttributeKeys.includes(item.key)) || [];
941
- }, [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]);
942
851
  return react.useMemo(
943
852
  () => ({
944
- attributes: [...commonAttributes, ...extraAttributesInCart].filter(
945
- (item) => item?.value
946
- )
853
+ attributes
947
854
  }),
948
- [commonAttributes, extraAttributesInCart]
855
+ [attributes]
949
856
  );
950
857
  };
951
858
  var useUpdateLineCodeAmountAttributes = ({
@@ -1091,6 +998,73 @@ var createInitialValue = () => ({
1091
998
  profile: void 0
1092
999
  });
1093
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
+ }
1094
1068
  var CartContext = react.createContext(null);
1095
1069
  function CartProvider({
1096
1070
  children,
@@ -1138,7 +1112,17 @@ function CartProvider({
1138
1112
  metafieldIdentifiers,
1139
1113
  disabled: isCartLoading
1140
1114
  });
1141
- 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
+ });
1142
1126
  ahooks.useRequest(
1143
1127
  () => {
1144
1128
  const newAttributes = [...attributes];
@@ -1201,7 +1185,8 @@ function CartProvider({
1201
1185
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
1202
1186
  campaign: gradientGiftsConfig || null,
1203
1187
  _giveaway: CUSTOMER_SCRIPT_GIFT_KEY,
1204
- cart
1188
+ cart,
1189
+ profile
1205
1190
  });
1206
1191
  const formattedScriptGifts = react.useMemo(() => {
1207
1192
  return formatScriptAutoFreeGift({
@@ -1268,6 +1253,8 @@ function CartProvider({
1268
1253
  removeCustomAttributes,
1269
1254
  setCustomAttributes,
1270
1255
  locale,
1256
+ profile,
1257
+ customer,
1271
1258
  isCodeChanging,
1272
1259
  setIsCodeChanging,
1273
1260
  autoFreeGiftConfig,
@@ -1283,7 +1270,8 @@ function CartProvider({
1283
1270
  scriptAutoFreeGiftResult,
1284
1271
  setScriptAutoFreeGift,
1285
1272
  giftNeedAddToCartLines,
1286
- metafieldIdentifiers
1273
+ metafieldIdentifiers,
1274
+ memberSetting
1287
1275
  }),
1288
1276
  [
1289
1277
  cart,
@@ -1307,7 +1295,10 @@ function CartProvider({
1307
1295
  scriptAutoFreeGiftResult,
1308
1296
  setScriptAutoFreeGift,
1309
1297
  giftNeedAddToCartLines,
1310
- metafieldIdentifiers
1298
+ metafieldIdentifiers,
1299
+ customer,
1300
+ profile,
1301
+ memberSetting
1311
1302
  ]
1312
1303
  );
1313
1304
  return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });