ДобавлениеÐ Яндекс верификации
This commit is contained in:
parent
aa40fac43b
commit
2e892cccfb
17 changed files with 190 additions and 205 deletions
|
|
@ -3,7 +3,8 @@ import { MONTHS } from "@lib/constants";
|
|||
import NumberedContent from "./NumberedContent.astro";
|
||||
import AuthorConsultation from "./AuthorConsultation.astro";
|
||||
|
||||
export interface Props {
|
||||
// Определяем интерфейс пропсов
|
||||
interface Props {
|
||||
title: string;
|
||||
excerpt: string;
|
||||
author: {
|
||||
|
|
@ -30,6 +31,7 @@ function formatDate(dateInput: string | Date): string {
|
|||
return `${date.getDate()} ${MONTHS[date.getMonth()]} ${date.getFullYear()} года`;
|
||||
}
|
||||
|
||||
// Деструктуризация пропсов через Astro.props с явной типизацией
|
||||
const {
|
||||
title,
|
||||
excerpt,
|
||||
|
|
@ -43,9 +45,9 @@ const {
|
|||
postId,
|
||||
likes = 0,
|
||||
dislikes = 0,
|
||||
} = Astro.props;
|
||||
} = Astro.props as Props;
|
||||
|
||||
// дата поста
|
||||
// Дата поста
|
||||
const formattedPublishDate = typeof publishDate === "string"
|
||||
? publishDate
|
||||
: formatDate(publishDate);
|
||||
|
|
@ -304,8 +306,6 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<script>
|
||||
// Инициализация виджета голосования
|
||||
(function() {
|
||||
|
|
@ -320,22 +320,20 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
|
||||
let likes = parseInt(widget.getAttribute('data-initial-likes') || '0');
|
||||
let dislikes = parseInt(widget.getAttribute('data-initial-dislikes') || '0');
|
||||
let userVote = null;
|
||||
let userVote: string | null = null;
|
||||
let isLoading = false;
|
||||
let isDisabled = false; // Блокировка кнопок
|
||||
let isDisabled = false;
|
||||
|
||||
console.log('[PostLikes] Инициализация:', { postId, likes, dislikes });
|
||||
|
||||
// Блокировка/разблокировка кнопок
|
||||
function setButtonsDisabled(disabled) {
|
||||
function setButtonsDisabled(disabled: boolean) {
|
||||
isDisabled = disabled;
|
||||
if (likeBtn) likeBtn.disabled = disabled;
|
||||
if (dislikeBtn) dislikeBtn.disabled = disabled;
|
||||
widget.style.opacity = disabled ? '0.6' : '1';
|
||||
widget.style.pointerEvents = disabled ? 'none' : 'auto';
|
||||
if (likeBtn) (likeBtn as HTMLButtonElement).disabled = disabled;
|
||||
if (dislikeBtn) (dislikeBtn as HTMLButtonElement).disabled = disabled;
|
||||
(widget as HTMLElement).style.opacity = disabled ? '0.6' : '1';
|
||||
(widget as HTMLElement).style.pointerEvents = disabled ? 'none' : 'auto';
|
||||
}
|
||||
|
||||
// Загрузка реальных данных
|
||||
async function loadVotes() {
|
||||
if (!postId) return;
|
||||
|
||||
|
|
@ -344,7 +342,7 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
console.log('[PostLikes] Ответ API:', response.status);
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
const data = await response.json() as { likes: number; dislikes: number; userVote: string | null };
|
||||
console.log('[PostLikes] Данные:', data);
|
||||
|
||||
likes = data.likes || 0;
|
||||
|
|
@ -357,9 +355,7 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
}
|
||||
}
|
||||
|
||||
// Голосование
|
||||
async function handleVote(action) {
|
||||
// Двойная проверка блокировки
|
||||
async function handleVote(action: 'like' | 'dislike') {
|
||||
if (isLoading || isDisabled) {
|
||||
console.log('[PostLikes] Заблокировано, пропускаем');
|
||||
return;
|
||||
|
|
@ -369,11 +365,9 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
|
||||
if (!postId) return;
|
||||
|
||||
// Блокируем кнопки СРАЗУ
|
||||
isLoading = true;
|
||||
setButtonsDisabled(true);
|
||||
|
||||
// Оптимистичное обновление
|
||||
if (action === 'like') {
|
||||
if (userVote === 'like') {
|
||||
likes = Math.max(0, likes - 1);
|
||||
|
|
@ -409,38 +403,34 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
body: JSON.stringify({ post_id: postId, vote_type: action }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
const data = await response.json() as { error?: string; likes?: number; dislikes?: number; userVote?: string | null };
|
||||
console.log('[PostLikes] Ответ сервера:', response.status, data);
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
alert('Для голосования необходимо войти в систему');
|
||||
loadVotes(); // Откат
|
||||
loadVotes();
|
||||
return;
|
||||
}
|
||||
throw new Error(data.error || 'Ошибка');
|
||||
}
|
||||
|
||||
// Обновляем реальными данными с сервера
|
||||
likes = data.likes || 0;
|
||||
dislikes = data.dislikes || 0;
|
||||
userVote = data.userVote;
|
||||
userVote = data.userVote || null;
|
||||
updateUI();
|
||||
|
||||
console.log('[PostLikes] Успешно:', { likes, dislikes, userVote });
|
||||
} catch (error) {
|
||||
console.error('[PostLikes] Ошибка:', error);
|
||||
alert('Не удалось сохранить голос');
|
||||
loadVotes(); // Откат
|
||||
loadVotes();
|
||||
} finally {
|
||||
isLoading = false;
|
||||
// НЕ разблокируем кнопки - голосование окончено
|
||||
// Кнопки остаются заблокированными для повторного голосования
|
||||
console.log('[PostLikes] Голосование завершено');
|
||||
}
|
||||
}
|
||||
|
||||
// Обновление UI
|
||||
function updateUI() {
|
||||
if (likeCount) likeCount.textContent = likes.toString();
|
||||
if (dislikeCount) dislikeCount.textContent = dislikes.toString();
|
||||
|
|
@ -459,20 +449,15 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
}
|
||||
}
|
||||
|
||||
// Обработчики
|
||||
likeBtn?.addEventListener('click', () => handleVote('like'));
|
||||
dislikeBtn?.addEventListener('click', () => handleVote('dislike'));
|
||||
|
||||
// Загружаем реальные данные
|
||||
loadVotes();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
|
||||
</main>
|
||||
|
||||
<!-- Боковой блок -->
|
||||
<aside class="lg:col-span-4">
|
||||
<AuthorConsultation author={author} />
|
||||
</aside>
|
||||
|
|
@ -517,7 +502,6 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
/* Лайк */
|
||||
.vote-btn.like-btn:hover {
|
||||
border-color: #86efac;
|
||||
background: #f0fdf4;
|
||||
|
|
@ -530,7 +514,6 @@ const formattedPublishDate = typeof publishDate === "string"
|
|||
color: #ffffff;
|
||||
}
|
||||
|
||||
/* Дизлайк */
|
||||
.vote-btn.dislike-btn:hover {
|
||||
border-color: #fca5a5;
|
||||
background: #fef2f2;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ interface Props {
|
|||
pagePath?: string;
|
||||
blogPostTitle?: string;
|
||||
bodyClass?: string;
|
||||
noContainer?: boolean; // Новый проп для отключения контейнера
|
||||
noContainer?: boolean;
|
||||
}
|
||||
|
||||
const props = Astro.props || {};
|
||||
|
|
@ -46,6 +46,8 @@ if (Astro.request) {
|
|||
<title>{title}</title>
|
||||
{description ? <meta name="description" content={description} /> : null}
|
||||
{canonicalLink ? <link rel="canonical" href={canonicalLink} /> : null}
|
||||
<!-- yandex verification -->
|
||||
<meta name="yandex-verification" content="b10d32bf1e46a882" />
|
||||
</head>
|
||||
<body class={`${props.bodyClass || "bg-gray-50"}`}>
|
||||
<Header />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue