dioxus/examples/dog_app.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

2022-01-03 06:12:39 +00:00
use dioxus::prelude::*;
2022-11-19 21:43:19 +00:00
use std::collections::HashMap;
2022-01-03 06:12:39 +00:00
fn main() {
2024-01-15 21:06:05 +00:00
dioxus_desktop::launch(app);
2022-01-03 06:12:39 +00:00
}
2024-01-15 21:06:05 +00:00
fn app() -> Element {
2024-01-14 05:12:21 +00:00
let breed = use_signal(|| "deerhound".to_string());
2022-11-18 06:55:46 +00:00
let breed_list = use_future(|| async move {
2022-01-03 06:12:39 +00:00
reqwest::get("https://dog.ceo/api/breeds/list/all")
.await
.unwrap()
.json::<ListBreeds>()
.await
});
match breed_list.value().read().as_ref() {
Some(Ok(breeds)) => rsx! {
2022-11-16 19:48:47 +00:00
div { height: "500px",
2022-02-21 20:39:47 +00:00
h1 { "Select a dog breed!" }
2022-01-03 06:12:39 +00:00
div { display: "flex",
ul { flex: "50%",
for cur_breed in breeds.message.keys().take(10).cloned() {
2022-11-18 06:55:46 +00:00
li { key: "{cur_breed}",
2024-01-15 22:51:34 +00:00
button { onclick: move |_| breed.set(cur_breed.clone()), "{cur_breed}" }
2022-01-03 06:12:39 +00:00
}
2022-11-17 06:10:04 +00:00
}
2022-01-03 06:12:39 +00:00
}
2024-01-15 22:40:56 +00:00
div { flex: "50%", BreedPic { breed } }
2022-01-03 06:12:39 +00:00
}
}
2024-01-14 05:12:21 +00:00
},
_ => rsx! { div { "loading breeds" } },
2022-01-03 06:12:39 +00:00
}
}
#[component]
2024-01-15 22:40:56 +00:00
fn BreedPic(breed: Signal<String>) -> Element {
let fut = use_future(|| async move {
2023-01-28 02:35:46 +00:00
reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
2022-03-09 23:11:40 +00:00
.await
.unwrap()
.json::<DogApi>()
.await
2022-01-03 06:12:39 +00:00
});
match fut.value().read().as_ref() {
Some(Ok(resp)) => rsx! {
2022-01-03 06:12:39 +00:00
div {
button { onclick: move |_| fut.restart(), "Click to fetch another doggo" }
img { max_width: "500px", max_height: "500px", src: "{resp.message}" }
2022-01-03 06:12:39 +00:00
}
2022-11-19 21:21:02 +00:00
},
_ => rsx! { div { "loading dog picture" } },
2022-11-16 19:48:47 +00:00
}
2022-01-03 06:12:39 +00:00
}
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct ListBreeds {
message: HashMap<String, Vec<String>>,
}
#[derive(serde::Deserialize, Debug)]
struct DogApi {
message: String,
}