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
+ } ;
2
5
use townhall_client:: { post:: post_create:: post_create:: PostCreateInput , Client } ;
3
6
4
7
use crate :: components:: text_field:: TextField ;
5
8
6
9
#[ component]
7
10
pub fn Publisher ( ) -> impl IntoView {
11
+ let text_title = create_rw_signal ( String :: default ( ) ) ;
8
12
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 ( ) ;
11
18
12
19
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" )
14
34
. unwrap ( )
15
35
. post
16
36
. post_create ( PostCreateInput {
17
- title : content ,
18
- content : None ,
37
+ title,
38
+ content,
19
39
parent_id : None ,
20
40
} )
21
- . await
41
+ . await ;
42
+
43
+ if let Err ( err) = result {
44
+ creation_error. set ( Some ( err. to_string ( ) ) ) ;
45
+ }
22
46
}
23
47
} ) ;
24
48
@@ -29,18 +53,26 @@ pub fn Publisher() -> impl IntoView {
29
53
<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" />
30
54
</figure>
31
55
</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 " >
33
57
<form class="flex flex-col justify-start w-full" on: submit=move |ev| {
34
58
ev. prevent_default( ) ;
35
59
let content = text_content. get( ) ;
36
- send_post_action. dispatch( content) ;
60
+ let title = text_title. get( ) ;
61
+ send_post_action. dispatch( ( title, content) ) ;
37
62
} >
38
63
<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" />
40
69
</div>
41
70
<div class="flex justify-end items-center" >
42
71
<button type ="submit" >Post </button>
43
72
</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 >
44
76
</form>
45
77
</div>
46
78
</div>
0 commit comments