added price filter, changed mobile filter ui
This commit is contained in:
@@ -2,31 +2,16 @@ import React from "react";
|
||||
import { Home, ShoppingBag, ShoppingCart, Heart, User } from "lucide-react";
|
||||
import { Link, useLocation } from "react-router-dom";
|
||||
import styles from "./FooterBar.module.scss";
|
||||
import { useGetCartQuery } from "../../app/api/cartApi";
|
||||
import { useCart } from "../../app/api/useCart";
|
||||
import { useGetFavoritesQuery } from "../../app/api/favoritesApi";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
const FooterBar = () => {
|
||||
const location = useLocation();
|
||||
const { t } = useTranslation();
|
||||
const { data: cartData } = useGetCartQuery();
|
||||
const { cartCount } = useCart();
|
||||
const { data: favoriteData } = useGetFavoritesQuery();
|
||||
|
||||
// FIX: Object içindeki tüm channel'ların item'larını birleştir
|
||||
const getCartCount = () => {
|
||||
if (!cartData?.data || typeof cartData.data !== 'object') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Object.values ile tüm channel array'lerini al
|
||||
const allCartItems = Object.values(cartData.data).flat();
|
||||
|
||||
return allCartItems.reduce((total, item) => {
|
||||
return total + (parseInt(item.product_quantity, 10) || 0);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const cartCount = getCartCount();
|
||||
const favoriteCount = favoriteData?.length || 0;
|
||||
|
||||
const navItems = [
|
||||
@@ -88,4 +73,4 @@ const FooterBar = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default FooterBar;
|
||||
export default FooterBar;
|
||||
|
||||
@@ -17,7 +17,7 @@ import { CiLocationOn } from "react-icons/ci";
|
||||
import Sidebar from "../CategorySideBar";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useSearchProductQuery } from "../../app/api/searchApi";
|
||||
import { useGetCartQuery } from "../../app/api/cartApi";
|
||||
import { useCart } from "../../app/api/useCart";
|
||||
import { useGetOrdersQuery } from "../../app/api/orderApi";
|
||||
import { useGetFavoritesQuery } from "../../app/api/favoritesApi";
|
||||
import { useAuth } from "../../context/authContext";
|
||||
@@ -31,27 +31,11 @@ const NavbarDown = () => {
|
||||
const { data: searchData, refetch } = useSearchProductQuery(searchQuery, {
|
||||
skip: !searchQuery,
|
||||
});
|
||||
const { data: cartData } = useGetCartQuery(undefined, {
|
||||
refetchOnMountOrArgChange: false,
|
||||
});
|
||||
|
||||
const { cartCount: cartItemCount } = useCart();
|
||||
|
||||
const { isAuthenticated, logout } = useAuth();
|
||||
|
||||
// FIX: Object içindeki tüm channel'ların item'larını birleştir
|
||||
const getCartItemCount = () => {
|
||||
if (!cartData?.data || typeof cartData.data !== 'object') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Object.values ile tüm channel array'lerini al ve flat ile birleştir
|
||||
const allCartItems = Object.values(cartData.data).flat();
|
||||
|
||||
return allCartItems.reduce((total, item) => {
|
||||
return total + (parseInt(item.product_quantity, 10) || 0);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const cartItemCount = getCartItemCount();
|
||||
|
||||
const { data: ordersData } = useGetOrdersQuery();
|
||||
const ordersItemCount = ordersData?.length || 0;
|
||||
|
||||
@@ -305,4 +289,4 @@ const NavbarDown = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default NavbarDown;
|
||||
export default NavbarDown;
|
||||
|
||||
@@ -32,6 +32,8 @@ const truncateDescription = (htmlString, maxLength = 80) => {
|
||||
return truncatedText;
|
||||
};
|
||||
|
||||
import { useCart } from "../../app/api/useCart";
|
||||
|
||||
const ProductCard = ({
|
||||
product,
|
||||
showAddToCart = true,
|
||||
@@ -49,64 +51,39 @@ const ProductCard = ({
|
||||
const { data: favoriteProducts = [] } = useGetFavoritesQuery();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [localIsFavorite, setLocalIsFavorite] = useState(
|
||||
favoriteProducts.some((fav) => fav.product?.id === product.id)
|
||||
favoriteProducts.some((fav) => fav.product?.id === product.id),
|
||||
);
|
||||
|
||||
const truncatedDesc = truncateDescription(
|
||||
product.description,
|
||||
descriptionMaxLength
|
||||
descriptionMaxLength,
|
||||
);
|
||||
|
||||
// ✅ Sadece cache'den oku, yeni request gönderme
|
||||
const { data: cartData } = useGetCartQuery(undefined, {
|
||||
selectFromResult: (result) => ({
|
||||
data: result.data,
|
||||
}),
|
||||
refetchOnMountOrArgChange: false, // ✅ Mount'ta yeniden çağırma
|
||||
refetchOnFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
});
|
||||
const { getCartItem } = useCart();
|
||||
|
||||
const [addToCart] = useAddToCartMutation();
|
||||
const [updateCartItem] = useUpdateCartItemMutation();
|
||||
const [removeFromCart] = useRemoveFromCartMutation();
|
||||
|
||||
// ✅ Cart data'yı düzgün parse et
|
||||
const getCartItem = () => {
|
||||
if (!cartData || typeof cartData !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Eğer data grouped object ise (store bazlı)
|
||||
const allCartItems = Object.values(cartData).flat();
|
||||
|
||||
return allCartItems.find(
|
||||
(item) =>
|
||||
item.product?.id === product.id || item.product_id === product.id
|
||||
);
|
||||
};
|
||||
|
||||
const cartItem = getCartItem();
|
||||
const cartItem = getCartItem(product.id);
|
||||
const [localQuantity, setLocalQuantity] = useState(0);
|
||||
const [pendingQuantity, setPendingQuantity] = useState(0);
|
||||
|
||||
// ✅ Cart item değiştiğinde local state'i güncelle
|
||||
useEffect(() => {
|
||||
if (cartItem) {
|
||||
const qty = cartItem.quantity || cartItem.product_quantity || 0;
|
||||
setLocalQuantity(qty);
|
||||
setPendingQuantity(qty);
|
||||
} else {
|
||||
setLocalQuantity(0);
|
||||
setPendingQuantity(0);
|
||||
}
|
||||
}, [cartItem]); // ✅ Sadece cartItem değişince, cartData değil
|
||||
const qty = parseInt(
|
||||
cartItem?.quantity || cartItem?.product_quantity || 0,
|
||||
10,
|
||||
);
|
||||
setLocalQuantity(qty);
|
||||
setPendingQuantity(qty);
|
||||
}, [cartItem]);
|
||||
|
||||
// ✅ Favorite state'i güncelle
|
||||
useEffect(() => {
|
||||
if (Array.isArray(favoriteProducts)) {
|
||||
const isFav = favoriteProducts.some(
|
||||
(fav) => fav.product?.id === product.id
|
||||
(fav) => fav.product?.id === product.id,
|
||||
);
|
||||
setLocalIsFavorite(isFav);
|
||||
}
|
||||
@@ -138,38 +115,32 @@ const ProductCard = ({
|
||||
|
||||
// ✅ Debounced update - sadece mutation, refetch yok
|
||||
useEffect(() => {
|
||||
const updateCart = async () => {
|
||||
const currentCartQty =
|
||||
cartItem?.quantity || cartItem?.product_quantity || 0;
|
||||
const serverQty = parseInt(
|
||||
cartItem?.quantity || cartItem?.product_quantity || 0,
|
||||
10,
|
||||
);
|
||||
|
||||
if (pendingQuantity !== currentCartQty && pendingQuantity > 0) {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await updateCartItem({
|
||||
productId: product.id,
|
||||
quantity: pendingQuantity,
|
||||
}).unwrap();
|
||||
// ✅ RTK Query invalidatesTags ile otomatik güncellenecek
|
||||
} catch (error) {
|
||||
console.error("Failed to update cart item:", error);
|
||||
// ✅ Hata varsa önceki değere dön
|
||||
setLocalQuantity(currentCartQty);
|
||||
setPendingQuantity(currentCartQty);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const debouncedUpdate = debounce(updateCart, 300);
|
||||
|
||||
const currentCartQty =
|
||||
cartItem?.quantity || cartItem?.product_quantity || 0;
|
||||
if (pendingQuantity !== currentCartQty) {
|
||||
debouncedUpdate();
|
||||
if (pendingQuantity === serverQty || pendingQuantity <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return () => debouncedUpdate.cancel();
|
||||
const handler = setTimeout(async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await updateCartItem({
|
||||
productId: product.id,
|
||||
quantity: pendingQuantity,
|
||||
}).unwrap();
|
||||
} catch (error) {
|
||||
console.error("Failed to update cart item:", error);
|
||||
setLocalQuantity(serverQty);
|
||||
setPendingQuantity(serverQty);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, 500);
|
||||
|
||||
return () => clearTimeout(handler);
|
||||
}, [pendingQuantity, cartItem, product.id, updateCartItem]);
|
||||
|
||||
const handleQuantityIncrease = (event) => {
|
||||
|
||||
Reference in New Issue
Block a user