Manage posts on dashboard and templates fixes

This commit is contained in:
Alphonse Paix
2025-10-03 19:18:15 +02:00
parent ce8c602ddb
commit af9cbdcafb
9 changed files with 65 additions and 55 deletions

View File

@@ -42,10 +42,10 @@ pub async fn admin_dashboard(
.await .await
.context("Could not fetch subscribers from database.") .context("Could not fetch subscribers from database.")
.map_err(AppError::unexpected_message)?; .map_err(AppError::unexpected_message)?;
let count = get_total_subs(&connection_pool) let subs_count = get_total_subs(&connection_pool)
.await .await
.context("Could not fetch total subscribers count from the database.")?; .context("Could not fetch total subscribers count from the database.")?;
let max_page = get_max_page(count, SUBS_PER_PAGE); let max_page = get_max_page(subs_count, SUBS_PER_PAGE);
let users = get_users(&connection_pool) let users = get_users(&connection_pool)
.await .await
.context("Could not fetch users")?; .context("Could not fetch users")?;
@@ -53,10 +53,10 @@ pub async fn admin_dashboard(
.await .await
.context("Could not fetch posts.")?; .context("Could not fetch posts.")?;
let posts_current_page = 1; let posts_current_page = 1;
let count = get_posts_count(&connection_pool) let posts_count = get_posts_count(&connection_pool)
.await .await
.context("Could not fetch posts count.")?; .context("Could not fetch posts count.")?;
let posts_max_page = get_max_page(count, POSTS_PER_PAGE); let posts_max_page = get_max_page(posts_count, POSTS_PER_PAGE);
let template = DashboardTemplate { let template = DashboardTemplate {
user, user,
idempotency_key_1, idempotency_key_1,
@@ -65,10 +65,12 @@ pub async fn admin_dashboard(
subscribers, subscribers,
current_page, current_page,
max_page, max_page,
count: subs_count,
users, users,
posts, posts,
posts_current_page, posts_current_page,
posts_max_page, posts_max_page,
posts_count,
}; };
Ok(Html(template.render().unwrap()).into_response()) Ok(Html(template.render().unwrap()).into_response())
} }

View File

@@ -136,7 +136,7 @@ pub async fn delete_post(
"We could not find the post in the database." "We could not find the post in the database."
))) )))
} else { } else {
let template = MessageTemplate::success("The subscriber has been deleted.".into()); let template = MessageTemplate::success("The post has been deleted.".into());
Ok(template.render().unwrap().into_response()) Ok(template.render().unwrap().into_response())
} }
} }

View File

