cart checker for remove cart when slug changed

This commit is contained in:
hamid zarghami
2025-12-29 09:04:58 +03:30
parent d6c2f2630d
commit 98b834b982
5 changed files with 65 additions and 7 deletions
+30
View File
@@ -0,0 +1,30 @@
"use client"
import { useCart } from '@/app/[name]/(Dialogs)/cart/hook/useCart'
import { useGetProfile } from '@/app/[name]/(Profile)/profile/hooks/userProfileData'
import { useParams } from 'next/navigation'
import { useEffect, useRef, type FC } from 'react'
const CartChecker: FC = () => {
const { isSuccess } = useGetProfile()
const params = useParams()
const currentSlug = params.name as string
const { slug: cartSlug, clearCart } = useCart()
const hasClearedRef = useRef(false)
useEffect(() => {
if (!isSuccess && currentSlug && cartSlug && currentSlug !== cartSlug) {
if (!hasClearedRef.current) {
hasClearedRef.current = true
clearCart()
}
} else if (currentSlug === cartSlug) {
hasClearedRef.current = false
}
}, [isSuccess, currentSlug, cartSlug, clearCart])
return null
}
export default CartChecker
+2 -1
View File
@@ -43,7 +43,7 @@ function applyPrimaryColor(colorHex: string) {
function PreferenceWrapper({ children }: Props) {
const { state: nightMode } = usePreference('night-mode', false);
const { data: aboutData, isSuccess } = useGetAbout();
const { data: aboutData } = useGetAbout();
// بررسی اینکه آیا باید اسپلش نمایش داده بشه
const [showSplash, setShowSplash] = useState(() => {
@@ -90,6 +90,7 @@ function PreferenceWrapper({ children }: Props) {
applyPrimaryColor(aboutData.data.menuColor);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [aboutData?.data?.menuColor]);
// اعمال تم تاریک/روشن