Correct new comments on listing. On reply and submission increments the count

remotes/1693045480750635534/spooky-22
Yo Mama 2021-09-27 20:14:22 +02:00
parent 54007e91cb
commit 5b65ea121b
5 changed files with 64 additions and 42 deletions

View File

@ -83,22 +83,25 @@
};
document.getElementById('reportCommentModal').addEventListener('hidden.bs.modal', function () {
document.addEventListener("DOMContentLoaded", function() {
var button = document.getElementById("reportCommentButton");
document.getElementById('reportCommentModal').addEventListener('hidden.bs.modal', function () {
var beforeModal = document.getElementById("reportCommentFormBefore");
var afterModal = document.getElementById("reportCommentFormAfter");
const button = document.getElementById("reportCommentButton");
button.innerHTML='Report comment';
button.disabled= false;
afterModal.classList.add('d-none');
const beforeModal = document.getElementById("reportCommentFormBefore");
const afterModal = document.getElementById("reportCommentFormAfter");
if ( beforeModal.classList.contains('d-none') ) {
beforeModal.classList.remove('d-none');
}
button.innerHTML = 'Report comment';
button.disabled = false;
afterModal.classList.add('d-none');
});
if (beforeModal.classList.contains('d-none')) {
beforeModal.classList.remove('d-none');
}
});
});
//Commenting form
@ -147,7 +150,7 @@
};
post_comment=function(fullname){
post_comment=function(fullname, postId){
var form = new FormData();
@ -168,6 +171,8 @@
myToast.hide();
var myToast = new bootstrap.Toast(document.getElementById('toast-post-success'));
myToast.show();
incrementCommentCount(postId)
}
else {
var commentError = document.getElementById("comment-error-text");
@ -758,10 +763,10 @@
<input id="file-upload-reply-{{c.fullname}}" type="file" name="file" accept="image/*" onchange="document.getElementById('filename-show-reply-{{c.id}}').innerHTML='image';" hidden>
</label>
<a href="javascript:void(0)" onclick="document.getElementById('reply-to-{{c.id}}').classList.add('d-none')" class="d-none d-md-block btn btn-link text-muted ml-auto cancel-form">Cancel</a>
<a id="save-reply-to-{{c.fullname}}" class="d-none d-md-block btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}');" href="javascript:void(0)">Comment</a>
<a id="save-reply-to-{{c.fullname}}" class="d-none d-md-block btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}');" href="javascript:void(0)">Comment</a>
</div>
<a href="javascript:void(0)" onclick="document.getElementById('reply-to-{{c.id}}').classList.add('d-none')" class="d-block d-md-none btn btn-link text-muted ml-auto cancel-form">Cancel</a>
<a id="save-reply-to-{{c.fullname}}" class="d-block d-md-none btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}');" href="javascript:void(0)">Comment</a>
<a id="save-reply-to-{{c.fullname}}" class="d-block d-md-none btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}');" href="javascript:void(0)">Comment</a>
</form>
</div>

View File

@ -219,11 +219,15 @@
function showNewCommentCounts(postId, lastTotalComs) {
const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {}
const lastCount = comments["" + postId]
console.log(postId)
const lastCount = comments[postId]
console.log(lastCount)
if (lastCount) {
const newComments = lastTotalComs - lastCount.p
const newComments = lastTotalComs - lastCount.c
console.log('new:', newComments)
if (newComments > 0) {
document.querySelectorAll(`#post-${postId} .new-comments`).forEach(elem => {
console.log(elem)
elem.textContent = ` (+${newComments})`
elem.classList.remove("d-none")
})
@ -231,6 +235,10 @@
}
}
function incrementCommentCount(postId) {
saveCommentsCount(postId)
}
/**
* Saves the comment count to the localStorage
*
@ -240,14 +248,13 @@
function saveCommentsCount(postId, lastTotalComs = null) {
const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {}
lastTotalComs = lastTotalComs || (comments["" + postId] || { c: 0 }).c + 1
const newTotal = lastTotalComs || ((comments[postId] || { c: 0 }).c + 1)
comments["" + postId] = { p: lastTotalComs, t: Date.now() }
console.log(newTotal)
comments[postId] = { c: newTotal, t: Date.now() }
window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments))
//TODO: increment count on submit response and new comment
//TODO: restrict on display unread comments flag
}
@ -271,10 +278,8 @@
delete comments[key]
}
}
window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments))
}
window.localStorage.setItem(LAST_CACHE_CLEAN_ID, JSON.stringify(now))
}

View File

@ -31,23 +31,25 @@
}
};
document.getElementById('reportPostModal').addEventListener('hidden.bs.modal', function () {
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('reportPostModal').addEventListener('hidden.bs.modal', function () {
var button = document.getElementById("reportPostButton");
const button = document.getElementById("reportPostButton");
var beforeModal = document.getElementById("reportPostFormBefore");
var afterModal = document.getElementById("reportPostFormAfter");
const beforeModal = document.getElementById("reportPostFormBefore");
const afterModal = document.getElementById("reportPostFormAfter");
button.innerHTML='Report post';
button.disabled= false;
button.innerHTML = 'Report post';
button.disabled = false;
afterModal.classList.add('d-none');
afterModal.classList.add('d-none');
if ( beforeModal.classList.contains('d-none') ) {
beforeModal.classList.remove('d-none');
}
if (beforeModal.classList.contains('d-none')) {
beforeModal.classList.remove('d-none');
}
});
});
})
</script>
<!-- Report Post Modal -->

View File

@ -37,7 +37,7 @@
{% if v %}
<script>
post_comment=function(fullname){
post_comment=function(fullname, postId){
var form = new FormData();
@ -57,6 +57,8 @@
myToast.hide();
var myToast = new bootstrap.Toast(document.getElementById('toast-post-success'));
myToast.show();
incrementCommentCount(postId)
}
else {
var commentError = document.getElementById("comment-error-text");
@ -687,7 +689,7 @@
<input type="checkbox" class="custom-control-input" id="nsfwCheck-{{p.fullname}}" name="over_18" form="reply-to-{{p.fullname}}">
<label class="custom-control-label" for="nsfwCheck">+18</label>
</div>
--> <a id="save-reply-to-{{p.fullname}}" href="javascript:void(0)" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto" onclick="post_comment('{{p.fullname}}')">Comment</a>
--> <a id="save-reply-to-{{p.fullname}}" href="javascript:void(0)" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto" onclick="post_comment('{{p.fullname}}', '{{p.id}}')">Comment</a>
</div>
</form>
</div>
@ -745,8 +747,8 @@
document.getElementById('edited_timestamp').title = dateEdited.toString();
{% endif %}
showNewCommentCounts({{p.id}}, {{p.comment_count}})
saveCommentsCount({{p.id}}, {{p.comment_count}})
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
saveCommentsCount('{{p.id}}', {{p.comment_count}})
})()
</script>

View File

@ -141,8 +141,12 @@
{% if p.realbody(v) %}
<li class="list-inline-item"><a href="javascript:void(0)" class="text-expand" data-bs-id="{{p.id}}"><i class="fas fa-expand-alt mr-0 text-expand-icon-{{p.id}}"></i></a></li>
{% endif %}
<li class="list-inline-item"><a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item">
<a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
<li class="list-inline-item"><a href="/votes?link={{p.fullname}}"><i class="fas fa-arrows-v"></i>Votes</a></li>
{% if v and v.id!=p.author_id %}
@ -229,8 +233,12 @@
<div class="card-footer d-block d-md-none mt-2">
<div class="post-actions">
<ul class="list-inline text-right d-flex">
<li class="list-inline-item mr-auto"><a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item mr-auto">
<a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
<li class="list-inline-item"><a href="javascript:void(0);" role="button" class="copy-link" data-clipboard-text="{% if 'rdrama' in request.host %}https://taytay.life{{p.permalink}}{% else %}{{p.permalink | full_link}}{% endif %}"><i class="fas fa-link"></i>Copy link</a></li>
{% if p.realbody(v) and request.path != "/changelog"%}
@ -400,7 +408,7 @@
document.getElementById('edited_timestamp-{{p.id}}').title = dateEdited.toString();
{% endif %}
showNewCommentCounts({{p.id}}, {{p.comment_count}})
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
})()
</script>