2021-07-11 18:49:52 +00:00
|
|
|
//! Basic example that renders a simple VNode to the browser.
|
|
|
|
|
|
|
|
use dioxus::events::on::MouseEvent;
|
|
|
|
use dioxus_core as dioxus;
|
|
|
|
use dioxus_core::prelude::*;
|
|
|
|
use dioxus_hooks::*;
|
|
|
|
use dioxus_html as dioxus_elements;
|
|
|
|
// use wasm_timer;
|
|
|
|
|
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
use std::{pin::Pin, time::Duration};
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Setup logging
|
|
|
|
console_error_panic_hook::set_once();
|
2021-07-18 07:54:42 +00:00
|
|
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
|
2021-07-11 18:49:52 +00:00
|
|
|
|
2021-07-18 07:54:42 +00:00
|
|
|
dioxus_web::launch(App, |c| c)
|
2021-07-11 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 06:30:41 +00:00
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
struct DogApi {
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random/";
|
|
|
|
|
2021-07-11 18:49:52 +00:00
|
|
|
static App: FC<()> = |cx| {
|
2021-07-14 06:04:19 +00:00
|
|
|
let state = use_state(cx, || 0);
|
|
|
|
|
2021-07-15 04:40:37 +00:00
|
|
|
let dog_node = cx.use_suspense(
|
|
|
|
|| surf::get(ENDPOINT).recv_json::<DogApi>(),
|
|
|
|
|cx, res| match res {
|
|
|
|
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
|
2021-07-18 07:54:42 +00:00
|
|
|
Err(_err) => rsx!(in cx, div { "No doggos for you :(" }),
|
2021-07-15 04:40:37 +00:00
|
|
|
},
|
|
|
|
);
|
2021-07-11 18:49:52 +00:00
|
|
|
|
|
|
|
cx.render(rsx! {
|
2021-07-18 07:54:42 +00:00
|
|
|
div { style: { align_items: "center" }
|
2021-07-11 18:49:52 +00:00
|
|
|
section { class: "py-12 px-4 text-center"
|
|
|
|
div { class: "w-full max-w-2xl mx-auto"
|
|
|
|
span { class: "text-sm font-semibold"
|
|
|
|
"count: {state}"
|
|
|
|
}
|
2021-07-15 03:18:02 +00:00
|
|
|
br {}
|
2021-07-11 18:49:52 +00:00
|
|
|
div {
|
|
|
|
button {
|
|
|
|
onclick: move |_| state.set(state + 1)
|
|
|
|
"incr"
|
|
|
|
}
|
|
|
|
br {}
|
|
|
|
button {
|
|
|
|
onclick: move |_| state.set(state - 1)
|
|
|
|
"decr"
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 06:30:41 +00:00
|
|
|
div {
|
|
|
|
h1{"doggo!"}
|
|
|
|
{dog_node}
|
|
|
|
}
|
2021-07-11 18:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|