diff --git a/frontend/src/components/CardItem.jsx b/frontend/src/components/CardItem.jsx index c5b3478..2d5cd6a 100644 --- a/frontend/src/components/CardItem.jsx +++ b/frontend/src/components/CardItem.jsx @@ -2,7 +2,7 @@ import { useState } from "react"; import Arrow from "../components/Arrow"; import { useNavigate } from "react-router-dom"; -export default function CardItem({ item, onAddToCart }) { +export default function CardItem({ item, onAddToCart, onNavigate }) { const navigate = useNavigate(); const isPiece = item.unit_type === "piece"; const step = isPiece ? 1 : 0.1; @@ -20,11 +20,16 @@ export default function CardItem({ item, onAddToCart }) { ); }; + const handleGoToItem = () => { + if (onNavigate) onNavigate(); + navigate(`/items/${item.id}`); + }; + return (
navigate(`/items/${item.id}`)} + onClick={handleGoToItem} > sessionStorage.getItem("shopCategory") || "все", + ); const [loading, setLoading] = useState(true); - useEffect(() => { Promise.all([api.getAllItems(), api.getAllCategories()]) .then(([itemsRes, categoriesRes]) => { @@ -24,16 +25,32 @@ export default function Shop() { }); }, []); + // восстановление скролла после загрузки товаров + useEffect(() => { + if (!loading && items.length > 0) { + const saved = sessionStorage.getItem("shopScroll"); + if (saved) { + window.scrollTo(0, parseInt(saved)); + sessionStorage.removeItem("shopScroll"); + } + } + }, [loading, items]); + const filteredItems = useMemo(() => { return activeCategory === "все" ? items || [] : (items || []).filter((item) => { if (!item || !item.category) return false; - return item.category.toLowerCase() === activeCategory.toLowerCase(); }); }, [items, activeCategory]); + // сохранить позицию и категорию перед уходом на товар + const handleNavigateToItem = () => { + sessionStorage.setItem("shopScroll", window.scrollY); + sessionStorage.setItem("shopCategory", activeCategory); + }; + const handleAddToCart = async (product, selectedWeight) => { try { await api.addToCart({ @@ -48,11 +65,11 @@ export default function Shop() { }; if (loading) { - return( + return (

Загрузка...

- ) + ); } return ( @@ -68,7 +85,12 @@ export default function Shop() { />
{filteredItems.map((item) => ( - + ))}