Skip to content

Commit

Permalink
chore: handle the errors for the title
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Sep 24, 2024
1 parent f829d0c commit 84852f9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crates/web/src/components/publisher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
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;
Expand All @@ -8,12 +11,15 @@ 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(|data: &(String, String)| {
let creation_error: RwSignal<Option<String>> = create_rw_signal(None);

let send_post_action = create_action(move |data: &(String, String)| {
let (title, content) = data;

let title = title.to_owned();

if title.is_empty() {
creation_error.set(Some("Title is required".to_owned()));
()
}

Expand Down Expand Up @@ -43,7 +49,7 @@ pub fn Publisher() -> impl IntoView {
<img src="https://images.unsplash.com/photo-1534528741775-53994a69daeb?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="A beautiful image" />
</figure>
</div>
<div id="publisher-form" class="flex flex-col items-start justify-start w-full space-y-2">
<div id="publisher-form" class="flex flex-col items-start justify-start w-full space-y-5">
<form class="flex flex-col justify-start w-full" on:submit=move |ev| {
ev.prevent_default();
let content = text_content.get();
Expand All @@ -52,6 +58,9 @@ pub fn Publisher() -> impl IntoView {
}>
<div class="w-full h-12">
<TextField class="w-full" name="title" value=text_title placeholder="Title" required=true />
<Show when=move || creation_error.get().is_some() fallback=move || ()>
<p class="text-red-500 mb-3">{creation_error.get().unwrap()}</p>
</Show>
</div>
<div class="w-full h-12">
<TextField class="w-full" name="content" value=text_content placeholder="Content" />
Expand Down

0 comments on commit 84852f9

Please sign in to comment.