Begin display all posts
This commit is contained in:
parent
224b5d4832
commit
ce305e1cbf
4 changed files with 85 additions and 112 deletions
|
@ -1,18 +1,18 @@
|
|||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct PostMetadata {
|
||||
image_path: Option<String>,
|
||||
title: String,
|
||||
date: String,
|
||||
description: String,
|
||||
project_link: Option<String>,
|
||||
pub image_path: String,
|
||||
pub title: String,
|
||||
pub date: String,
|
||||
pub description: String,
|
||||
pub project_link: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct Post {
|
||||
metadata: PostMetadata,
|
||||
content: String,
|
||||
pub metadata: PostMetadata,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
|
|
|
@ -1,13 +1,44 @@
|
|||
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)
|
||||
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
||||
|
||||
Ok(posts)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn PostList(
|
||||
folder: String
|
||||
) -> impl IntoView {
|
||||
let posts = create_resource(|| (), |_| get_posts("posts".to_string()));
|
||||
|
||||
let posts_view = move || {
|
||||
posts.and_then(|posts| {
|
||||
posts.iter()
|
||||
.map(|post| view! {
|
||||
<li>
|
||||
{post.metadata.title.clone()}
|
||||
<div>
|
||||
{post.content.clone()}
|
||||
</div>
|
||||
</li>
|
||||
})
|
||||
.collect_view()
|
||||
})
|
||||
};
|
||||
|
||||
view! {
|
||||
<main class="m-0 p-0 bg-surface dark:bg-dark_surface text-on_surface dark:text-dark_on_surface">
|
||||
Page List
|
||||
<h1>"My Great Blog"</h1>
|
||||
|
||||
<Suspense fallback=move || view! { <p>"Loading posts..."</p> }>
|
||||
<ul>{posts_view}</ul>
|
||||
</Suspense>
|
||||
</main>
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue