From 62bb128866af129e9f72ac94a1acd8fbf0dcb276 Mon Sep 17 00:00:00 2001 From: Michael Liendo Date: Thu, 28 Nov 2024 23:29:16 -0400 Subject: [PATCH] feat(web): title content posting (#228) --- crates/web/src/components/publisher/mod.rs | 52 +++++++++++++++++----- crates/web/src/components/text_field.rs | 2 + 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/crates/web/src/components/publisher/mod.rs b/crates/web/src/components/publisher/mod.rs index 0174f7d..228259e 100644 --- a/crates/web/src/components/publisher/mod.rs +++ b/crates/web/src/components/publisher/mod.rs @@ -1,24 +1,48 @@ -use leptos::{component, create_action, create_rw_signal, view, IntoView, SignalGet}; +use leptos::{ + component, create_action, create_rw_signal, view, IntoView, RwSignal, Show, SignalGet, + SignalSet, +}; use townhall_client::{post::post_create::post_create::PostCreateInput, Client}; use crate::components::text_field::TextField; #[component] pub fn Publisher() -> impl IntoView { + let text_title = create_rw_signal(String::default()); let text_content = create_rw_signal(String::default()); - let send_post_action = create_action(|content: &String| { - let content = content.to_owned(); + + let creation_error: RwSignal> = create_rw_signal(None); + + let send_post_action = create_action(move |data: &(String, String)| { + let (title, content) = data.clone(); async move { - Client::new("http://127.0.0.1:8080") + let title = title.trim().to_owned(); + + if title.is_empty() { + creation_error.set(Some("Title is required".to_owned())); + return; + } + + let content = if content.is_empty() { + None + } else { + Some(content.trim().to_owned()) + }; + + let result = Client::new("http://127.0.0.1:8080") .unwrap() .post .post_create(PostCreateInput { - title: content, - content: None, + title, + content, parent_id: None, }) - .await + .await; + + if let Err(err) = result { + creation_error.set(Some(err.to_string())); + } } }); @@ -29,18 +53,26 @@ pub fn Publisher() -> impl IntoView { A beautiful image -
+
- + + +
+
+
+ +

{creation_error.get().unwrap()}

+
diff --git a/crates/web/src/components/text_field.rs b/crates/web/src/components/text_field.rs index a86ee27..86c2f10 100644 --- a/crates/web/src/components/text_field.rs +++ b/crates/web/src/components/text_field.rs @@ -40,6 +40,7 @@ pub fn TextField( #[prop(optional, into)] class: TextProp, #[prop(optional, into)] variant: MaybeProp, #[prop(optional, into)] r#type: TextFieldType, + #[prop(optional, into)] required: MaybeProp, #[prop(optional, into)] disabled: MaybeProp, #[prop(optional, into)] full_width: MaybeProp, ) -> impl IntoView { @@ -103,6 +104,7 @@ pub fn TextField( placeholder=placeholder class=class_names disabled=disabled + required=required on:change=handle_change on:input=handle_input />