refactor: add status_label/status_class/formatted_date helpers to Post model

This commit is contained in:
xfy 2026-06-04 16:13:49 +08:00
parent c27b2d513e
commit 58e9dbc5b7
3 changed files with 21 additions and 31 deletions

View File

@ -54,6 +54,20 @@ impl Post {
.map(|d| d.format("%Y-%m-%d").to_string())
.unwrap_or_else(|| self.created_at.format("%Y-%m-%d").to_string())
}
pub fn status_label(&self) -> &'static str {
match self.status {
PostStatus::Published => "已发布",
PostStatus::Draft => "草稿",
}
}
pub fn status_class(&self) -> &'static str {
match self.status {
PostStatus::Published => "text-green-600 dark:text-green-400",
PostStatus::Draft => "text-gray-400 dark:text-[#9b9c9d]",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

View File

@ -98,20 +98,9 @@ fn StatCard(value: String, label: String) -> Element {
#[component]
fn RecentPostItem(post: Post) -> Element {
let date_str = post
.published_at
.map(|d| d.format("%Y-%m-%d").to_string())
.unwrap_or_else(|| post.created_at.format("%Y-%m-%d").to_string());
let status_label = if post.status == crate::models::post::PostStatus::Published {
"已发布"
} else {
"草稿"
};
let status_class = if post.status == crate::models::post::PostStatus::Published {
"text-green-600 dark:text-green-400"
} else {
"text-gray-400 dark:text-[#9b9c9d]"
};
let date_str = post.formatted_date();
let status_label = post.status_label();
let status_class = post.status_class();
rsx! {
div { class: "flex justify-between items-center py-3 border-b border-gray-100 dark:border-[#333]",

View File

@ -3,7 +3,7 @@ use dioxus::router::components::Link;
use crate::api::posts::{delete_post, list_posts, CreatePostResponse, PostListResponse};
use crate::hooks::delayed_loading::use_delayed_loading;
use crate::models::post::{Post, PostStatus};
use crate::models::post::Post;
use crate::router::Route;
#[component]
@ -103,22 +103,9 @@ pub fn Posts() -> Element {
#[component]
fn PostRow(post: Post, deleting: bool, on_delete: EventHandler<i32>) -> Element {
let date_str = post
.published_at
.map(|d| d.format("%Y-%m-%d").to_string())
.unwrap_or_else(|| post.created_at.format("%Y-%m-%d").to_string());
let (status_label, status_class) = if post.status == PostStatus::Published {
(
"已发布",
"bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300",
)
} else {
(
"草稿",
"bg-gray-100 dark:bg-[#333] text-gray-600 dark:text-[#9b9c9d]",
)
};
let date_str = post.formatted_date();
let status_label = post.status_label();
let status_class = post.status_class();
rsx! {
tr { class: "border-b border-gray-100 dark:border-[#333] last:border-0 hover:bg-gray-50 dark:hover:bg-[#2a2a2a] transition-colors",