Skip to content

Commit 62bb128

Browse files
feat(web): title content posting (#228)
1 parent 11f3761 commit 62bb128

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

crates/web/src/components/publisher/mod.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1-
use leptos::{component, create_action, create_rw_signal, view, IntoView, SignalGet};
1+
use leptos::{
2+
component, create_action, create_rw_signal, view, IntoView, RwSignal, Show, SignalGet,
3+
SignalSet,
4+
};
25
use townhall_client::{post::post_create::post_create::PostCreateInput, Client};
36

47
use crate::components::text_field::TextField;
58

69
#[component]
710
pub fn Publisher() -> impl IntoView {
11+
let text_title = create_rw_signal(String::default());
812
let text_content = create_rw_signal(String::default());
9-
let send_post_action = create_action(|content: &String| {
10-
let content = content.to_owned();
13+
14+
let creation_error: RwSignal<Option<String>> = create_rw_signal(None);
15+
16+
let send_post_action = create_action(move |data: &(String, String)| {
17+
let (title, content) = data.clone();
1118

1219
async move {
13-
Client::new("http://127.0.0.1:8080")
20+
let title = title.trim().to_owned();
21+
22+
if title.is_empty() {
23+
creation_error.set(Some("Title is required".to_owned()));
24+
return;
25+
}
26+
27+
let content = if content.is_empty() {
28+
None
29+
} else {
30+
Some(content.trim().to_owned())
31+
};
32+
33+
let result = Client::new("http://127.0.0.1:8080")
1434
.unwrap()
1535
.post
1636
.post_create(PostCreateInput {
17-
title: content,
18-
content: None,
37+
title,
38+
content,
1939
parent_id: None,
2040
})
21-
.await
41+
.await;
42+
43+
if let Err(err) = result {
44+
creation_error.set(Some(err.to_string()));
45+
}
2246
}
2347
});
2448

@@ -29,18 +53,26 @@ pub fn Publisher() -> impl IntoView {
2953
<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" />
3054
</figure>
3155
</div>
32-
<div id="publisher-form" class="flex flex-col items-start justify-start w-full">
56+
<div id="publisher-form" class="flex flex-col items-start justify-start w-full space-y-5">
3357
<form class="flex flex-col justify-start w-full" on:submit=move |ev| {
3458
ev.prevent_default();
3559
let content = text_content.get();
36-
send_post_action.dispatch(content);
60+
let title = text_title.get();
61+
send_post_action.dispatch((title, content));
3762
}>
3863
<div class="w-full h-12">
39-
<TextField class="w-full" name="content" value=text_content />
64+
<TextField class="w-full" name="title" value=text_title placeholder="Title" required=true />
65+
66+
</div>
67+
<div class="w-full h-12">
68+
<TextField class="w-full" name="content" value=text_content placeholder="Content" />
4069
</div>
4170
<div class="flex justify-end items-center">
4271
<button type="submit">Post</button>
4372
</div>
73+
<Show when=move || creation_error.get().is_some() fallback=move || ()>
74+
<p class="text-red-500 mb-3">{creation_error.get().unwrap()}</p>
75+
</Show>
4476
</form>
4577
</div>
4678
</div>

crates/web/src/components/text_field.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn TextField(
4040
#[prop(optional, into)] class: TextProp,
4141
#[prop(optional, into)] variant: MaybeProp<TextFieldVariant>,
4242
#[prop(optional, into)] r#type: TextFieldType,
43+
#[prop(optional, into)] required: MaybeProp<bool>,
4344
#[prop(optional, into)] disabled: MaybeProp<bool>,
4445
#[prop(optional, into)] full_width: MaybeProp<bool>,
4546
) -> impl IntoView {
@@ -103,6 +104,7 @@ pub fn TextField(
103104
placeholder=placeholder
104105
class=class_names
105106
disabled=disabled
107+
required=required
106108
on:change=handle_change
107109
on:input=handle_input
108110
/>

0 commit comments

Comments
 (0)