Begin add PostPage

This commit is contained in:
Florian RICHER 2023-11-26 19:18:39 +01:00
parent 6ef76ea48d
commit dcb5026a66
4 changed files with 45 additions and 5 deletions

View file

@ -2,4 +2,4 @@ mod home;
pub use home::Home;
mod posts;
pub use posts::PostList;
pub use posts::{PostList, PostElement};

View file

@ -11,6 +11,20 @@ pub async fn get_posts(
Ok(posts)
}
#[server]
pub async fn get_post(
folder: String,
slug: String
) -> Result<Post, ServerFnError> {
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! {
<li>
{post.metadata.title.clone()}
<div inner_html=post.content.clone() />
<a href={format!("/posts/{}", post.slug.clone())}>{post.metadata.title.clone()}</a>
</li>
})
.collect_view()
@ -39,4 +52,27 @@ pub fn PostList(
</Suspense>
</main>
}
}
#[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! {
<div inner_html={post.content.clone()}></div>
}
})
};
view! {
<main class="m-0 p-0 bg-surface dark:bg-dark_surface text-on_surface dark:text-dark_on_surface">
<Suspense fallback=move || view! { <p>"Loading posts..."</p> }>
{post_view}
</Suspense>
</main>
}
}