From 0436136cdd0d7bdb1c16465a68c4f56bd18d818c Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 18 Feb 2024 19:49:06 +0100 Subject: [PATCH] Add all tags in posts --- src/app/models/mod.rs | 16 ++++++++++++--- src/app/pages/posts.rs | 45 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/app/models/mod.rs b/src/app/models/mod.rs index 9d95d2e..5101687 100644 --- a/src/app/models/mod.rs +++ b/src/app/models/mod.rs @@ -9,6 +9,7 @@ cfg_if::cfg_if! { pub posts: Vec>, pub posts_by_slug: HashMap>, pub posts_by_tag: HashMap>>, + pub tags: Vec, } impl Data { @@ -21,6 +22,8 @@ cfg_if::cfg_if! { .map(Arc::new) .collect::>(); + + let mut tags : Vec = Vec::new(); let mut posts_by_slug : HashMap> = HashMap::new(); let mut posts_by_tag : HashMap>> = HashMap::new(); @@ -28,19 +31,26 @@ cfg_if::cfg_if! { posts_by_slug.insert(post.metadata.slug.clone(), post.clone()); for tag in &post.metadata.tags { - let tag = tag.to_lowercase(); - let posts = posts_by_tag.entry(tag).or_default(); + let tag_lower = tag.to_lowercase(); + + if let None = tags.iter().find(|t| t.to_lowercase().eq(&tag_lower)) { + tags.push(tag.clone()); + } + + let posts = posts_by_tag.entry(tag_lower).or_default(); posts.push(post.clone()); } } log::info!("Loaded {} posts", posts.len()); - log::info!("Found {} tags", posts_by_tag.len()); + log::info!("Found {} tags in global", tags.len()); + log::info!("Found {} tags from posts", posts_by_tag.len()); Ok(Self { posts, posts_by_slug, posts_by_tag, + tags }) } } diff --git a/src/app/pages/posts.rs b/src/app/pages/posts.rs index 0c7ecf5..5d79a09 100644 --- a/src/app/pages/posts.rs +++ b/src/app/pages/posts.rs @@ -32,6 +32,16 @@ pub async fn get_posts( ) } +#[server] +pub async fn get_tags() -> Result, ServerFnError> { + let data : actix_web::web::Data = leptos_actix::extract().await?; + let data = data.into_inner(); + let mut tags = data.tags.clone(); + tags.sort(); + + Ok(tags) +} + #[server] pub async fn get_post( slug: String @@ -117,6 +127,7 @@ pub fn PostList() -> impl IntoView { let query = use_query_map(); let tag = move || query.with(|query| query.get("tag").cloned()); let posts = create_resource(move || tag(), move |_| get_posts(tag())); + let tags = create_resource(|| (), move |_| get_tags()); let posts_view = move || { posts.and_then(|posts| { @@ -126,18 +137,40 @@ pub fn PostList() -> impl IntoView { }) }; + let tags_view = move || { + tags.and_then(|tags| { + tags.iter() + .map(|tag| { + let tag = tag.clone(); + view! { {tag} } + }) + .collect_view() + }) + }; + let filter_view = move || { - tag().map(|tag| Some(view! { -
- Tag sélectionné : {tag} -
- })) + match tag() { + Some(tag) => { + view! { + {tag} + }.into_view() + }, + None => { + view! { +
+ {tags_view} +
+ }.into_view() + } + } }; view! { }>