Add pagination for notifications

And correctly close an <a> tag
This commit is contained in:
Bat 2018-07-25 15:33:54 +02:00
parent 18125ab398
commit 4b0aba62f3
4 changed files with 25 additions and 5 deletions

View File

@ -35,4 +35,13 @@ impl Notification {
.load::<Notification>(conn) .load::<Notification>(conn)
.expect("Couldn't load user notifications") .expect("Couldn't load user notifications")
} }
pub fn page_for_user(conn: &PgConnection, user: &User, (min, max): (i32, i32)) -> Vec<Notification> {
notifications::table.filter(notifications::user_id.eq(user.id))
.order_by(notifications::creation_date.desc())
.offset(min.into())
.limit((max - min).into())
.load::<Notification>(conn)
.expect("Couldn't load user notifications page")
}
} }

View File

@ -54,6 +54,7 @@ fn main() {
routes::likes::create, routes::likes::create,
routes::likes::create_auth, routes::likes::create_auth,
routes::notifications::paginated_notifications,
routes::notifications::notifications, routes::notifications::notifications,
routes::notifications::notifications_auth, routes::notifications::notifications_auth,

View File

@ -3,13 +3,21 @@ use rocket_contrib::Template;
use plume_common::utils; use plume_common::utils;
use plume_models::{db_conn::DbConn, notifications::Notification, users::User}; use plume_models::{db_conn::DbConn, notifications::Notification, users::User};
use routes::Page;
#[get("/notifications?<page>")]
fn paginated_notifications(conn: DbConn, user: User, page: Page) -> Template {
Template::render("notifications/index", json!({
"account": user,
"notifications": Notification::page_for_user(&*conn, &user, page.limits()),
"page": page.page,
"n_pages": Page::total(Notification::find_for_user(&*conn, &user).len() as i32)
}))
}
#[get("/notifications")] #[get("/notifications")]
fn notifications(conn: DbConn, user: User) -> Template { fn notifications(conn: DbConn, user: User) -> Template {
Template::render("notifications/index", json!({ paginated_notifications(conn, user, Page::first())
"account": user,
"notifications": Notification::find_for_user(&*conn, &user)
}))
} }
#[get("/notifications", rank = 2)] #[get("/notifications", rank = 2)]

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ "Notifications" | _ }} {{ "Notifications" | _ }}
@ -9,11 +10,12 @@
<div class="list"> <div class="list">
{% for notification in notifications %} {% for notification in notifications %}
<div class="card"> <div class="card">
<h3><a href="{% if notification.link %}{{ notification.link }}/{% else %}#{% endif %}">{{ notification.title | _(data=notification.data) }}</h3> <h3><a href="{% if notification.link %}{{ notification.link }}/{% else %}#{% endif %}">{{ notification.title | _(data=notification.data) }}</a></h3>
{% if notification.content %} {% if notification.content %}
<p>{{ notification.content }}</p> <p>{{ notification.content }}</p>
{% endif %} {% endif %}
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
{{ macros::paginate(page=page, total=n_pages) }}
{% endblock content %} {% endblock content %}