import React, { useState, useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import styles from '../../css/RecipesCss/recipe-style.module.css';
import Header from '../HomeComponents/Header';
import Footer from '../HomeComponents/Footer';
const Recipes = () => {
const [recipes, setRecipes] = useState([]);
const [page, setPage] = useState(0);
const [totalPages, setTotalPages] = useState(0);
const [loading, setLoading] = useState(false);
const location = useLocation();
const navigate = useNavigate();
const getSearchParams = (location) => {
const searchParams = new URLSearchParams(location.search);
const nationality = searchParams.get('nationality');
const category = searchParams.get('category');
const page = parseInt(searchParams.get('page'), 10);
const productIds = searchParams.getAll('productId').map(id => parseInt(id));
const searchTerm = searchParams.get('searchTerm');
return {
nationality,
category,
page: isNaN(page) ? 0 : page,
productIds,
searchTerm,
};
};
useEffect(() => {
const { nationality, category, page, productIds, searchTerm } = getSearchParams(location);
setPage(page);
loadRecipes(page, nationality, category, productIds, searchTerm);
}, [location]);
const loadRecipes = (page, nationality, category, productIds, searchTerm) => {
setLoading(true);
let url = `http://localhost:8080/api/recipes?page=${page}&size=9`;
if (searchTerm) {
url += `&searchTerm=${encodeURIComponent(searchTerm)}`;
}
if (nationality) {
url += `&nationality=${nationality}`;
}
if (category) {
url += `&category=${category}`;
}
if (productIds && productIds.length > 0) {
productIds.forEach(id => {
url += `&productId=${id}`;
});
}
fetch(url)
.then(response => response.json())
.then(data => {
setRecipes(data.content);
setTotalPages(data.totalPages);
setTimeout(() => {
setLoading(false);
}, 300);
window.scrollTo({ top: 0, behavior: 'smooth' });
})
.catch(error => {
console.error('Error fetching recipes:', error);
setLoading(false);
});
};
const handlePageChange = (newPage) => {
if (newPage !== page && newPage >= 0 && newPage < totalPages) {
setPage(newPage);
const searchParams = new URLSearchParams(location.search);
searchParams.set('page', newPage);
navigate(`/recipes?${searchParams.toString()}`);
}
};
const navigateToRecipe = (id) => {
const { nationality, category, productIds, page, searchTerm } = getSearchParams(location);
navigate(`/recipes/${id}`, {
state: {
category,
nationality,
productIds,
page,
searchTerm,
previousSearch: location.search,
},
});
};
return (
<>