diff --git a/src/app/compare/page.tsx b/src/app/compare/page.tsx index 448493c..a29bb48 100644 --- a/src/app/compare/page.tsx +++ b/src/app/compare/page.tsx @@ -6,17 +6,20 @@ import { NextPage } from 'next' import Image from 'next/image' import { useSearchParams } from 'next/navigation' import { useEffect, useState } from 'react'; -import { useGetCompare } from './hooks/useCompareData' +import { useGetCompare, useSearchCompareProduct } from './hooks/useCompareData' import { useLocalCart } from '@/app/product/hooks/useLocalCart' import { toast } from '@/components/Toast' import { CompareProduct } from './types/Types' import { useSharedStore } from '@/share/store/sharedStore' +import DefaulModal from '@/components/DefaulModal' const ComparePage: NextPage = () => { const [productIds, setProductIds] = useState([]); + const [isCompareModalOpen, setIsCompareModalOpen] = useState(false); const search = useSearchParams() const { data, isLoading } = useGetCompare(productIds) + const { data: searchData } = useSearchCompareProduct(productIds[0]) const { addToLocalCart } = useLocalCart() const { isLogin } = useSharedStore() @@ -34,10 +37,58 @@ const ComparePage: NextPage = () => { toast('محصول به سبد خرید اضافه شد', 'success') } + const handleProductClick = (productId: number) => { + const productIdStr = productId.toString() + + // بررسی اینکه محصول قبلاً اضافه نشده باشد + if (productIds.includes(productIdStr)) { + toast('این محصول قبلاً در لیست مقایسه اضافه شده است', 'error') + return + } + + // اضافه کردن محصول به لیست مقایسه + const newProductIds = [...productIds, productIdStr] + console.log('New product IDs:', newProductIds) + setProductIds(newProductIds) + + // به‌روزرسانی URL با محصولات جدید + const url = new URL(window.location.href) + url.searchParams.set('productIds', newProductIds.join(',')) + url.searchParams.delete('productId') // حذف productId قدیمی + window.history.pushState({}, '', url.toString()) + + setIsCompareModalOpen(false) + toast('محصول به لیست مقایسه اضافه شد', 'success') + } + useEffect(() => { const productId = search.get('productId') - if (productId) { - setProductIds([productId]); + const productIdsParam = search.get('productIds') + + console.log('URL params - productId:', productId, 'productIds:', productIdsParam) + + if (productIdsParam) { + // اگر productIds در URL موجود است، آن را استفاده کن + const ids = productIdsParam.split(',').filter(id => id.trim()) + console.log('Parsed product IDs:', ids) + setProductIds(ids) + + // حذف productId قدیمی از URL + if (productId) { + const url = new URL(window.location.href) + url.searchParams.delete('productId') + window.history.replaceState({}, '', url.toString()) + } + } else if (productId) { + // اگر فقط productId موجود است، آن را به productIds تبدیل کن + console.log('Single product ID:', productId) + setProductIds([productId]) + + // URL را به productIds تبدیل کن + const url = new URL(window.location.href) + url.searchParams.delete('productId') + url.searchParams.set('productIds', productId) + window.history.replaceState({}, '', url.toString()) } }, [search]); @@ -74,7 +125,7 @@ const ComparePage: NextPage = () => { return (
-
+
{products.map((product, index) => (
{
))} + +
@@ -127,6 +182,102 @@ const ComparePage: NextPage = () => {
))}
+ + {/* مودال انتخاب کالا برای مقایسه */} + setIsCompareModalOpen(false)} + isHeader={true} + title_header="انتخاب کالا برای مقایسه" + width={800} + > +
+ {/* نوار جستجو */} +
+
+ 759 كالا +
+
+ +
+ + + + +
+
+
+ + {/* عنوان برترین کالاها */} +
+ برترین کالاها برای مقایسه +
+ + {/* لیست محصولات */} +
+ {searchData?.results?.products?.slice(0, 6).map((product: CompareProduct) => ( +
handleProductClick(product._id)} + > +
+ {/* تصویر محصول */} +
+ {product.title_fa} +
+ + {/* متن ذخیره سازی */} +
+ ذخیره سازی +
+ + {/* نام محصول */} +

+ {product.title_fa} +

+ + {/* ستاره‌ها */} +
+ {[...Array(5)].map((_, i) => ( + + + + ))} +
+ + {/* قیمت */} +
+ {product.default_variant.price.selling_price.toLocaleString()} تومان +
+ + {/* آیکون قلب */} + +
+
+ ))} +
+
+
) } diff --git a/src/app/compare/service/Service.ts b/src/app/compare/service/Service.ts index a8db42e..bdff035 100644 --- a/src/app/compare/service/Service.ts +++ b/src/app/compare/service/Service.ts @@ -5,7 +5,18 @@ import { CompareResponse } from "../types/Types"; export const getCompare = async ( productIds: string[] ): Promise => { - const { data } = await axios.get(`/product/compare?productIds=${productIds}`); + console.log("API call - productIds:", productIds); + + // روش اول: ارسال آرایه به صورت جداگانه + const params = new URLSearchParams(); + productIds.forEach((id) => { + params.append("productIds", id); + }); + + console.log("Final URL:", `/product/compare?${params.toString()}`); + + const { data } = await axios.get(`/product/compare?${params.toString()}`); + console.log("API response:", data); return data; }; diff --git a/src/app/product/hooks/useProductData.ts b/src/app/product/hooks/useProductData.ts index 1357f56..6b0a2c5 100644 --- a/src/app/product/hooks/useProductData.ts +++ b/src/app/product/hooks/useProductData.ts @@ -1,5 +1,6 @@ import { useMutation, useQuery } from "@tanstack/react-query"; import * as api from "../service/Service"; +import { useSharedStore } from "@/share/store/sharedStore"; export const useGetDetailProduct = (id: string) => { return useQuery({ @@ -26,10 +27,11 @@ export const useAddQuestion = () => { }; export const useCheckWishProduct = (productId: string, variantId: string) => { + const { isLogin } = useSharedStore(); return useQuery({ queryKey: ["check-wish-product", productId, variantId], queryFn: () => api.checkWishProduct(productId, variantId), - enabled: !!productId && !!variantId, + enabled: !!productId && !!variantId && isLogin, }); }; diff --git a/src/config/axios.ts b/src/config/axios.ts index 90d4ec9..f55080b 100644 --- a/src/config/axios.ts +++ b/src/config/axios.ts @@ -73,7 +73,7 @@ axiosInstance.interceptors.response.use( console.error("Token refresh failed:", refreshError); await removeToken(); await removeRefreshToken(); - window.location.href = `/`; + // window.location.href = `/`; return Promise.reject(refreshError); } finally { window.isRefreshingToken = false; diff --git a/src/config/const.ts b/src/config/const.ts index e034054..e2e602b 100644 --- a/src/config/const.ts +++ b/src/config/const.ts @@ -1,6 +1,6 @@ export const TOKEN_NAME = "sh_token"; export const REFRESH_TOKEN_NAME = "sh_refresh_token"; -export const BASE_URL = "https://shop-api.dev.danakcorp.com"; -// export const BASE_URL = "https://api.shinan.ir"; +// export const BASE_URL = "https://shop-api.dev.danakcorp.com"; +export const BASE_URL = "http://192.168.1.111:4000"; export const PRIMARY_COLOR = "#015699";