bugfix on homepage (filter and buttons)

This commit is contained in:
Laura Dolibois
2025-05-31 20:56:06 +02:00
parent d97a135b2f
commit 339af6f318
4 changed files with 42 additions and 23 deletions

View File

@@ -38,6 +38,3 @@ export default function App() {
</StyledEngineProvider> </StyledEngineProvider>
) )
} }
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);

View File

@@ -39,9 +39,6 @@ export default function ItemCard({ item }: { item: Item }) {
<Rating name="half-rating" readOnly defaultValue={item.rating} precision={0.5} /> <Rating name="half-rating" readOnly defaultValue={item.rating} precision={0.5} />
<Typography variant="body2" sx={{ color: 'text.secondary' }} className="item-description"> <Typography variant="body2" sx={{ color: 'text.secondary' }} className="item-description">
{(item.price * (1 - item.discount / 100)).toFixed(2)} {(item.price * (1 - item.discount / 100)).toFixed(2)}
<IconButton aria-label={t('addToCart')} onClick={handleAddToCart}>
<AddShoppingCart />
</IconButton>
</Typography> </Typography>
{item.stock > 10 ? ( {item.stock > 10 ? (
@@ -59,6 +56,11 @@ export default function ItemCard({ item }: { item: Item }) {
)} )}
</CardContent> </CardContent>
</CardActionArea> </CardActionArea>
<CardContent>
<IconButton aria-label={t('addToCart')} onClick={handleAddToCart}>
<AddShoppingCart />
</IconButton>
</CardContent>
</Card> </Card>
</Paper> </Paper>
) )

View File

@@ -1,11 +1,17 @@
import './components/i18n/i18n'; import './components/i18n/i18n';
import { StrictMode } from 'react' import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client' import { createRoot } from 'react-dom/client';
import './index.css' import './index.css';
import App from './App.tsx' import App from './App.tsx';
createRoot(document.getElementById('root')!).render( console.log("main.tsx wurde geladen");
<StrictMode>
<App /> const rootElement = document.getElementById('root');
</StrictMode>, if (!rootElement) throw new Error("Root element not found");
)
const root = createRoot(rootElement);
root.render(
<StrictMode>
<App />
</StrictMode>
);

View File

@@ -4,6 +4,7 @@ import Item from "../components/Item";
import FilterItem from "../helper/homepage/FilterItem"; import FilterItem from "../helper/homepage/FilterItem";
import ItemCard from "../helper/homepage/ItemCard"; import ItemCard from "../helper/homepage/ItemCard";
import "./pages.css"; // Import der CSS-Datei import "./pages.css"; // Import der CSS-Datei
import { useNavigate } from "react-router-dom"
import { useLocation } from "react-router-dom"; import { useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import PriceSlider from "../helper/homepage/PriceSlider"; import PriceSlider from "../helper/homepage/PriceSlider";
@@ -11,6 +12,7 @@ import PriceSlider from "../helper/homepage/PriceSlider";
export default function Home() { export default function Home() {
const { t } = useTranslation(); const { t } = useTranslation();
const navigate = useNavigate();
const items: Item[] = [ const items: Item[] = [
{ {
@@ -174,9 +176,13 @@ export default function Home() {
}, [location.search]); }, [location.search]);
const handleCategoryChange = (category: string) => { const handleCategoryChange = (category: string) => {
setSelectedCategory(category); if (category === "") {
// Optional: URL aktualisieren setSelectedCategory(null);
// navigate(`/?category=${encodeURIComponent(category)}`); navigate(`/`);
} else {
setSelectedCategory(category);
navigate(`/?category=${encodeURIComponent(category)}`);
}
}; };
// Handler für die Pagination // Handler für die Pagination
@@ -200,20 +206,28 @@ export default function Home() {
const filteredCount = filteredItems.length; const filteredCount = filteredItems.length;
// Aktualisiere die Anzahl der Karten bei Größenänderung // Aktualisiert die sichtbaren Items bei Seitenwechsel oder Fenstergrößenänderung
useEffect(() => { useEffect(() => {
const updateItems = () => { const updateItems = () => {
const newItemsPerPage = calculateItemsPerPage(); const newItemsPerPage = calculateItemsPerPage();
setItemsPerPage(newItemsPerPage); setItemsPerPage(newItemsPerPage);
const startIndex = (currentPage - 1) * newItemsPerPage; const startIndex = (currentPage - 1) * newItemsPerPage;
setVisibleItems(filteredItems.slice(startIndex, startIndex + newItemsPerPage)); const newVisibleItems = filteredItems.slice(startIndex, startIndex + newItemsPerPage);
setVisibleItems(prev => (JSON.stringify(prev) !== JSON.stringify(newVisibleItems) ? newVisibleItems : prev));
}; };
updateItems(); updateItems();
window.addEventListener("resize", updateItems);
return () => window.removeEventListener("resize", updateItems); const handleResize = () => {
}, [currentPage, filteredItems]); updateItems();
};
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, [currentPage, priceRange, selectedCategory, selectedRating]);
// Dynamische Berechnung der Anzahl der Items pro Seite basierend auf der Fensterbreite // Dynamische Berechnung der Anzahl der Items pro Seite basierend auf der Fensterbreite
const calculateItemsPerPage = () => { const calculateItemsPerPage = () => {