Add post page

This commit is contained in:
Florian RICHER 2023-11-26 19:42:14 +01:00
parent dcb5026a66
commit c30d1d5531
2 changed files with 18 additions and 22 deletions

View file

@ -15,15 +15,14 @@ pub fn App() -> impl IntoView {
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Router>
<Routes>
<Route path="" view=pages::Home />
<Route path="posts" view=|| view! { <pages::PostList folder="posts".to_string() /> } />
<Route path="" view=pages::Home/>
<Route path="posts" view=pages::PostList/>
<Route path="posts/:slug" view=|| view! {
<Link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<pages::PostElement folder="posts".to_string() />
} />
<Route path="/*any" view=|| view! { <h1>"Not Found"</h1> }/>
<Link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<pages::PostElement/>
}/>
</Routes>
</Router>
}

View file

@ -1,11 +1,10 @@
use leptos::*;
use leptos_router::*;
use crate::app::models::Post;
#[server]
pub async fn get_posts(
folder: String
) -> Result<Vec<Post>, ServerFnError> {
let posts = Post::get_all(&folder)
pub async fn get_posts() -> Result<Vec<Post>, ServerFnError> {
let posts = Post::get_all("posts")
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
Ok(posts)
@ -13,10 +12,9 @@ pub async fn get_posts(
#[server]
pub async fn get_post(
folder: String,
slug: String
) -> Result<Post, ServerFnError> {
let posts = Post::get_all(&folder)
let posts = Post::get_all("posts")
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
let post = posts.into_iter().find(|post| post.slug == slug)
@ -26,10 +24,8 @@ pub async fn get_post(
}
#[component]
pub fn PostList(
folder: String
) -> impl IntoView {
let posts = create_resource(|| (), |_| get_posts("posts".to_string()));
pub fn PostList() -> impl IntoView {
let posts = create_resource(|| (), |_| get_posts());
let posts_view = move || {
posts.and_then(|posts| {
@ -55,10 +51,11 @@ 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()));
pub fn PostElement() -> impl IntoView {
let params = use_params_map();
let slug = move || params.with(|params| params.get("slug").cloned().unwrap_or_default());
let post = create_resource(|| (), move |_| get_post(slug()));
let post_view = move || {
post.and_then(|post| {