@@ -210,11 +210,14 @@ pub async fn load_more(
let posts = get_posts_page(&connection_pool, page) let posts = get_posts_page(&connection_pool, page)
.await .await
.context("Could not fetch posts from database.")?; .context("Could not fetch posts from database.")?;
let count = posts.len(); let count = get_posts_count(&connection_pool)
.await
.context("Could not fetch posts count.")?;
let max_page = get_max_page(count, POSTS_PER_PAGE);
Ok(Html( Ok(Html(
PostListTemplate { PostListTemplate {
posts, posts,
next_page: if count as i64 == POSTS_PER_PAGE { next_page: if page < max_page {
Some(page + 1) Some(page + 1)
} else { } else {
None None

View File

@@ -64,10 +64,12 @@ pub struct DashboardTemplate {
pub subscribers: Vec<SubscriberEntry>, pub subscribers: Vec<SubscriberEntry>,
pub current_page: i64, pub current_page: i64,
pub max_page: i64, pub max_page: i64,
pub count: i64,
pub users: Vec<UserEntry>, pub users: Vec<UserEntry>,
pub posts: Vec<PostEntry>, pub posts: Vec<PostEntry>,
pub posts_current_page: i64, pub posts_current_page: i64,
pub posts_max_page: i64, pub posts_max_page: i64,
pub posts_count: i64,
} }
#[derive(Template)] #[derive(Template)]

View File

@@ -10,16 +10,16 @@
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/> d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg> </svg>
Posts management <span>Posts management ({{ posts_count }})</span>
</h2> </h2>
<p class="text-sm text-gray-600 mt-1">View and manage all published posts.</p> <p class="text-sm text-gray-600 mt-1">View and manage all published posts.</p>
</div> </div>
</div> </div>
</div> </div>
<div id="posts-list" class="py-6 space-y-6"> <div id="posts-list" class="space-y-6">
{% block posts %} {% block posts %}
{% if posts.is_empty() %} {% if posts.is_empty() %}
<div class="bg-gray-50 rounded-lg p-8 border-2 border-dashed border-gray-300 text-center"> <div class="p-8 text-center">
<div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4"> <div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4">
<svg class="w-8 h-8 text-gray-500" <svg class="w-8 h-8 text-gray-500"
fill="none" fill="none"
@@ -29,8 +29,8 @@
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/> d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg> </svg>
</div> </div>
<h3 class="text-lg font-medium text-gray-900 mb-2">No posts yet</h3> <h3 class="text-lg font-medium text-gray-900 mb-2">No data to display</h3>
<p class="text-gray-600">Published posts will appear here.</p> <p class="text-gray-600">The request did not return any data.</p>
</div> </div>
{% else %} {% else %}
<div class="divide-y divide-gray-200"> <div class="divide-y divide-gray-200">
@@ -39,7 +39,7 @@
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
<div class="flex items-center justify-center space-x-2"> <div class="flex items-center justify-center space-x-2 mb-6">
<button hx-get="/admin/posts?page={{ posts_current_page - 1 }}" <button hx-get="/admin/posts?page={{ posts_current_page - 1 }}"
hx-target="#posts-list" hx-target="#posts-list"
hx-swap="innerHTML" hx-swap="innerHTML"

View File

@@ -13,16 +13,16 @@
<path d="M23 21v-2a4 4 0 0 0-3-3.87"/> <path d="M23 21v-2a4 4 0 0 0-3-3.87"/>
<path d="M16 3.13a4 4 0 0 1 0 7.75"/> <path d="M16 3.13a4 4 0 0 1 0 7.75"/>
</svg> </svg>
Subscribers management Subscribers management ({{ count }})
</h2> </h2>
<p class="text-sm text-gray-600 mt-1">View and manage your subscribers.</p> <p class="text-sm text-gray-600 mt-1">View and manage your subscribers.</p>
</div> </div>
</div> </div>
</div> </div>
<div id="subscribers-list" class="py-6 space-y-6"> <div id="subscribers-list" class="space-y-6">
{% block subs %} {% block subs %}
{% if subscribers.is_empty() %} {% if subscribers.is_empty() %}
<div class="bg-gray-50 rounded-lg p-8 border-2 border-dashed border-gray-300 text-center"> <div class="g p-8 text-center">
<div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4"> <div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4">
<svg class="w-8 h-8 text-gray-500" <svg class="w-8 h-8 text-gray-500"
fill="none" fill="none"
@@ -42,7 +42,7 @@
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
<div class="flex items-center justify-center space-x-2"> <div class="flex items-center justify-center space-x-2 mb-6">
<button hx-get="/admin/subscribers?page={{ current_page - 1 }}" <button hx-get="/admin/subscribers?page={{ current_page - 1 }}"
hx-target="#subscribers-list" hx-target="#subscribers-list"
hx-swap="innerHTML" hx-swap="innerHTML"

View File

@@ -4,7 +4,7 @@
<div class="flex-1 min-w-0"> <div class="flex-1 min-w-0">
<div class="flex items-center space-x-2 mb-1"> <div class="flex items-center space-x-2 mb-1">
<a href="/users/{{ user.username }}" <a href="/users/{{ user.username }}"
class="font-medium text-gray-900 group-hover:text-blue-600 transition-colors truncate"> class="font-medium text-gray-900 group-hover:text-blue-600 hover:underline transition-colors truncate">
{{ user.username }} {{ user.username }}
</a> </a>
{% if user.role.to_string() == "admin" %} {% if user.role.to_string() == "admin" %}

View File

@@ -16,9 +16,9 @@
</div> </div>
</div> </div>
</div> </div>
<div id="users-list" class="py-6"> <div id="users-list">
{% if users.is_empty() %} {% if users.is_empty() %}
<div class="bg-gray-50 rounded-lg p-8 border-2 border-dashed border-gray-300 text-center"> <div class="py-6 p-8 text-center">
<div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4"> <div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-4">
<svg class="w-8 h-8 text-gray-500" <svg class="w-8 h-8 text-gray-500"
fill="none" fill="none"

View File

@@ -1,44 +1,47 @@
<div class="bg-white rounded-lg shadow-md border border-gray-200 p-8"> <div class="bg-white rounded-lg shadow-md border border-gray-200">
<h2 class="text-xl font-semibold text-gray-900 mb-4">Activity</h2> <h2 class="text-xl font-semibold text-gray-900 pb-4 pt-8 px-8">Activity</h2>
{% if posts.is_empty() %} {% if posts.is_empty() %}
<div class="text-center py-8 text-gray-500"> <div class="text-center text-gray-500 p-8">
<svg class="w-12 h-12 mx-auto mb-3 text-gray-400" <svg class="w-12 h-12 mx-auto mb-3 text-gray-400"
fill="none" fill="none"
viewBox="0 0 24 24" viewBox="0 0 24 24"
stroke="currentColor"> stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
</svg> d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
<p>No posts yet</p> </svg>
</div> <p>No posts yet</p>
</div>
{% else %} {% else %}
<div class="divide-y divide-gray-200"> <div class="divide-y divide-gray-200 pb-8">
{% for post in posts %} {% for post in posts %}
<a href="/posts/{{ post.post_id }}" <a href="/posts/{{ post.post_id }}"
class="block py-4 hover:bg-gray-50 -mx-8 px-8 transition-colors group"> class="block py-4 hover:bg-gray-50 px-8 transition-colors group">
<div class="flex items-start justify-between"> <div class="flex items-start justify-between">
<div class="flex-1 min-w-0"> <div class="flex-1 min-w-0">
<h3 class="text-base font-medium text-gray-900 group-hover:text-blue-600 transition-colors mb-1">{{ post.title }}</h3> <h3 class="text-base font-medium text-gray-900 group-hover:text-blue-600 transition-colors mb-1">{{
<div class="flex items-center text-sm text-gray-500"> post.title }}</h3>
<svg class="w-4 h-4 mr-1.5" <div class="flex items-center text-sm text-gray-500">
fill="none" <svg class="w-4 h-4 mr-1.5"
viewBox="0 0 24 24"
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
<time datetime="{{ post.published_at }}">
{{ post.formatted_date() }}
</time>
</div>
</div>
<svg class="w-5 h-5 text-gray-400 group-hover:text-blue-600 transition-colors flex-shrink-0 ml-4"
fill="none" fill="none"
viewBox="0 0 24 24" viewBox="0 0 24 24"
stroke="currentColor"> stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" /> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
</svg> </svg>
<time datetime="{{ post.published_at }}">
{{ post.formatted_date() }}
</time>
</div> </div>
</a> </div>
{% endfor %} <svg class="w-5 h-5 text-gray-400 group-hover:text-blue-600 transition-colors flex-shrink-0 ml-4"
</div> fill="none"
viewBox="0 0 24 24"
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
</svg>
</div>
</a>
{% endfor %}
</div>
{% endif %} {% endif %}
</div> </div>