diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index 7767c5a..ac690c3 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -35,6 +35,7 @@ ssr = [ "dep:leptos_axum", "dep:serde_json", "dep:axum", + "reqwest", ] reqwest = ["dep:reqwest"] axum = ["dep:axum"] diff --git a/crates/app/src/error_template.rs b/crates/app/src/error_template.rs index df367a9..fa018a6 100644 --- a/crates/app/src/error_template.rs +++ b/crates/app/src/error_template.rs @@ -25,7 +25,7 @@ impl AppError { #[component] pub fn ErrorTemplate( #[prop(optional)] outside_errors: Option, - #[prop(optional)] errors: Option>, + #[prop(optional, into)] errors: Option>, ) -> impl IntoView { let errors = match outside_errors { Some(e) => RwSignal::new(e), diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index 6f317fc..71c8b4b 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -1,9 +1,10 @@ use crate::error_template::{AppError, ErrorTemplate}; -use leptos::{ev::SubmitEvent, html, prelude::*}; +use leptos::{either::Either, ev::SubmitEvent, html, prelude::*}; use leptos_meta::*; use leptos_router::{components::*, StaticSegment}; use message::Message; +use serde::{Deserialize, Serialize}; pub mod error_template; @@ -70,7 +71,47 @@ fn smooth_scroll_to_bottom() { #[component] pub fn HomePage() -> impl IntoView { - let (messages, set_messages) = signal( + let messages = Resource::new(|| (), move |_| get_messages()); + + let existing_messages = move || { + Suspend::new(async move { + messages.await.map(|messages| { + if messages.messages.is_empty() { + Either::Left(view! {

"No messages sent yet"

}) + } else { + Either::Right( + messages + .messages + .iter() + .map(move |message| { + view! { +
+
{message.content.clone()}
+
+ } + }) + .collect::>(), + ) + } + }) + }) + }; + + let (_, set_messages) = signal( message::get_messages() .into_iter() .enumerate() @@ -83,21 +124,21 @@ pub fn HomePage() -> impl IntoView { // stop the page from reloading! ev.prevent_default(); - let messages_len = messages.get().len(); - let mut messages = set_messages.write(); - messages.push(( - messages_len, - ArcRwSignal::new(Message { - role: "user".into(), - content: input.get().into(), - }), - )); + // let messages_len = messages.get().len(); + // let mut messages = set_messages.write(); + // messages.push(( + // messages_len, + // ArcRwSignal::new(Message { + // role: "user".into(), + // content: input.get().into(), + // }), + // )); - set_input.set("".into()); + // set_input.set("".into()); - request_animation_frame(move || { - smooth_scroll_to_bottom(); - }); + // request_animation_frame(move || { + // smooth_scroll_to_bottom(); + // }); }; view! { @@ -113,38 +154,21 @@ pub fn HomePage() -> impl IntoView {
- -
{message.content.clone()}
+ +
+ "Loading..."
- } +
} - /> -
-
- "Loading..." -
-
+ }> + } + }>{existing_messages} + +
@@ -171,3 +195,21 @@ pub fn HomePage() -> impl IntoView { } } + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct Messages { + pub messages: Vec, +} + +#[server] +pub async fn get_messages() -> Result { + let messages: Messages = + reqwest::get("https://lebusiness-service.prod.kjuulh.app/api/messages") + .await + .map_err(|e| ServerFnError::new(e.to_string()))? + .json() + .await + .map_err(|e| ServerFnError::new(e.to_string()))?; + + Ok(messages) +} diff --git a/crates/app/src/message.rs b/crates/app/src/message.rs index ebdf5fe..16683e6 100644 --- a/crates/app/src/message.rs +++ b/crates/app/src/message.rs @@ -1,3 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Message { pub role: String, pub content: String,