add categories
This commit is contained in:
@@ -11,7 +11,7 @@ export interface Props {
|
||||
hasNextPage: boolean;
|
||||
endCursor: string | null;
|
||||
};
|
||||
perLoad?: number; // Переименовали first в perLoad
|
||||
perLoad?: number;
|
||||
}
|
||||
|
||||
const {
|
||||
@@ -20,15 +20,14 @@ const {
|
||||
type = 'latest',
|
||||
slug = '',
|
||||
pageInfo = { hasNextPage: false, endCursor: null },
|
||||
perLoad = 11 // perLoad на верхнем уровне с дефолтом 11
|
||||
perLoad = 11
|
||||
} = Astro.props;
|
||||
|
||||
// Формируем конфиг для sentinel из пропсов верхнего уровня
|
||||
// Внутри оставляем поле first для совместимости с API и скриптом
|
||||
// Используем perLoad везде
|
||||
const loadMoreConfig = {
|
||||
type,
|
||||
slug,
|
||||
first: perLoad // Маппим perLoad в first для обратной совместимости
|
||||
perLoad // Теперь используем perLoad вместо first
|
||||
};
|
||||
|
||||
function getCoauthorsNames(coauthors: any[]): string {
|
||||
@@ -163,7 +162,7 @@ function shouldBeLarge(index: number): boolean {
|
||||
interface LoadMoreConfig {
|
||||
type: 'latest' | 'category' | 'author' | 'tag';
|
||||
slug?: string;
|
||||
perLoad?: number; // В скрипте оставляем first для совместимости
|
||||
perLoad: number; // Только perLoad, никакого first
|
||||
}
|
||||
|
||||
class InfinityScroll {
|
||||
@@ -186,15 +185,25 @@ function shouldBeLarge(index: number): boolean {
|
||||
this.noMorePosts = document.getElementById('no-more-posts');
|
||||
this.postsCount = document.getElementById('posts-count');
|
||||
|
||||
const defaultConfig: LoadMoreConfig = { type: 'latest', first: 11 };
|
||||
// Дефолтный конфиг только с perLoad
|
||||
const defaultConfig: LoadMoreConfig = {
|
||||
type: 'latest',
|
||||
perLoad: 11
|
||||
};
|
||||
|
||||
if (this.sentinel) {
|
||||
this.endCursor = this.sentinel.dataset.endCursor || null;
|
||||
this.currentIndex = parseInt(this.sentinel.dataset.currentIndex || '0');
|
||||
|
||||
try {
|
||||
this.loadMoreConfig = JSON.parse(this.sentinel.dataset.loadConfig || '{}');
|
||||
this.loadMoreConfig = { ...defaultConfig, ...this.loadMoreConfig };
|
||||
// Парсим конфиг из data-атрибута
|
||||
const parsedConfig = JSON.parse(this.sentinel.dataset.loadConfig || '{}');
|
||||
this.loadMoreConfig = {
|
||||
...defaultConfig,
|
||||
...parsedConfig,
|
||||
// Убеждаемся, что perLoad определен
|
||||
perLoad: parsedConfig.perLoad || defaultConfig.perLoad
|
||||
};
|
||||
} catch {
|
||||
this.loadMoreConfig = defaultConfig;
|
||||
}
|
||||
@@ -233,13 +242,14 @@ function shouldBeLarge(index: number): boolean {
|
||||
this.showLoading();
|
||||
|
||||
try {
|
||||
// Отправляем только perLoad (никакого first)
|
||||
const response = await fetch('/load-more-posts', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
perLoad: this.loadMoreConfig.perLoad || 11,
|
||||
perLoad: this.loadMoreConfig.perLoad,
|
||||
after: this.endCursor,
|
||||
type: this.loadMoreConfig.type,
|
||||
slug: this.loadMoreConfig.slug,
|
||||
@@ -248,7 +258,7 @@ function shouldBeLarge(index: number): boolean {
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Ошибка загрузки постов');
|
||||
throw new Error(`Ошибка загрузки постов: ${response.status}`);
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
@@ -274,7 +284,8 @@ function shouldBeLarge(index: number): boolean {
|
||||
|
||||
this.grid?.appendChild(fragment);
|
||||
|
||||
this.currentIndex += this.loadMoreConfig.first || 11;
|
||||
// Используем perLoad для увеличения индекса
|
||||
this.currentIndex += this.loadMoreConfig.perLoad;
|
||||
this.endCursor = newEndCursor;
|
||||
this.hasMore = hasNextPage;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user