fix: shoud not be empty variandId

This commit is contained in:
hamid zarghami
2025-10-28 09:29:09 +03:30
parent e56e73bf7b
commit cc23cf86e6
5 changed files with 73 additions and 31 deletions
+43 -3
View File
@@ -1,7 +1,8 @@
'use client' 'use client'
import { FC, useState, useEffect } from 'react' import { FC, useState, useEffect, useRef } from 'react'
import { Swiper, SwiperSlide } from 'swiper/react'; import { Swiper, SwiperSlide, SwiperRef } from 'swiper/react';
import { Pagination } from 'swiper/modules'; import { Pagination } from 'swiper/modules';
import { ArrowLeft2, ArrowRight2 } from 'iconsax-react';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';
import { useGetLanding } from '../hooks/useHomeData'; import { useGetLanding } from '../hooks/useHomeData';
@@ -9,6 +10,9 @@ import { useGetLanding } from '../hooks/useHomeData';
const Carousel: FC = () => { const Carousel: FC = () => {
const { data } = useGetLanding() const { data } = useGetLanding()
const [isMobile, setIsMobile] = useState(false) const [isMobile, setIsMobile] = useState(false)
const swiperRef = useRef<SwiperRef>(null)
const [isBeginning, setIsBeginning] = useState(true)
const [isEnd, setIsEnd] = useState(false)
useEffect(() => { useEffect(() => {
const checkIsMobile = () => { const checkIsMobile = () => {
@@ -30,7 +34,21 @@ const Carousel: FC = () => {
return ( return (
<div className='relative' style={{ zIndex: 0 }}> <div className='relative' style={{ zIndex: 0 }}>
<Swiper pagination={true} modules={[Pagination]} className="mySwiper pb-5" style={{ zIndex: 0, position: 'relative' }}> <Swiper
ref={swiperRef}
pagination={true}
modules={[Pagination]}
className="mySwiper pb-5"
style={{ zIndex: 0, position: 'relative' }}
onSlideChange={(swiper) => {
setIsBeginning(swiper.isBeginning)
setIsEnd(swiper.isEnd)
}}
onSwiper={(swiper) => {
setIsBeginning(swiper.isBeginning)
setIsEnd(swiper.isEnd)
}}
>
{sliders.map((slider) => ( {sliders.map((slider) => (
<SwiperSlide key={slider._id}> <SwiperSlide key={slider._id}>
{slider.linkUrl ? ( {slider.linkUrl ? (
@@ -55,6 +73,28 @@ const Carousel: FC = () => {
</SwiperSlide> </SwiperSlide>
))} ))}
</Swiper> </Swiper>
{/* Navigation buttons */}
<button
onClick={() => swiperRef.current?.swiper?.slidePrev()}
disabled={isBeginning}
className={`absolute left-2 top-1/2 -translate-y-1/2 z-10 rounded-full p-2 shadow-md transition-all duration-200 ${isBeginning
? 'bg-gray-100 cursor-not-allowed opacity-50'
: 'bg-white/80 hover:bg-white cursor-pointer'
}`}
>
<ArrowLeft2 size="20" color={isBeginning ? "#9CA3AF" : "#374151"} />
</button>
<button
onClick={() => swiperRef.current?.swiper?.slideNext()}
disabled={isEnd}
className={`absolute right-2 top-1/2 -translate-y-1/2 z-10 rounded-full p-2 shadow-md transition-all duration-200 ${isEnd
? 'bg-gray-100 cursor-not-allowed opacity-50'
: 'bg-white/80 hover:bg-white cursor-pointer'
}`}
>
<ArrowRight2 size="20" color={isEnd ? "#9CA3AF" : "#374151"} />
</button>
</div> </div>
) )
} }
+10 -21
View File
@@ -1,7 +1,7 @@
'use client' 'use client'
import Layout from '@/hoc/Layout' import Layout from '@/hoc/Layout'
import { NextPage } from 'next' import { NextPage } from 'next'
import React, { useState, useMemo } from 'react' import React, { useState } from 'react'
import Menu from '../components/Menu' import Menu from '../components/Menu'
import { useGetOrders, useGetReturnOrder } from './hooks/useOrdersData' import { useGetOrders, useGetReturnOrder } from './hooks/useOrdersData'
import { Order, ReturnOrder } from './types/Types' import { Order, ReturnOrder } from './types/Types'
@@ -16,25 +16,6 @@ const Orders: NextPage = () => {
const { data: ordersData } = useGetOrders(activeTab); const { data: ordersData } = useGetOrders(activeTab);
const { data: returnOrderData } = useGetReturnOrder(); const { data: returnOrderData } = useGetReturnOrder();
// محاسبه تعداد سفارشات در هر تب از داده‌های اصلی
const tabCounts = useMemo(() => {
if (!ordersData?.results?.orders) return { current: 0, delivered: 0, returned: 0, cancelled: 0 };
return ordersData.results.orders.reduce((acc, order: Order) => {
const status = (order.status || 'pending').toLowerCase();
if (['pending', 'processing', 'shipped'].includes(status)) {
acc.current++;
} else if (status === 'delivered') {
acc.delivered++;
} else if (status === 'returned') {
acc.returned++;
} else if (status === 'cancelled') {
acc.cancelled++;
}
return acc;
}, { current: 0, delivered: 0, returned: 0, cancelled: 0 });
}, [ordersData]);
// تابع تبدیل وضعیت به فارسی // تابع تبدیل وضعیت به فارسی
const getStatusText = (status: string) => { const getStatusText = (status: string) => {
const statusMap: { [key: string]: string } = { const statusMap: { [key: string]: string } = {
@@ -48,6 +29,9 @@ const Orders: NextPage = () => {
return statusMap[status.toLowerCase()] || status; return statusMap[status.toLowerCase()] || status;
}; };
console.log('ordersData', ordersData);
return ( return (
<div className='p-3 sm:p-4 md:p-6'> <div className='p-3 sm:p-4 md:p-6'>
@@ -57,7 +41,12 @@ const Orders: NextPage = () => {
<OrderTabs <OrderTabs
activeTab={activeTab} activeTab={activeTab}
setActiveTab={setActiveTab} setActiveTab={setActiveTab}
tabCounts={tabCounts} tabCounts={{
cancelled: ordersData?.results?.userOrderCount.cancelled || 0,
current: ordersData?.results?.userOrderCount.processing || 0,
delivered: ordersData?.results?.userOrderCount.delivered || 0,
returned: ordersData?.results?.userOrderCount.returned || 0,
}}
/> />
{/* Orders List */} {/* Orders List */}
+8 -3
View File
@@ -289,15 +289,20 @@ export interface OrderDetailResponse {
}; };
} }
export interface UserOrderCount {
processing: number;
delivered: number;
cancelled: number;
returned: number;
}
export interface OrdersResponse { export interface OrdersResponse {
status: number; status: number;
success: boolean; success: boolean;
results: { results: {
mappedOrders: MappedOrder[]; mappedOrders: MappedOrder[];
orders: Order[]; orders: Order[];
shipmentAddress: ShipmentAddress; userOrderCount: UserOrderCount;
orderStatus: string;
createdAt: string;
}; };
} }
+11 -3
View File
@@ -2,16 +2,24 @@
import { useProductStore } from "@/app/product/store/Store"; import { useProductStore } from "@/app/product/store/Store";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import { useEffect } from "react"; import { useEffect, useRef } from "react";
const ManageData = () => { const ManageData = () => {
const { setVariantId } = useProductStore() const { setVariantId } = useProductStore()
const pathname = usePathname() const pathname = usePathname()
const prevPathnameRef = useRef(pathname)
useEffect(() => { useEffect(() => {
setVariantId('') const prevPath = prevPathnameRef.current
}, [pathname]); const isLeavingProduct = prevPath?.startsWith('/product/') && !pathname.startsWith('/product/')
if (isLeavingProduct) {
setVariantId('')
}
prevPathnameRef.current = pathname
}, [pathname, setVariantId]);
return null return null
} }
+1 -1
View File
@@ -1,6 +1,6 @@
export const TOKEN_NAME = "sh_token"; export const TOKEN_NAME = "sh_token";
export const REFRESH_TOKEN_NAME = "sh_refresh_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.fajrtabloshop.com";
// export const BASE_URL = "http://192.168.1.111:4000"; // export const BASE_URL = "http://192.168.1.111:4000";
export const PRIMARY_COLOR = "#DA2129"; export const PRIMARY_COLOR = "#DA2129";