|
|
@@ -9,7 +9,8 @@ import {
|
|
|
import { ProductAddToCart } from "@/app/(public)/product/_components/ProductAddToCart";
|
|
|
import { useCustomToast } from "@utils/hooks/useToast";
|
|
|
import {clientFetch} from "@/lib/restApiClient";
|
|
|
-
|
|
|
+import { useAddProduct } from "@utils/hooks/useAddToCart";
|
|
|
+import { redirect, RedirectType } from 'next/navigation'
|
|
|
/**
|
|
|
* 辅助函数:判断一个完整的选项值ID组合是否对应一个可购买的变体(quantity > 0)
|
|
|
* @param selectedIds {number[]} 选中的选项值ID的数组
|
|
|
@@ -138,7 +139,7 @@ export function ProductInformation({
|
|
|
flexibleVariants: ResolvedVariant[];
|
|
|
isSaleable: string | undefined;
|
|
|
}) {
|
|
|
-
|
|
|
+ const { isCartLoading, onAddToCart } = useAddProduct();
|
|
|
const { showToast } = useCustomToast();
|
|
|
|
|
|
// 记录当前哪个选项组刚刚被点击(用于实现“点击组内全部可点”)
|
|
|
@@ -344,24 +345,51 @@ export function ProductInformation({
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async function addProductToCart(action:string = 'addtocart') {
|
|
|
+
|
|
|
+ let params = {
|
|
|
+ productId: String(productId),
|
|
|
+ quantity: productQty,
|
|
|
+ variantId: currentVariantInfo.variant?._id,
|
|
|
+ };
|
|
|
+ let res = await onAddToCart(params);
|
|
|
+ console.log('onAddToCart result --- ', res);
|
|
|
+ if(action === 'buynow') {
|
|
|
+ if(res) {
|
|
|
+ const responseData = res.data?.createAddProductInCart?.addProductInCart;
|
|
|
+ if(responseData.success) {
|
|
|
+ redirect('/checkout?step=address', RedirectType.push);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
let addToCartHandler = async () => {
|
|
|
- // /api/test
|
|
|
- // let result = await clientFetch('/api/shop/categories/1',{
|
|
|
- // method: "GET",
|
|
|
+
|
|
|
+ // let result = await clientFetch('/api/shop/addProductInCart',{
|
|
|
+ // method: "POST",
|
|
|
+ // body: JSON.stringify({ productId: productId, quantity: productQty, variantId: currentVariantInfo.variant?._id }),
|
|
|
// });
|
|
|
- let result = await clientFetch('/api/shop/addProductInCart',{
|
|
|
- method: "POST",
|
|
|
- body: JSON.stringify({ productId: productId, quantity: productQty, variantId: currentVariantInfo.variant?.id }),
|
|
|
- });
|
|
|
- console.log(result);
|
|
|
- //if(!isCurrentSelectionAvailable) {
|
|
|
- // showToast("Please fill in all required fields", "warning");
|
|
|
- // return;
|
|
|
- //}
|
|
|
+ // console.log(result);
|
|
|
+ if(!isCurrentSelectionAvailable) {
|
|
|
+ showToast("The selected options are not available!", "warning");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(currentVariantInfo.variant && !isCartLoading) {
|
|
|
+ addProductToCart();
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- let buyNowHandler = () => {};
|
|
|
+ let buyNowHandler = () => {
|
|
|
+ if(!isCurrentSelectionAvailable) {
|
|
|
+ showToast("The selected options are not available!", "warning");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(currentVariantInfo.variant && !isCartLoading) {
|
|
|
+ addProductToCart('buynow');
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
|
|
|
return (<>
|
|
|
@@ -438,6 +466,7 @@ export function ProductInformation({
|
|
|
</div>
|
|
|
<ProductAddToCart
|
|
|
isAvailable={isCurrentSelectionAvailable}
|
|
|
+ isLoading={isCartLoading}
|
|
|
onAddToCart={addToCartHandler}
|
|
|
onBuyNow={buyNowHandler}
|
|
|
/>
|