Новые изменения
This commit is contained in:
parent
dc00e31578
commit
e621b3c40e
8 changed files with 234 additions and 54 deletions
|
|
@ -314,20 +314,15 @@ const {
|
|||
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');
|
||||
}
|
||||
fetch(`/api/increment-views?postId=${postId}`, { method: 'POST' })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.views !== undefined) {
|
||||
viewsEl.textContent = formatViews(data.views);
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
function formatViews(n: number): string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue