06/19 Fix notification mark as read button
This commit is contained in:
@@ -71,15 +71,13 @@
|
||||
</a>
|
||||
{% endif %}
|
||||
{% if not n.is_read %}
|
||||
<form method="post"
|
||||
action="{{ url_for('notifications.mark_read', notif_id=n.id) }}"
|
||||
class="mb-0">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
<button type="submit" class="btn btn-sm btn-outline-secondary"
|
||||
title="Mark as read">
|
||||
<i class="bi bi-check2"></i>
|
||||
</button>
|
||||
</form>
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-secondary mark-read-btn"
|
||||
data-notif-id="{{ n.id }}"
|
||||
data-url="{{ url_for('notifications.mark_read', notif_id=n.id) }}"
|
||||
title="Mark as read">
|
||||
<i class="bi bi-check2"></i>
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@@ -125,3 +123,42 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script>
|
||||
document.querySelectorAll('.mark-read-btn').forEach(function(btn) {
|
||||
btn.addEventListener('click', function() {
|
||||
var url = btn.dataset.url;
|
||||
var csrfToken = {{ csrf_token() | tojson }};
|
||||
btn.disabled = true;
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'X-CSRFToken': csrfToken, 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: 'csrf_token=' + encodeURIComponent(csrfToken),
|
||||
})
|
||||
.then(function(res) { return res.json(); })
|
||||
.then(function(data) {
|
||||
if (data.ok) {
|
||||
var li = btn.closest('li');
|
||||
li.classList.remove('list-group-item-light', 'border-start', 'border-primary', 'border-3');
|
||||
var badge = li.querySelector('.badge.bg-primary');
|
||||
if (badge) badge.remove();
|
||||
btn.remove();
|
||||
var unreadBadge = document.querySelector('.badge.bg-danger');
|
||||
if (unreadBadge) {
|
||||
var count = parseInt(unreadBadge.textContent) - 1;
|
||||
if (count <= 0) {
|
||||
unreadBadge.remove();
|
||||
var markAllForm = document.querySelector('form[action*="mark-all-read"]');
|
||||
if (markAllForm) markAllForm.closest('.col-auto') && markAllForm.remove();
|
||||
} else {
|
||||
unreadBadge.textContent = count + ' unread';
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(function() { btn.disabled = false; });
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user