import { useEffect, useState } from 'react'; import { useSearchParams } from 'react-router-dom'; import { ArticleCard } from './ArticleCard'; import { Pagination } from './Pagination'; import { Article, CategoryTitles, CityTitles } from '../types'; import axios from "axios"; const ARTICLES_PER_PAGE = 6; export function FeaturedSection() { const [searchParams, setSearchParams] = useSearchParams(); const category = searchParams.get('category'); const city = searchParams.get('city'); const currentPage = Math.max(1, parseInt(searchParams.get('page') || '1', 10)); const [articles, setArticles] = useState([]); const [totalPages, setTotalPages] = useState(1); const [totalArticles, setTotalArticles] = useState(0); const [error, setError] = useState(null); // Загрузка статей useEffect(() => { const fetchArticles = async () => { try { const response = await axios.get('/api/articles/', { params: { page: currentPage, categoryId: category || undefined, cityId: city || undefined, }, }); console.log(response.data.articles[0]); setArticles(response.data.articles); setTotalPages(response.data.totalPages); setTotalArticles(response.data.total); } catch (error) { setError('Не удалось загрузить статьи'); console.error(error); } }; fetchArticles(); }, [category, city, currentPage]); const handlePageChange = (page: number) => { searchParams.set('page', page.toString()); setSearchParams(searchParams); window.scrollTo({ top: 0, behavior: 'smooth' }); }; if (articles.length === 0) { return (

Еще нет заметок

Выберите другой раздел или город

); } const shouldShowFeatured = currentPage === 1 && !category && !city; return (

{city ? `${CityTitles[Number(city)]} ` : ''} {category ? `${CategoryTitles[Number(category)]}` : 'Читайте сегодня'}

Статьи {Math.min((currentPage - 1) * ARTICLES_PER_PAGE + 1, totalArticles)} - {Math.min(currentPage * ARTICLES_PER_PAGE, totalArticles)} из {totalArticles}

{error && (
{error}
)}
{articles.map((article, index) => ( ))}
{totalPages > 1 && ( )}
); }