dioxus/examples/dog_app.rs

91 lines
3.1 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! This example demonstrates a simple app that fetches a list of dog breeds and displays a random dog.
//!
//! The app uses the `use_signal` and `use_resource` hooks to manage state and fetch data from the Dog API.
//! `use_resource` is basically an async version of use_memo - it will track dependencies between .await points
//! and then restart the future if any of the dependencies change.
//!
//! You should generally throttle requests to an API - either client side or server side. This example doesn't do that
//! since it's unlikely the user will rapidly cause new fetches, but it's something to keep in mind.
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-02-14 20:33:07 +00:00
// Breed is a signal that will be updated when the user clicks a breed in the list
// `shiba` is just a default that we know will exist. We could also use a `None` instead
let mut breed = use_signal(|| "shiba".to_string());
2024-02-14 20:33:07 +00:00
// Fetch the list of breeds from the Dog API
// Since there are no dependencies, this will never restart
let breed_list = use_resource(move || async move {
2024-02-14 20:33:07 +00:00
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct ListBreeds {
message: HashMap<String, Vec<String>>,
}
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-02-14 20:33:07 +00:00
for cur_breed in breeds.message.keys().take(20).cloned() {
2024-01-16 04:45:59 +00:00
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-02-14 20:33:07 +00:00
// We can use early returns in dioxus!
// Traditional signal-based libraries can't do this since the scope is by default non-reactive
2024-02-05 19:59:50 +00:00
let Some(breed_list) = breed_list() 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",
2024-02-14 20:33:07 +00:00
ul { width: "100px", {breed_list} }
div { flex: 1, BreedPic { breed } }
2024-01-16 06:14:58 +00:00
}
2022-01-03 06:12:39 +00:00
}
}
#[component]
2024-01-15 22:40:56 +00:00
fn BreedPic(breed: Signal<String>) -> Element {
2024-02-14 20:33:07 +00:00
// This resource will restart whenever the breed changes
2024-02-05 19:59:50 +00:00
let mut fut = use_resource(move || async move {
2024-02-14 20:33:07 +00:00
#[derive(serde::Deserialize, Debug)]
struct DogApi {
message: String,
}
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-03-08 16:32:57 +00:00
match fut.read_unchecked().as_ref() {
2024-01-16 19:18:46 +00:00
Some(Ok(resp)) => rsx! {
2024-02-05 19:59:50 +00:00
button { onclick: move |_| fut.restart(), "Click to fetch another doggo" }
img { max_width: "500px", max_height: "500px", src: "{resp.message}" }
2022-11-19 21:21:02 +00:00
},
2024-02-05 19:59:50 +00:00
Some(Err(_)) => rsx! { "loading image failed" },
None => rsx! { "loading image..." },
2022-11-16 19:48:47 +00:00
}
2022-01-03 06:12:39 +00:00
}