Add post page
This commit is contained in:
parent
dcb5026a66
commit
c30d1d5531
2 changed files with 18 additions and 22 deletions
|
@ -15,15 +15,14 @@ pub fn App() -> impl IntoView {
|
||||||
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
|
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
|
||||||
<Router>
|
<Router>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="" view=pages::Home />
|
<Route path="" view=pages::Home/>
|
||||||
<Route path="posts" view=|| view! { <pages::PostList folder="posts".to_string() /> } />
|
<Route path="posts" view=pages::PostList/>
|
||||||
<Route path="posts/:slug" view=|| view! {
|
<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"/>
|
<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 src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
||||||
<script>hljs.highlightAll();</script>
|
<script>hljs.highlightAll();</script>
|
||||||
<pages::PostElement folder="posts".to_string() />
|
<pages::PostElement/>
|
||||||
} />
|
}/>
|
||||||
<Route path="/*any" view=|| view! { <h1>"Not Found"</h1> }/>
|
|
||||||
</Routes>
|
</Routes>
|
||||||
</Router>
|
</Router>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
use leptos_router::*;
|
||||||
use crate::app::models::Post;
|
use crate::app::models::Post;
|
||||||
|
|
||||||
#[server]
|
#[server]
|
||||||
pub async fn get_posts(
|
pub async fn get_posts() -> Result<Vec<Post>, ServerFnError> {
|
||||||
folder: String
|
let posts = Post::get_all("posts")
|
||||||
) -> Result<Vec<Post>, ServerFnError> {
|
|
||||||
let posts = Post::get_all(&folder)
|
|
||||||
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
||||||
|
|
||||||
Ok(posts)
|
Ok(posts)
|
||||||
|
@ -13,10 +12,9 @@ pub async fn get_posts(
|
||||||
|
|
||||||
#[server]
|
#[server]
|
||||||
pub async fn get_post(
|
pub async fn get_post(
|
||||||
folder: String,
|
|
||||||
slug: String
|
slug: String
|
||||||
) -> Result<Post, ServerFnError> {
|
) -> Result<Post, ServerFnError> {
|
||||||
let posts = Post::get_all(&folder)
|
let posts = Post::get_all("posts")
|
||||||
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
||||||
|
|
||||||
let post = posts.into_iter().find(|post| post.slug == slug)
|
let post = posts.into_iter().find(|post| post.slug == slug)
|
||||||
|
@ -26,10 +24,8 @@ pub async fn get_post(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn PostList(
|
pub fn PostList() -> impl IntoView {
|
||||||
folder: String
|
let posts = create_resource(|| (), |_| get_posts());
|
||||||
) -> impl IntoView {
|
|
||||||
let posts = create_resource(|| (), |_| get_posts("posts".to_string()));
|
|
||||||
|
|
||||||
let posts_view = move || {
|
let posts_view = move || {
|
||||||
posts.and_then(|posts| {
|
posts.and_then(|posts| {
|
||||||
|
@ -55,10 +51,11 @@ pub fn PostList(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn PostElement(
|
pub fn PostElement() -> impl IntoView {
|
||||||
folder: String,
|
let params = use_params_map();
|
||||||
) -> impl IntoView {
|
let slug = move || params.with(|params| params.get("slug").cloned().unwrap_or_default());
|
||||||
let post = create_resource(|| (), |_| get_post("posts".to_string(), "2023-11-26_testing_layout".to_string()));
|
|
||||||
|
let post = create_resource(|| (), move |_| get_post(slug()));
|
||||||
|
|
||||||
let post_view = move || {
|
let post_view = move || {
|
||||||
post.and_then(|post| {
|
post.and_then(|post| {
|
||||||
|
|
Loading…
Reference in a new issue