use crate::infra::api::HostService; use anyhow::Result; use graphql_client::GraphQLQuery; use wasm_bindgen_futures::spawn_local; use yew::{use_effect, use_state_eq, UseStateHandle}; // Enum to represent a result that is fetched asynchronously. #[derive(Debug)] pub enum LoadableResult { // The result is still being fetched Loading, // The async call is completed Loaded(Result), } impl PartialEq for LoadableResult { fn eq(&self, other: &Self) -> bool { match (self, other) { (LoadableResult::Loading, LoadableResult::Loading) => true, (LoadableResult::Loaded(Ok(d1)), LoadableResult::Loaded(Ok(d2))) => d1.eq(d2), (LoadableResult::Loaded(Err(e1)), LoadableResult::Loaded(Err(e2))) => { e1.to_string().eq(&e2.to_string()) } _ => false, } } } pub fn use_graphql_call( variables: QueryType::Variables, ) -> UseStateHandle> where QueryType: GraphQLQuery + 'static, ::ResponseData: std::cmp::PartialEq, { let loadable_result: UseStateHandle> = use_state_eq(|| LoadableResult::Loading); { let loadable_result = loadable_result.clone(); use_effect(move || { let task = HostService::graphql_query::(variables, "Failed graphql query"); spawn_local(async move { let response = task.await; loadable_result.set(LoadableResult::Loaded(response)); }); || () }) } loadable_result.clone() }