Новые изменения в компоненты
This commit is contained in:
parent
faf02848ed
commit
735289481c
7 changed files with 151 additions and 3 deletions
|
|
@ -9,6 +9,7 @@ import ConsultationModal from "@components/base/ConsultationModal.astro";
|
|||
import Toast from "@components/base/Toast.astro";
|
||||
import PostSocialShare from "@components/blog/PostSocialShare.astro";
|
||||
import PostReactionButtons from "@components/blog/PostReactionButtons.astro";
|
||||
import PostViewCounter from "@components/blog/PostViewCounter.astro";
|
||||
|
||||
export interface Props {
|
||||
title: string;
|
||||
|
|
@ -27,6 +28,7 @@ export interface Props {
|
|||
postUrl: string;
|
||||
initialLikes?: number;
|
||||
initialDislikes?: number;
|
||||
initialViews?: number;
|
||||
}
|
||||
|
||||
const {
|
||||
|
|
@ -45,7 +47,8 @@ const {
|
|||
postId,
|
||||
postUrl,
|
||||
initialLikes = 0,
|
||||
initialDislikes = 0
|
||||
initialDislikes = 0,
|
||||
initialViews = 0
|
||||
} = Astro.props;
|
||||
---
|
||||
|
||||
|
|
@ -104,6 +107,10 @@ const {
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="meta-icon"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
|
||||
{readmeTime} мин
|
||||
</span>
|
||||
<span class="meta-item">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="meta-icon"><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"></path><circle cx="12" cy="12" r="3"></circle></svg>
|
||||
<span class="meta-views" data-post-id={postId}>{initialViews}</span>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="article-actions">
|
||||
|
|
@ -302,6 +309,33 @@ const {
|
|||
consultationBtn?.addEventListener("click", () => {
|
||||
window.dispatchEvent(new CustomEvent("open-modal", { detail: "consultation-modal" }));
|
||||
});
|
||||
|
||||
// 5. СЧЁТЧИК ПРОСМОТРОВ
|
||||
const viewsEl = document.querySelector('.meta-views') as HTMLElement & { dataset: { postId: string } };
|
||||
if (viewsEl?.dataset?.postId) {
|
||||
const postId = viewsEl.dataset.postId;
|
||||
const hasViewed = sessionStorage.getItem(`viewed_${postId}`);
|
||||
|
||||
if (!hasViewed) {
|
||||
fetch(`/api/increment-views?postId=${postId}`, { method: 'POST' })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.views !== undefined) {
|
||||
viewsEl.textContent = formatViews(data.views);
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
|
||||
sessionStorage.setItem(`viewed_${postId}`, 'true');
|
||||
}
|
||||
}
|
||||
|
||||
function formatViews(n: number): string {
|
||||
if (n >= 1000) {
|
||||
return (n / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
|
||||
}
|
||||
return n.toString();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", setupArticleLogic);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue