Skip to content

Commit

Permalink
Add messages.html
Browse files Browse the repository at this point in the history
and refactoring base.html, user.html
  • Loading branch information
R2 committed Feb 15, 2024
1 parent 213ef0b commit 1b6cea7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
<a class="nav-link" aria-current="page" href="{{ url_for('auth.login') }}">{{ _('Login') }}</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" aria-current="page" href="{{ url_for('main.messages') }}">{{ _('Messages') }}
{% set unread_message_count = current_user.unread_message_count() %}
<span id="message_count" class="badge text-bg-danger"
style="visibility: {% if unread_message_count %}visible
{% else %}hidden{% endif %};">
{{ unread_message_count }}
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="{{ url_for('main.user', username=current_user.username) }}">{{ _('Profile') }}</a>
</li>
Expand Down Expand Up @@ -117,6 +127,28 @@
}
}
document.addEventListener('DOMContentLoaded', initialize_popovers);

function set_message_count(n) {
const count = document.getElementById('message_count');
count.innerText = n;
count.style.visibility = n ? 'visible' : 'hidden';
}

{% if current_user.is_authenticated %}
function initialize_notifications() {
let since = 0;
setInterval(async function() {
const response = await fetch('{{ url_for('main.notifications') }}?since=' + since);
const notifications = await response.json();
for (let i = 0; i < notifications.length; i++) {
if (notifications[i].name == 'unread_message_count')
set_message_count(notifications[i].data);
since = notifications[i].timestamp;
}
}, 10000);
}
document.addEventListener('DOMContentLoaded', initialize_notifications);
{% endif %}
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions app/templates/messages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ _('Messages') }}</h1>
{% for post in messages %}
{% include '_post.html' %}
{% endfor %}
<nav aria-label="Post navigation">
<ul class="pagination">
<li class="page-item{% if not prev_url %} disabled{% endif %}">
<a class="page-link" href="#">
<span aria-hidden="true">&larr;</span> {{ _('Newer messages') }}
</a>
</li>
<li class="page-item{% if not next_url %} disabled{% endif %}">
<a class="page-link" href="#">
{{ _('Older messages') }} <span aria-hidden="true">&rarr;</span>
</a>
</li>
</ul>
</nav>
{% endblock %}
3 changes: 3 additions & 0 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h1>{{ _('User') }}: {{ user.username }}</h1>
</form>
</p>
{% endif %}
{% if user != current_user %}
<p><a href="{{ url_for('main.send_message', recipient=user.username) }}">{{ _('Send private message') }}</a></p>
{% endif %}
</td>
</tr>
</table>
Expand Down

0 comments on commit 1b6cea7

Please sign in to comment.