work version archive
This commit is contained in:
@@ -1,15 +1,44 @@
|
||||
---
|
||||
import { parseSlug } from '@utils/slugParser';
|
||||
|
||||
import { slugParse } from '@utils/slugParser';
|
||||
import SimplePagination from '@components/Pagination.astro';
|
||||
import { getTagBySlug, getPostsByTagPaginated } from '@api/posts.js';
|
||||
|
||||
//api
|
||||
import { getTag, getTagWithPostsById } from '@api/tags.js';
|
||||
import { getArchivePostsById } from '@api/archiveById.ts';
|
||||
|
||||
import MainLayout from '@layouts/MainLayout.astro';
|
||||
|
||||
export const prerender = false; // динамический роутинг
|
||||
const { slug: rawSlug } = Astro.params;
|
||||
|
||||
// Используем функцию
|
||||
const { slug: tagSlug, page: currentPage } = parseSlug(rawSlug);
|
||||
const { slug } = Astro.params;
|
||||
|
||||
// Используем функцию (правильное название)
|
||||
const { slug: tagSlug, page: currentPage } = slugParse(slug);
|
||||
|
||||
// Если slug пустой - 404
|
||||
if (!tagSlug) {
|
||||
return Astro.redirect('/404');
|
||||
}
|
||||
|
||||
|
||||
// Получаем данные тега
|
||||
const tag = await getTag(tagSlug);
|
||||
|
||||
if (!tag) {
|
||||
return Astro.redirect('/404');
|
||||
}
|
||||
|
||||
|
||||
const { posts, pageInfo } = await getArchivePostsById({
|
||||
type: 'tag',
|
||||
id: tag.databaseId, // ID тега
|
||||
page: 2,
|
||||
perPage: 10
|
||||
});
|
||||
|
||||
|
||||
console.log('Данные, полученные от pageInfo:', pageInfo);
|
||||
|
||||
|
||||
---
|
||||
@@ -19,6 +48,101 @@ const { slug: tagSlug, page: currentPage } = parseSlug(rawSlug);
|
||||
description="Информационное агентство Деловой журнал Профиль"
|
||||
>
|
||||
|
||||
<p>Current page:{currentPage}</p>
|
||||
<div class="container mx-auto px-4 py-8">
|
||||
<!-- Заголовок тега -->
|
||||
<header class="mb-8">
|
||||
<h1 class="text-3xl md:text-4xl font-bold mb-2">
|
||||
Тег: {tag.name}
|
||||
</h1>
|
||||
|
||||
{tag.description && (
|
||||
<div class="text-lg text-gray-600 mb-4" set:html={tag.description} />
|
||||
)}
|
||||
|
||||
<div class="text-gray-500">
|
||||
<span>Всего постов: {data.pagination?.totalPosts || 0}</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- Список постов -->
|
||||
{data.posts && data.posts.length > 0 ? (
|
||||
<>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
|
||||
{data.posts.map(post => (
|
||||
<article class="bg-white rounded-lg shadow-md overflow-hidden hover:shadow-lg transition-shadow duration-300">
|
||||
{post.featuredImage?.node?.sourceUrl && (
|
||||
<a href={post.uri} class="block">
|
||||
<img
|
||||
src={post.featuredImage.node.sourceUrl}
|
||||
alt={post.featuredImage.node.altText || post.title}
|
||||
width="400"
|
||||
height="250"
|
||||
loading="lazy"
|
||||
class="w-full h-48 object-cover"
|
||||
/>
|
||||
</a>
|
||||
)}
|
||||
|
||||
<div class="p-4">
|
||||
<h2 class="text-xl font-semibold mb-2">
|
||||
<a href={post.uri} class="hover:text-blue-600 transition-colors">
|
||||
{post.title}
|
||||
</a>
|
||||
</h2>
|
||||
|
||||
{post.excerpt && (
|
||||
<div
|
||||
class="text-gray-600 mb-3 line-clamp-3"
|
||||
set:html={post.excerpt}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div class="flex items-center justify-between text-sm text-gray-500">
|
||||
<time datetime={post.date}>
|
||||
{new Date(post.date).toLocaleDateString('ru-RU')}
|
||||
</time>
|
||||
|
||||
{post.categories?.nodes?.length > 0 && (
|
||||
<div class="flex gap-1">
|
||||
{post.categories.nodes.slice(0, 2).map(cat => (
|
||||
<a
|
||||
href={`/category/${cat.slug}`}
|
||||
class="px-2 py-1 bg-gray-100 rounded hover:bg-gray-200"
|
||||
>
|
||||
{cat.name}
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<!-- Пагинация -->
|
||||
{data.pagination && data.pagination.totalPages > 1 && (
|
||||
<SimplePagination
|
||||
currentPage={pageNumber}
|
||||
totalPages={data.pagination.totalPages}
|
||||
baseUrl={baseUrl}
|
||||
showPrevNext={true}
|
||||
showFirstLast={true}
|
||||
maxVisible={7}
|
||||
className="mt-8"
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div class="text-center py-12">
|
||||
<h3 class="text-2xl font-semibold mb-4">Постов пока нет</h3>
|
||||
<p class="text-gray-600">В этом теге еще нет опубликованных статей.</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</MainLayout>
|
||||
|
||||
|
||||
|
||||
</MainLayout>
|
||||
Reference in New Issue
Block a user