Created
September 19, 2024 02:40
-
-
Save hYdos/2a5a3e8c145fcc2e907c642384333870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(non_snake_case)] | |
use dioxus::events::FormData; | |
use dioxus::prelude::*; | |
use dioxus_logger::tracing; | |
#[derive(Clone, Routable, Debug, PartialEq, serde::Serialize, serde::Deserialize)] | |
enum Route { | |
#[route("/")] | |
Home {}, | |
#[route("/blog/:id")] | |
Blog { id: i32 }, | |
} | |
fn main() { | |
dioxus_logger::init(tracing::Level::INFO).expect("failed to init logger"); | |
tracing::info!("starting app"); | |
launch(App); | |
} | |
fn App() -> Element { | |
rsx! { | |
Router::<Route> {} | |
} | |
} | |
#[component] | |
fn Blog(id: i32) -> Element { | |
let mut filenames: Signal<Vec<String>> = use_signal(Vec::new); | |
rsx! { | |
input { | |
// tell the input to pick a file | |
r#type: "file", | |
// list the accepted extensions | |
accept: ".txt", | |
// pick multiple files | |
multiple: true, | |
onchange: move |evt: Event<FormData>| { | |
if let Some(file_engine) = evt.files() { | |
let files = file_engine.files(); | |
for file_name in files { | |
filenames.write().push(file_name); | |
} | |
} | |
} | |
} | |
h1 {"Blog post {id}"} | |
} | |
} | |
#[component] | |
fn Home() -> Element { | |
let mut count = use_signal(|| 0); | |
let mut text = use_signal(|| String::from("...")); | |
rsx! { | |
Link { | |
to: Route::Blog { | |
id: count() | |
}, | |
"Go to blog" | |
} | |
div { | |
h1 { "High-Five counter: {count}" } | |
button { onclick: move |_| count += 1, "Up high!" } | |
button { onclick: move |_| count -= 1, "Down low!" } | |
button { | |
onclick: move |_| async move { | |
if let Ok(data) = get_server_data().await { | |
tracing::info!("Client received: {}", data); | |
text.set(data.clone()); | |
post_server_data(data).await.unwrap(); | |
} | |
}, | |
"Get Server Data" | |
} | |
p { "Server data: {text}"} | |
} | |
} | |
} | |
#[server(PostServerData)] | |
async fn post_server_data(data: String) -> Result<(), ServerFnError> { | |
tracing::info!("Server received: {}", data); | |
Ok(()) | |
} | |
#[server(GetServerData)] | |
async fn get_server_data() -> Result<String, ServerFnError> { | |
Ok("Hello from the server!".to_string()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment