import { Head, Link } from '@inertiajs/react'; import { useEffect, useState } from 'react'; import { type BreadcrumbItem } from '@/types'; import AppLayout from '@/layouts/app-layout'; import { Pagination } from '@/components/pagination'; const breadcrumbs: BreadcrumbItem[] = [ { title: 'Strona Główna', href: '/', }, { title: 'Aktualności', href: '/aktualnosci', }, ]; interface News { id: number; title: string; slug: string; thumbnail: string; body: string; active: boolean; published_at: string; created_at: string; updated_at: string; user_id: number; } interface PaginationData { total: number; per_page: number; current_page: number; last_page: number; } interface NewsApiResponse { articleNews: Record; pagination: PaginationData; } interface NewsProps { page?: string | number; } export default function News({ page = 1 }: NewsProps) { const [news, setNews] = useState(null); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(typeof page === 'string' ? parseInt(page) : page); useEffect(() => { const initialPage = typeof page === 'string' ? parseInt(page) : page; if (initialPage !== currentPage) { setCurrentPage(initialPage); } }, [page]); useEffect(() => { setLoading(true); fetch(`/api/news?page=${currentPage}&per_page=10`) .then(response => response.json()) .then(data => { setNews(data); setLoading(false); }) .catch(error => { setNews(null); setLoading(false); console.error('Error fetching news data:', error); }); }, [currentPage]); return (

Aktualności

{loading ? (
Wczytywanie...
) : !news || Object.keys(news.articleNews).length === 0 ? (
Brak artykułów.
) : (
{Object.values(news.articleNews).map((article) => { function stripHtml(html: string) { const htmlWithSpaces = html .replace(//gi, ' ') .replace(/<\/p>

/gi, ' ') .replace(/<\/div>

/gi, ' ') .replace(/<\/h[1-6]>/gi, ' ') .replace(/<\/li>
  • /gi, ' '); const div = document.createElement('div'); div.innerHTML = htmlWithSpaces; return (div.textContent || div.innerText || '') .replace(/\s+/g, ' ') .trim(); } const plainText = stripHtml(article.body); const shortText = plainText.length > 200 ? plainText.slice(0, 200).replace(/\s+$/, '') + '…' : plainText; return (

    {article.title}

    Data publikacji: {new Date(article.published_at).toLocaleDateString('pl-PL')}
    {shortText}
    Czytaj więcej
    ); })}
  • {news.pagination && news.pagination.total > 10 && ( { setCurrentPage(page); window.scrollTo(0, 0); if (page === 1) { window.history.pushState({}, '', '/aktualnosci'); } else { window.history.pushState({}, '', `/aktualnosci?strona=${page}`); } }} /> )}
    )}
    ); }