|
@@ -1,10 +1,10 @@
|
|
|
"use client";
|
|
"use client";
|
|
|
|
|
|
|
|
-import {useState, useEffect} from "react";
|
|
|
|
|
|
|
+import {useState, useEffect, useRef, Suspense} from "react";
|
|
|
|
|
|
|
|
import { LoadingSpinner } from "@components/common/LoadingSpinner";
|
|
import { LoadingSpinner } from "@components/common/LoadingSpinner";
|
|
|
-// import { useForm, SubmitHandler } from "react-hook-form"
|
|
|
|
|
-import { useForm, FormProvider } from "react-hook-form"
|
|
|
|
|
|
|
+import CheckoutShippingMethodLoading from "./CheckoutShippingMethodLoading";
|
|
|
|
|
+import { useForm, FormProvider } from "react-hook-form";
|
|
|
|
|
|
|
|
import {
|
|
import {
|
|
|
Country,
|
|
Country,
|
|
@@ -18,13 +18,23 @@ import AddressResultDisplayLoading from "./AddressResultDisplayLoading";
|
|
|
import CheckoutAddressModal from "./CheckoutAddressModal";
|
|
import CheckoutAddressModal from "./CheckoutAddressModal";
|
|
|
import ShippingAddressCheckout from "./ShippingAddressCheckout";
|
|
import ShippingAddressCheckout from "./ShippingAddressCheckout";
|
|
|
import BillingAddressCheckout from "./BillingAddressCheckout";
|
|
import BillingAddressCheckout from "./BillingAddressCheckout";
|
|
|
-// import CheckoutShippingMethod from "./CheckoutShippingMethod";
|
|
|
|
|
|
|
+import {ShippingMethodCheckout,RefShippingMethodsHandle} from "./ShippingMethodCheckout";
|
|
|
import { useCustomToast } from "@/utils/hooks/useToast";
|
|
import { useCustomToast } from "@/utils/hooks/useToast";
|
|
|
import {useCheckoutAddress} from "@/utils/hooks/useCheckoutAddress"
|
|
import {useCheckoutAddress} from "@/utils/hooks/useCheckoutAddress"
|
|
|
|
|
+import {useCheckoutPaymentMethod} from "@/utils/hooks/useCheckoutPaymentMethod";
|
|
|
|
|
+import { useAppSelector,useAppDispatch } from "@/store/hooks";
|
|
|
|
|
+import { updateCart } from "@/store/slices/cart-slice";
|
|
|
|
|
+import { useCartDetail } from "@utils/hooks/useCartDetail";
|
|
|
|
|
+import PaymentMethodCheckout from "./PaymentMethodCheckout";
|
|
|
|
|
+import LoadingPaymentMethod from "./LoadingPaymentMethod";
|
|
|
|
|
+import CheckoutPlaceOrder from "./CheckoutPlaceOrder";
|
|
|
|
|
+
|
|
|
/***
|
|
/***
|
|
|
* 不用useForShipping字段了
|
|
* 不用useForShipping字段了
|
|
|
* 以shipping address 为准,根据shippingaddress 设置billing address
|
|
* 以shipping address 为准,根据shippingaddress 设置billing address
|
|
|
- * 保存完地址之后要重新获取运输方式和支付方式
|
|
|
|
|
|
|
+ * @todo 保存完地址之后要重新获取运输方式和支付方式
|
|
|
|
|
+ * @todo 变更运输方式后重新获取支付方式
|
|
|
|
|
+ * @todo redux本地存储购物车详情优化(slice定义修改;useCartDetail自定义hook修改;最后结账也统一优化购物车详情获取)
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -81,29 +91,7 @@ function generateSaveCheckoutAddressParam(formData: FullAddressFormData):CreateC
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
-/**
|
|
|
|
|
- * 对比shipping address与billing address的数据是否完全相同
|
|
|
|
|
- */
|
|
|
|
|
-/*function addressIsSame(billAddress: BillAddressFormData | null, shipAddress: ShipAddressFormData | null) {
|
|
|
|
|
- if(billAddress === null && shipAddress === null) {
|
|
|
|
|
- return true;
|
|
|
|
|
- } else if(billAddress !== null && shipAddress !== null) {
|
|
|
|
|
- return (
|
|
|
|
|
- billAddress.billingEmail === shipAddress.shippingEmail &&
|
|
|
|
|
- billAddress.billingFirstName === shipAddress.shippingFirstName &&
|
|
|
|
|
- billAddress.billingLastName === shipAddress.shippingLastName &&
|
|
|
|
|
- billAddress.billingCompanyName === shipAddress.shippingCompanyName &&
|
|
|
|
|
- billAddress.billingAddress === shipAddress.shippingAddress &&
|
|
|
|
|
- billAddress.billingCity === shipAddress.shippingCity &&
|
|
|
|
|
- billAddress.billingState === shipAddress.shippingState &&
|
|
|
|
|
- billAddress.billingCountry === shipAddress.shippingCountry &&
|
|
|
|
|
- billAddress.billingPostcode === shipAddress.shippingPostcode &&
|
|
|
|
|
- billAddress.billingPhoneNumber === shipAddress.shippingPhoneNumber
|
|
|
|
|
- );
|
|
|
|
|
- } else {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-}*/
|
|
|
|
|
|
|
+
|
|
|
export default function CheckoutWrapper({
|
|
export default function CheckoutWrapper({
|
|
|
loginEmail,
|
|
loginEmail,
|
|
|
countries
|
|
countries
|
|
@@ -111,9 +99,22 @@ export default function CheckoutWrapper({
|
|
|
loginEmail: string;
|
|
loginEmail: string;
|
|
|
countries: Country[]
|
|
countries: Country[]
|
|
|
}) {
|
|
}) {
|
|
|
|
|
+ const dispatch = useAppDispatch();
|
|
|
|
|
+ const { getCartDetail } = useCartDetail();
|
|
|
|
|
+
|
|
|
|
|
+ const {
|
|
|
|
|
+ data: paymentMethodDatas,
|
|
|
|
|
+ loading: paymentMethodLoading,
|
|
|
|
|
+ error: paymentMethodError,
|
|
|
|
|
+ getPaymentMethod,
|
|
|
|
|
+ savePaymentMethod
|
|
|
|
|
+ } = useCheckoutPaymentMethod();
|
|
|
|
|
+
|
|
|
const { showToast } = useCustomToast();
|
|
const { showToast } = useCustomToast();
|
|
|
const { getCheckoutAddress, saveCheckoutAddress } = useCheckoutAddress(loginEmail);
|
|
const { getCheckoutAddress, saveCheckoutAddress } = useCheckoutAddress(loginEmail);
|
|
|
|
|
|
|
|
|
|
+ const methodShiippingRef = useRef<RefShippingMethodsHandle>(null);
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// useForm() 只会在组件初始化时读取一次 defaultValues
|
|
// useForm() 只会在组件初始化时读取一次 defaultValues
|
|
|
const addressForm = useForm<FullAddressFormData>({
|
|
const addressForm = useForm<FullAddressFormData>({
|
|
@@ -124,7 +125,8 @@ export default function CheckoutWrapper({
|
|
|
const [addressModalOpen, setAddressModalOpen] = useState(false);
|
|
const [addressModalOpen, setAddressModalOpen] = useState(false);
|
|
|
const [loadingData, setLoadingData] = useState(true);
|
|
const [loadingData, setLoadingData] = useState(true);
|
|
|
const [serverShippingAddress, setServerShippingAddress] = useState<ShipAddressFormData | null>(null);
|
|
const [serverShippingAddress, setServerShippingAddress] = useState<ShipAddressFormData | null>(null);
|
|
|
- const [serverBillingAddress, setServerBillingAddress] = useState<BillAddressFormData | null>(null);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const serverBillingAddress = useRef<BillAddressFormData | null>(null);
|
|
|
|
|
|
|
|
const [addressSaving, setAddressSaving] = useState(false);
|
|
const [addressSaving, setAddressSaving] = useState(false);
|
|
|
|
|
|
|
@@ -138,9 +140,8 @@ export default function CheckoutWrapper({
|
|
|
setServerShippingAddress({
|
|
setServerShippingAddress({
|
|
|
...shippingAddress
|
|
...shippingAddress
|
|
|
});
|
|
});
|
|
|
- setServerBillingAddress({
|
|
|
|
|
- ...billingAddress
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ serverBillingAddress.current = billingAddress;
|
|
|
|
|
+
|
|
|
setLoadingData(false);
|
|
setLoadingData(false);
|
|
|
}, ()=> {
|
|
}, ()=> {
|
|
|
setLoadingData(false);
|
|
setLoadingData(false);
|
|
@@ -151,10 +152,10 @@ export default function CheckoutWrapper({
|
|
|
const openAddressModal = () => {
|
|
const openAddressModal = () => {
|
|
|
setAddressModalOpen(true);
|
|
setAddressModalOpen(true);
|
|
|
|
|
|
|
|
- if(serverShippingAddress && serverBillingAddress) {
|
|
|
|
|
|
|
+ if(serverShippingAddress && serverBillingAddress.current) {
|
|
|
addressForm.reset({
|
|
addressForm.reset({
|
|
|
...serverShippingAddress,
|
|
...serverShippingAddress,
|
|
|
- ...serverBillingAddress,
|
|
|
|
|
|
|
+ ...serverBillingAddress.current,
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -166,7 +167,8 @@ export default function CheckoutWrapper({
|
|
|
};
|
|
};
|
|
|
const addressFormOnSubmit = async (formData: FullAddressFormData) => {
|
|
const addressFormOnSubmit = async (formData: FullAddressFormData) => {
|
|
|
// console.log('addressFormOnSubmit ---- ',formData); return;
|
|
// console.log('addressFormOnSubmit ---- ',formData); return;
|
|
|
- // 保存完地址之后还要重新请求地址,把保存后的地址id同步到表单里;还需要获取运输方式
|
|
|
|
|
|
|
+ // 保存完地址之后还要重新请求地址,把保存后的地址id同步到表单里;
|
|
|
|
|
+ // 还需要获取运输方式
|
|
|
|
|
|
|
|
const saveAddressParam = generateSaveCheckoutAddressParam(formData);
|
|
const saveAddressParam = generateSaveCheckoutAddressParam(formData);
|
|
|
setAddressSaving(true);
|
|
setAddressSaving(true);
|
|
@@ -174,6 +176,10 @@ export default function CheckoutWrapper({
|
|
|
|
|
|
|
|
const res = await saveCheckoutAddress(saveAddressParam);
|
|
const res = await saveCheckoutAddress(saveAddressParam);
|
|
|
console.log('CREATE_CHECKOUT_ADDRESS res ====== ',res);
|
|
console.log('CREATE_CHECKOUT_ADDRESS res ====== ',res);
|
|
|
|
|
+ if(methodShiippingRef.current) {
|
|
|
|
|
+ methodShiippingRef.current.reloadSHippingMethods();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const queryAddressRes = await getCheckoutAddress();
|
|
const queryAddressRes = await getCheckoutAddress();
|
|
|
if(queryAddressRes !== null) {
|
|
if(queryAddressRes !== null) {
|
|
|
addressForm.resetField('shippingAddressId',{
|
|
addressForm.resetField('shippingAddressId',{
|
|
@@ -185,9 +191,8 @@ export default function CheckoutWrapper({
|
|
|
setServerShippingAddress({
|
|
setServerShippingAddress({
|
|
|
...queryAddressRes.shippingAddress
|
|
...queryAddressRes.shippingAddress
|
|
|
});
|
|
});
|
|
|
- setServerBillingAddress({
|
|
|
|
|
- ...queryAddressRes.billingAddress
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ serverBillingAddress.current = queryAddressRes.billingAddress;
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
setAddressModalOpen(false);
|
|
setAddressModalOpen(false);
|
|
|
} catch(err) {
|
|
} catch(err) {
|
|
@@ -200,6 +205,22 @@ export default function CheckoutWrapper({
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ const handlePaymentMethodChange = async (method: string,title: string) => {
|
|
|
|
|
+
|
|
|
|
|
+ const res = await savePaymentMethod({ paymentMethod: method });
|
|
|
|
|
+ console.log('savePaymentMethod res ====== ',res);
|
|
|
|
|
+ if(!res.error) {
|
|
|
|
|
+ // 更新paymentMethod paymentMethodTitle
|
|
|
|
|
+ dispatch(updateCart({
|
|
|
|
|
+ paymentMethod: method,
|
|
|
|
|
+ paymentMethodTitle: title
|
|
|
|
|
+ }));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ showToast(res.msg, 'danger');
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
// function tt() {
|
|
// function tt() {
|
|
|
// const arr = [
|
|
// const arr = [
|
|
|
// ['US','3803800217'],
|
|
// ['US','3803800217'],
|
|
@@ -250,7 +271,30 @@ export default function CheckoutWrapper({
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
<div className="mt-6 box-border px-4">
|
|
<div className="mt-6 box-border px-4">
|
|
|
- {/* <CheckoutShippingMethod /> */}
|
|
|
|
|
|
|
+ <h3 className="text-ly-24 font-medium">Shipping Method</h3>
|
|
|
|
|
+ {/* <Suspense fallback={<CheckoutShippingMethodLoading />}> */}
|
|
|
|
|
+ <ShippingMethodCheckout ref={methodShiippingRef} />
|
|
|
|
|
+ {/* </Suspense> */}
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="mt-6 box-border px-4">
|
|
|
|
|
+ <h3 className="text-ly-24 font-medium">Payment Method</h3>
|
|
|
|
|
+ { paymentMethodLoading ?
|
|
|
|
|
+ <LoadingPaymentMethod />
|
|
|
|
|
+ :
|
|
|
|
|
+ <PaymentMethodCheckout
|
|
|
|
|
+ errorMsg={paymentMethodError}
|
|
|
|
|
+ paymentMethods={paymentMethodDatas}
|
|
|
|
|
+ onPaymentMethodChange={handlePaymentMethodChange}
|
|
|
|
|
+ />
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div className="mt-3 box-border px-4">
|
|
|
|
|
+ <p className="text-ly-12 text-[#666666] leading-ly-20">By providing your information, you agree to Wiggins'sPrivacy Policyand Terms of Use.</p>
|
|
|
|
|
+ <div className="mt-6 w-full">
|
|
|
|
|
+ <CheckoutPlaceOrder />
|
|
|
|
|
+ </div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
</section>
|