@anker-in/shopify-react 0.1.1-beta.43 → 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,73 +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 hasPlusMemberInCart({
732
- memberSetting,
733
- cart
734
- }) {
735
- const { plus_monthly_product, plus_annual_product } = memberSetting || {};
736
- if (!cart?.lineItems) {
737
- return {
738
- hasPlusMember: false,
739
- hasMonthlyPlus: false,
740
- hasAnnualPlus: false
741
- };
742
- }
743
- const monthlyPlusItem = cart.lineItems.find(
744
- (item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
745
- );
746
- const annualPlusItem = cart.lineItems.find(
747
- (item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
748
- );
749
- const hasMonthlyPlus = !!monthlyPlusItem;
750
- const hasAnnualPlus = !!annualPlusItem;
751
- const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
752
- return {
753
- hasPlusMember,
754
- hasMonthlyPlus,
755
- hasAnnualPlus,
756
- monthlyPlusItem,
757
- annualPlusItem
758
- };
759
- }
760
- function useHasPlusMemberInCart({
761
- memberSetting,
762
- cart
763
- }) {
764
- return useMemo(
765
- () => hasPlusMemberInCart({
766
- memberSetting,
767
- cart
768
- }),
769
- [memberSetting, cart]
770
- );
771
- }
772
707
  var getReferralAttributes = () => {
773
708
  const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
774
709
  const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
@@ -785,158 +720,130 @@ var getReferralAttributes = () => {
785
720
  }
786
721
  return [];
787
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
+ }
788
823
  var useCartAttributes = ({
789
824
  profile,
790
825
  customer,
791
826
  cart,
792
- memberSetting
827
+ memberType
793
828
  }) => {
794
829
  const [currentUrl, setCurrentUrl] = useState("");
795
- const { hasPlusMember } = useHasPlusMemberInCart({
796
- memberSetting,
797
- cart
798
- });
799
830
  useEffect(() => {
800
831
  setCurrentUrl(window.location.href);
801
832
  }, []);
802
- const userType = useMemo(() => {
803
- let userInfo = Cookies5.get("userInfo");
804
- if (userInfo) {
805
- userInfo = JSON.parse(userInfo);
806
- let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
807
- userInfo.setId = arr[arr.length - 1];
808
- }
809
- const customerInfo = userInfo || customer;
810
- if (!customerInfo) {
811
- return "new_user_unlogin";
812
- }
813
- if (customer) {
814
- const { orders = {} } = customer;
815
- if (orders?.edges?.length === 1) {
816
- return "old_user_orders_once";
817
- } else if (orders?.edges?.length > 1) {
818
- return "old_user_orders_twice";
819
- }
820
- }
821
- return "new_user_login";
822
- }, [customer]);
823
- const memberAttributes = useMemo(() => {
824
- const attributes = [
825
- {
826
- key: "_token",
827
- value: profile?.token
828
- //是否登录
829
- },
830
- {
831
- key: "_member_type",
832
- value: hasPlusMember ? "2" : profile?.memberType
833
- //:0(游客),1(普通会员),2(付费会员)
834
- },
835
- {
836
- key: "_user_type",
837
- value: userType
838
- // n
839
- },
840
- {
841
- key: "_is_login",
842
- value: profile?.token ? "true" : "false"
843
- }
844
- ];
845
- if (profile?.token) {
846
- attributes.push({
847
- key: "_login_user",
848
- value: "1"
849
- });
850
- }
851
- return attributes;
852
- }, [profile?.memberType, profile?.token, userType, hasPlusMember]);
853
- const functionAttributes = useMemo(() => {
854
- const hasFunctionEnvAttribute = cart?.lineItems.some(
855
- (item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
856
- );
857
- const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
858
- return hasFunctionEnvAttribute ? [
859
- {
860
- key: "_discounts_function_env",
861
- value: JSON.stringify({
862
- discount_code: discountCodes,
863
- user_tags: customer?.tags || []
864
- })
865
- }
866
- ] : [];
867
- }, [cart, customer]);
868
- const presellAttributes = useMemo(() => {
869
- return [
870
- {
871
- key: "_presale",
872
- value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
873
- }
874
- ];
875
- }, [cart]);
876
- const weightAttributes = useMemo(() => {
877
- return [
878
- {
879
- key: "_weight",
880
- value: cart?.lineItems.reduce((acc, item) => {
881
- return new Decimal2(acc).plus(item.variant.weight ?? 0).toNumber();
882
- }, 0).toString()
883
- },
884
- {
885
- key: "_app_source_name",
886
- value: "dtc"
887
- }
888
- ];
889
- }, [cart]);
890
- const trackingAttributes = useMemo(() => {
891
- return [
892
- {
893
- key: "utm_params",
894
- value: currentUrl
895
- }
896
- ];
897
- }, [currentUrl]);
898
- const commonAttributes = useMemo(
899
- () => [
900
- ...memberAttributes,
901
- ...functionAttributes,
902
- ...presellAttributes,
903
- ...weightAttributes,
904
- ...trackingAttributes,
905
- ...getReferralAttributes()
906
- ].filter((item) => item?.value),
907
- [memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
908
- );
909
- const extraAttributesInCart = useMemo(() => {
910
- const commonAttributeKeys = [
911
- // member attributes
912
- "_token",
913
- "_member_type",
914
- "_user_type",
915
- "_is_login",
916
- "_login_user",
917
- // function attributes
918
- "_discounts_function_env",
919
- // presell attributes
920
- "_presale",
921
- // weight attributes
922
- "_weight",
923
- "_app_source_name",
924
- // tracking attributes
925
- "utm_params",
926
- // referral attributes
927
- "_invite_code",
928
- "_play_mode_id",
929
- "_popup"
930
- ];
931
- return cart?.customAttributes?.filter((item) => !commonAttributeKeys.includes(item.key)) || [];
932
- }, [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]);
933
842
  return useMemo(
934
843
  () => ({
935
- attributes: [...commonAttributes, ...extraAttributesInCart].filter(
936
- (item) => item?.value
937
- )
844
+ attributes
938
845
  }),
939
- [commonAttributes, extraAttributesInCart]
846
+ [attributes]
940
847
  );
941
848
  };
942
849
  var useUpdateLineCodeAmountAttributes = ({
@@ -1082,6 +989,73 @@ var createInitialValue = () => ({
1082
989
  profile: void 0
1083
990
  });
1084
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
+ }
1085
1059
  var CartContext = createContext(null);
1086
1060
  function CartProvider({
1087
1061
  children,
@@ -1129,7 +1103,17 @@ function CartProvider({
1129
1103
  metafieldIdentifiers,
1130
1104
  disabled: isCartLoading
1131
1105
  });
1132
- 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
+ });
1133
1117
  useRequest(
1134
1118
  () => {
1135
1119
  const newAttributes = [...attributes];
@@ -1192,7 +1176,8 @@ function CartProvider({
1192
1176
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
1193
1177
  campaign: gradientGiftsConfig || null,
1194
1178
  _giveaway: CUSTOMER_SCRIPT_GIFT_KEY,
1195
- cart
1179
+ cart,
1180
+ profile
1196
1181
  });
1197
1182
  const formattedScriptGifts = useMemo(() => {
1198
1183
  return formatScriptAutoFreeGift({
@@ -1259,6 +1244,8 @@ function CartProvider({
1259
1244
  removeCustomAttributes,
1260
1245
  setCustomAttributes,
1261
1246
  locale,
1247
+ profile,
1248
+ customer,
1262
1249
  isCodeChanging,
1263
1250
  setIsCodeChanging,
1264
1251
  autoFreeGiftConfig,
@@ -1274,7 +1261,8 @@ function CartProvider({
1274
1261
  scriptAutoFreeGiftResult,
1275
1262
  setScriptAutoFreeGift,
1276
1263
  giftNeedAddToCartLines,
1277
- metafieldIdentifiers
1264
+ metafieldIdentifiers,
1265
+ memberSetting
1278
1266
  }),
1279
1267
  [
1280
1268
  cart,
@@ -1298,7 +1286,10 @@ function CartProvider({
1298
1286
  scriptAutoFreeGiftResult,
1299
1287
  setScriptAutoFreeGift,
1300
1288
  giftNeedAddToCartLines,
1301
- metafieldIdentifiers
1289
+ metafieldIdentifiers,
1290
+ customer,
1291
+ profile,
1292
+ memberSetting
1302
1293
  ]
1303
1294
  );
1304
1295
  return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });