|
|
@@ -1,24 +1,25 @@
|
|
|
"use client";
|
|
|
|
|
|
import {Ref, useState, useEffect, useCallback, useImperativeHandle, useMemo } from "react";
|
|
|
-
|
|
|
+import { useRouter } from 'next/navigation';
|
|
|
import { useForm, FormProvider } from "react-hook-form";
|
|
|
import { useCustomToast } from "@/utils/hooks/useToast";
|
|
|
import {useCheckoutAddress} from "@/utils/hooks/useCheckoutAddress"
|
|
|
-
|
|
|
+import {useLoginModal} from "@/providers/LoginModalProvider";
|
|
|
import { useGetCustomerAddress } from "@utils/hooks/useGetCustomerAddress";
|
|
|
+import { EMAIL_REGEX } from "@utils/constants";
|
|
|
import {
|
|
|
ShipAddressFormData,
|
|
|
FullAddressFormData,
|
|
|
CreateCheckoutAddressVariables,
|
|
|
} from "@/types/checkout/type";
|
|
|
-import { CustomerAddressItem } from "@/types/customer/type";
|
|
|
+import { CustomerAddressItem,CheckEmailRegisteredData } from "@/types/customer/type";
|
|
|
import { CartDetail,CartAddress } from "@/types/cart/type";
|
|
|
-
|
|
|
+import { formatCartDetail } from "@/utils/cartDetailTools";
|
|
|
+import {clientFetch} from "@/lib/restApiClient";
|
|
|
+import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
|
|
|
import { overlayLoading } from "@/components/theme/ui/kernel/loading/api";
|
|
|
-
|
|
|
import {normalizePhoneForForm} from "@/utils/phoneNumberTools";
|
|
|
-
|
|
|
import AddressResultDisplay from "./AddressResultDisplay";
|
|
|
import ShippingAddressCheckout from "./ShippingAddressCheckout";
|
|
|
import BillingAddressCheckout from "./BillingAddressCheckout";
|
|
|
@@ -210,13 +211,18 @@ export function CheckoutAddress({
|
|
|
loginEmail,
|
|
|
cartData,
|
|
|
onSaveAddress,
|
|
|
+ onCartChange
|
|
|
}: {
|
|
|
ref: Ref<RefCheckoutAddressHandle>;
|
|
|
loginEmail: string;
|
|
|
cartData: CartDetail;
|
|
|
onSaveAddress: (saveRes: {billingAddress: CartAddress; shippingAddress: CartAddress;}) => void;
|
|
|
+ onCartChange: (data: CartDetail,dispatch: boolean) => void;
|
|
|
}) {
|
|
|
|
|
|
+ const router = useRouter();
|
|
|
+ const {openLoginModal} = useLoginModal();
|
|
|
+
|
|
|
const addressFormDefaultValues = useMemo(() => {
|
|
|
return getAddressFormDataFromCart(cartData,loginEmail)
|
|
|
},[cartData,loginEmail]);
|
|
|
@@ -383,6 +389,53 @@ export function CheckoutAddress({
|
|
|
return () => window.cancelAnimationFrame(timer)
|
|
|
},[cartData.isGuest,addressFormDefaultValues]);
|
|
|
|
|
|
+ const verifyEmailIsRegistered = async (value: string) => {
|
|
|
+ if(!cartData.isGuest) return;
|
|
|
+ if(!EMAIL_REGEX.test(value)) return;
|
|
|
+ const param = new URLSearchParams({ email: value });
|
|
|
+
|
|
|
+ const res = await clientFetch<CheckEmailRegisteredData>(`/api/customer/check-email?${param.toString()}`,{
|
|
|
+ method: 'GET',
|
|
|
+ });
|
|
|
+
|
|
|
+ if(res.success && res.data.exists) { // 注册过
|
|
|
+ openLoginModal({
|
|
|
+ defaultEmail: value,
|
|
|
+ onFinalResult: async (res) => {
|
|
|
+ if(res.mergeCartSuccess) { // 购物车合并成功
|
|
|
+ if(res.cartAfterMerge){
|
|
|
+ const newCartDetail = formatCartDetail(res.cartAfterMerge);
|
|
|
+ onCartChange(newCartDetail,false);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else { // 购物车合并失败
|
|
|
+ // 三种 情况: 登录成功; 注册成功;
|
|
|
+ if(res.loginSuccess) {
|
|
|
+ // 登录成功 需要刷新页面
|
|
|
+ showToast('You have logged in successfully. But something wrong','danger');
|
|
|
+ await confirmDialog({
|
|
|
+ title: "Warning",
|
|
|
+ content: "You have logged in successfully. But something wrong. You must refresh the page.",
|
|
|
+ noCancel: true,
|
|
|
+ });
|
|
|
+ window.location.reload();
|
|
|
+ }
|
|
|
+ if(res.registerSuccess && !res.loginSuccess) {
|
|
|
+ // 注册成功,去登录
|
|
|
+ await confirmDialog({
|
|
|
+ title: "Warning",
|
|
|
+ content: "You have registered successfully. But something wrong. You have to login.",
|
|
|
+ noCancel: true,
|
|
|
+ });
|
|
|
+ router.replace('/customer/login');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
// function tt() {
|
|
|
// const arr = [
|
|
|
// ['US','3803800217'],
|
|
|
@@ -428,6 +481,7 @@ export function CheckoutAddress({
|
|
|
customerAddressList={myAddressList}
|
|
|
showFormField={showShipFormField}
|
|
|
onShowFormFieldChange={showShipFormFieldChange}
|
|
|
+ onEmailChange={verifyEmailIsRegistered}
|
|
|
/>
|
|
|
<BillingAddressCheckout
|
|
|
noAddress={noBillAddress}
|