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-20 08:11:55 +00:00
|
|
|
launch(app);
|
2022-01-03 06:12:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 21:06:05 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
let mut breed = use_signal(|| "deerhound".to_string());
|
2024-01-21 20:46:19 +00:00
|
|
|
let breed_list = use_resource(|| async move {
|
2024-01-16 04:45:59 +00:00
|
|
|
let list = reqwest::get("https://dog.ceo/api/breeds/list/all")
|
2022-01-03 06:12:39 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.json::<ListBreeds>()
|
2024-01-16 04:45:59 +00:00
|
|
|
.await;
|
2022-01-03 06:12:39 +00:00
|
|
|
|
2024-01-16 04:45:59 +00:00
|
|
|
let Ok(breeds) = list else {
|
2024-01-16 19:18:46 +00:00
|
|
|
return rsx! { "error fetching breeds" };
|
2024-01-16 04:45:59 +00:00
|
|
|
};
|
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-01-16 04:45:59 +00:00
|
|
|
for cur_breed in breeds.message.keys().take(10).cloned() {
|
|
|
|
li { key: "{cur_breed}",
|
|
|
|
button { onclick: move |_| breed.set(cur_breed.clone()),
|
|
|
|
"{cur_breed}"
|
2022-01-03 06:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 04:45:59 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-16 06:14:58 +00:00
|
|
|
let Some(breed_list) = breed_list.value().cloned() else {
|
2024-01-16 19:18:46 +00:00
|
|
|
return rsx! { "loading breeds..." };
|
2024-01-16 06:14:58 +00:00
|
|
|
};
|
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-01-16 06:14:58 +00:00
|
|
|
h1 { "Select a dog breed!" }
|
|
|
|
div { height: "500px", display: "flex",
|
|
|
|
ul { flex: "50%", {breed_list} }
|
|
|
|
div { flex: "50%", BreedPic { breed } }
|
|
|
|
}
|
2022-01-03 06:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-15 22:40:56 +00:00
|
|
|
fn BreedPic(breed: Signal<String>) -> Element {
|
2024-01-21 20:46:19 +00:00
|
|
|
let fut = use_resource(|| 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
|
|
|
});
|
|
|
|
|
2024-01-16 01:04:39 +00:00
|
|
|
match fut.value().read().as_ref() {
|
2024-01-16 19:18:46 +00:00
|
|
|
Some(Ok(resp)) => rsx! {
|
2022-01-03 06:12:39 +00:00
|
|
|
div {
|
2024-01-16 01:04:39 +00:00
|
|
|
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
|
|
|
},
|
2024-01-16 19:18:46 +00:00
|
|
|
_ => rsx! { "loading image..." },
|
2022-11-16 19:48:47 +00:00
|
|
|
}
|
2022-01-03 06:12:39 +00:00
|
|
|
}
|
2024-01-16 01:04:39 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
|
|
|
|
struct ListBreeds {
|
|
|
|
message: HashMap<String, Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Deserialize, Debug)]
|
|
|
|
struct DogApi {
|
|
|
|
message: String,
|
|
|
|
}
|