like-buttons: gelikte posts bleiben markiert via localStorage (frontpage + detail)
This commit is contained in:
@@ -2338,6 +2338,18 @@ def public_frontend():
|
|||||||
<script>
|
<script>
|
||||||
// Like button handler
|
// Like button handler
|
||||||
(function() {
|
(function() {
|
||||||
|
var likedPosts = JSON.parse(localStorage.getItem('likedPosts') || '[]');
|
||||||
|
|
||||||
|
// Restore liked state on page load
|
||||||
|
if (likedPosts.length) {
|
||||||
|
document.querySelectorAll('.stat-like-btn').forEach(function(btn) {
|
||||||
|
var id = parseInt(btn.getAttribute('data-id'));
|
||||||
|
if (likedPosts.indexOf(id) !== -1) {
|
||||||
|
btn.classList.add('liked');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
document.querySelectorAll('.stat-like-btn').forEach(function(btn) {
|
document.querySelectorAll('.stat-like-btn').forEach(function(btn) {
|
||||||
btn.addEventListener('click', function(e) {
|
btn.addEventListener('click', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -2352,6 +2364,8 @@ def public_frontend():
|
|||||||
this.classList.add('liked');
|
this.classList.add('liked');
|
||||||
this.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#20228a" stroke-width="2" stroke-linecap="round" style="vertical-align:middle;margin-right:2px;"><path d="M7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"/><path d="M7 11h11.5a2.5 2.5 0 0 1 2.5 2.5v.5a2.5 2.5 0 0 1-2.5 2.5h-2.5"/><path d="M16 16.5V19a3 3 0 0 1-6 1.5L7 11"/></svg> ' + data.likes;
|
this.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#20228a" stroke-width="2" stroke-linecap="round" style="vertical-align:middle;margin-right:2px;"><path d="M7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"/><path d="M7 11h11.5a2.5 2.5 0 0 1 2.5 2.5v.5a2.5 2.5 0 0 1-2.5 2.5h-2.5"/><path d="M16 16.5V19a3 3 0 0 1-6 1.5L7 11"/></svg> ' + data.likes;
|
||||||
this.setAttribute('data-likes', data.likes);
|
this.setAttribute('data-likes', data.likes);
|
||||||
|
likedPosts.push(parseInt(id));
|
||||||
|
localStorage.setItem('likedPosts', JSON.stringify(likedPosts));
|
||||||
}
|
}
|
||||||
this.disabled = false;
|
this.disabled = false;
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
@@ -3205,10 +3219,19 @@ def public_detail(sub_id: int):
|
|||||||
(function(){{
|
(function(){{
|
||||||
var btn = document.getElementById('detail-like-btn');
|
var btn = document.getElementById('detail-like-btn');
|
||||||
if (!btn) return;
|
if (!btn) return;
|
||||||
|
|
||||||
|
var likedPosts = JSON.parse(localStorage.getItem('likedPosts') || '[]');
|
||||||
|
var id = parseInt(btn.getAttribute('data-id'));
|
||||||
|
if (likedPosts.indexOf(id) !== -1) {{
|
||||||
|
btn.classList.add('liked');
|
||||||
|
btn.style.background = '#20228a';
|
||||||
|
btn.style.color = '#fff';
|
||||||
|
btn.style.borderColor = '#20228a';
|
||||||
|
}}
|
||||||
|
|
||||||
btn.addEventListener('click', function() {{
|
btn.addEventListener('click', function() {{
|
||||||
if (this.classList.contains('liked')) return;
|
if (this.classList.contains('liked')) return;
|
||||||
this.disabled = true;
|
this.disabled = true;
|
||||||
var id = this.getAttribute('data-id');
|
|
||||||
fetch('/api/like/' + id, {{method:'POST'}})
|
fetch('/api/like/' + id, {{method:'POST'}})
|
||||||
.then(function(r) {{ return r.json(); }})
|
.then(function(r) {{ return r.json(); }})
|
||||||
.then(function(data) {{
|
.then(function(data) {{
|
||||||
@@ -3218,6 +3241,8 @@ def public_detail(sub_id: int):
|
|||||||
btn.style.background = '#20228a';
|
btn.style.background = '#20228a';
|
||||||
btn.style.color = '#fff';
|
btn.style.color = '#fff';
|
||||||
btn.style.borderColor = '#20228a';
|
btn.style.borderColor = '#20228a';
|
||||||
|
likedPosts.push(id);
|
||||||
|
localStorage.setItem('likedPosts', JSON.stringify(likedPosts));
|
||||||
}}
|
}}
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
}})
|
}})
|
||||||
|
|||||||
Reference in New Issue
Block a user