From 62814c0bd2c41fb476769e963eb3080ca0e9eff4 Mon Sep 17 00:00:00 2001 From: Michael Liendo Date: Sun, 22 Sep 2024 12:33:46 -0400 Subject: [PATCH] feat(web): create and list posts --- crates/client/src/modules/post/posts/mod.rs | 3 +- crates/web/src/components/feed/mod.rs | 61 +++++++++++++++------ crates/web/src/components/feed/post.rs | 9 +-- crates/web/src/components/publisher/mod.rs | 26 ++++++++- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/crates/client/src/modules/post/posts/mod.rs b/crates/client/src/modules/post/posts/mod.rs index 0757069..517da5f 100644 --- a/crates/client/src/modules/post/posts/mod.rs +++ b/crates/client/src/modules/post/posts/mod.rs @@ -3,7 +3,6 @@ use graphql_client::reqwest::post_graphql; use graphql_client::GraphQLQuery; use posts::{PostsPostsEdges, PostsPostsPageInfo, Variables}; use pxid::Pxid; -use serde::Serialize; use crate::{DateTime, GRAPHQL_PATH}; @@ -11,7 +10,7 @@ use super::PostClient; #[derive(GraphQLQuery)] #[graphql( - response_derives = "Clone,Debug,Deserialize,Serializable", + response_derives = "Clone,Debug,Eq,PartialEq", schema_path = "schema.json", query_path = "src/modules/post/posts/Posts.gql" )] diff --git a/crates/web/src/components/feed/mod.rs b/crates/web/src/components/feed/mod.rs index d4b6bad..6be06ff 100644 --- a/crates/web/src/components/feed/mod.rs +++ b/crates/web/src/components/feed/mod.rs @@ -1,36 +1,61 @@ mod post; -use leptos::{component, create_resource, view, IntoView}; +use leptos::{ + component, create_effect, create_signal, spawn_local, view, For, IntoView, SignalGet, SignalSet, +}; + +use townhall_client::post::posts::posts::PostsPostsEdges; use townhall_client::Client; use self::post::Post; +#[derive(Clone, Debug, PartialEq, Eq)] +enum FeedPosts { + Loading, + Ready(Vec), + Error(String), +} + #[component] pub fn Feed() -> impl IntoView { - let posts = create_resource( - || (), - |_| async move { - let client = Client::new("http://localhost:8080"); + let (posts_getter, posts_setter) = create_signal(FeedPosts::Loading); - let posts = client + create_effect(move |_| { + spawn_local(async move { + match Client::new("http://127.0.0.1:8080") .unwrap() .post - .posts(None, None, Some(10), None) + .posts(None, None, Some(20), None) .await - .unwrap(); - - posts - }, - ); + { + Ok(posts) => posts_setter.set(FeedPosts::Ready(posts.edges.to_owned())), + Err(err) => posts_setter.set(FeedPosts::Error(err.to_string())), + }; + }); + }); view! {
-
    - - - - -
+ { + move || match posts_getter.get() { + FeedPosts::Loading => view! {

Loading...

}.into_view(), + FeedPosts::Ready(posts) => view! { +
    + } + } + /> +
+ }.into_view(), + FeedPosts::Error(err) => view! {

{err}

}.into_view(), + } + }
} } diff --git a/crates/web/src/components/feed/post.rs b/crates/web/src/components/feed/post.rs index 7cb79bd..a2d81bd 100644 --- a/crates/web/src/components/feed/post.rs +++ b/crates/web/src/components/feed/post.rs @@ -1,7 +1,9 @@ use leptos::{component, view, IntoView}; +use townhall_client::post::posts::posts::PostsPostsEdgesNode; + #[component] -pub fn Post() -> impl IntoView { +pub fn Post(post: PostsPostsEdgesNode) -> impl IntoView { view! {
  • @@ -10,15 +12,14 @@ pub fn Post() -> impl IntoView { A beautiful image
    - John Doe + {post.author.username}

    - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec - fermentum nunc. Nullam nec fermentum nunc. Nullam nec fermentum nunc. + {post.title}

    diff --git a/crates/web/src/components/publisher/mod.rs b/crates/web/src/components/publisher/mod.rs index 1ef040e..0174f7d 100644 --- a/crates/web/src/components/publisher/mod.rs +++ b/crates/web/src/components/publisher/mod.rs @@ -1,10 +1,26 @@ -use leptos::{component, create_rw_signal, view, IntoView}; +use leptos::{component, create_action, create_rw_signal, view, IntoView, SignalGet}; +use townhall_client::{post::post_create::post_create::PostCreateInput, Client}; use crate::components::text_field::TextField; #[component] pub fn Publisher() -> impl IntoView { let text_content = create_rw_signal(String::default()); + let send_post_action = create_action(|content: &String| { + let content = content.to_owned(); + + async move { + Client::new("http://127.0.0.1:8080") + .unwrap() + .post + .post_create(PostCreateInput { + title: content, + content: None, + parent_id: None, + }) + .await + } + }); view! {
    @@ -14,12 +30,16 @@ pub fn Publisher() -> impl IntoView {
    -
    +
    - +