From dcb5026a6696eea530cf92d423c987eab9ccfec5 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 26 Nov 2023 19:18:39 +0100 Subject: [PATCH] Begin add PostPage --- src/app/mod.rs | 5 +++-- src/app/models/post.rs | 3 +++ src/app/pages/mod.rs | 2 +- src/app/pages/posts.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 455932e..d1f89ce 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -16,11 +16,12 @@ pub fn App() -> impl IntoView { - } /> + - + } /> "Not Found" }/> diff --git a/src/app/models/post.rs b/src/app/models/post.rs index ff80570..1a476ec 100644 --- a/src/app/models/post.rs +++ b/src/app/models/post.rs @@ -11,6 +11,7 @@ pub struct PostMetadata { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct Post { + pub slug : String, pub metadata: PostMetadata, pub content: String, } @@ -26,6 +27,7 @@ cfg_if::cfg_if! { .parse_with_struct::(&content)?; let metadata = post_data.data; + let slug = format!("{}_{}", metadata.date, metadata.title.to_lowercase().replace(' ', "_")); let content = post_data.content; use pulldown_cmark::{Parser, Options, html}; @@ -34,6 +36,7 @@ cfg_if::cfg_if! { html::push_html(&mut html_output, parser); Some(Self { + slug, metadata, content: html_output, }) diff --git a/src/app/pages/mod.rs b/src/app/pages/mod.rs index 859cc08..76b9381 100644 --- a/src/app/pages/mod.rs +++ b/src/app/pages/mod.rs @@ -2,4 +2,4 @@ mod home; pub use home::Home; mod posts; -pub use posts::PostList; \ No newline at end of file +pub use posts::{PostList, PostElement}; \ No newline at end of file diff --git a/src/app/pages/posts.rs b/src/app/pages/posts.rs index 47b37f7..0c11753 100644 --- a/src/app/pages/posts.rs +++ b/src/app/pages/posts.rs @@ -11,6 +11,20 @@ pub async fn get_posts( Ok(posts) } +#[server] +pub async fn get_post( + folder: String, + slug: String +) -> Result { + let posts = Post::get_all(&folder) + .map_err(|e| ServerFnError::ServerError(e.to_string()))?; + + let post = posts.into_iter().find(|post| post.slug == slug) + .ok_or(ServerFnError::ServerError("Post not found".to_string()))?; + + Ok(post) +} + #[component] pub fn PostList( folder: String @@ -22,8 +36,7 @@ pub fn PostList( posts.iter() .map(|post| view! {
  • - {post.metadata.title.clone()} -
  • }) .collect_view() @@ -39,4 +52,27 @@ pub fn PostList( } +} + +#[component] +pub fn PostElement( + folder: String, +) -> impl IntoView { + let post = create_resource(|| (), |_| get_post("posts".to_string(), "2023-11-26_testing_layout".to_string())); + + let post_view = move || { + post.and_then(|post| { + view! { +
    + } + }) + }; + + view! { +
    + "Loading posts..."

    }> + {post_view} +
    +
    + } } \ No newline at end of file