use futures::{self, FutureExt}; use yew::prelude::*; mod services; fn main() { yew::Renderer::::new().render(); } struct SubxtExamplesComponent { operation_title: Option, lines: Vec, } enum Message { Error(subxt::Error), Reload, Line(AttrValue), Lines(Vec), ButtonClick(Button), } enum Button { SubscribeFinalized, FetchConstant, FetchEvents, } impl Component for SubxtExamplesComponent { type Message = Message; type Properties = (); fn create(_ctx: &Context) -> Self { SubxtExamplesComponent { lines: Vec::new(), operation_title: None, } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Message::Error(err) => { self.lines.push(err.to_string().into()); } Message::Reload => { let window = web_sys::window().expect("Failed to access the window object"); window .location() .reload() .expect("Failed to reload the page"); } Message::Line(line) => { self.lines.push(line); } Message::Lines(mut lines) => { self.lines.append(&mut lines); } Message::ButtonClick(button) => match button { Button::SubscribeFinalized => { self.operation_title = Some("Subscribe to finalized blocks:".into()); let cb: Callback = ctx.link().callback(Message::Line); ctx.link() .send_future(services::subscribe_to_finalized_blocks(cb).map(|result| { let err = result.unwrap_err(); Message::Error(err) })); } Button::FetchConstant => { self.operation_title = Some("Fetch the constant \"block_length\" of \"System\" pallet:".into()); ctx.link() .send_future(services::fetch_constant_block_length().map(|result| { match result { Ok(value) => Message::Line( format!( "constant \"block_length\" of \"System\" pallet:\n {value}" ) .into(), ), Err(err) => Message::Error(err), } })) } Button::FetchEvents => { self.operation_title = Some("Fetch events:".into()); ctx.link() .send_future(services::fetch_events_dynamically().map( |result| match result { Ok(value) => { Message::Lines(value.into_iter().map(AttrValue::from).collect()) } Err(err) => Message::Error(err), }, )) } }, } true } fn view(&self, ctx: &Context) -> Html { let reload: Callback = ctx.link().callback(|_| Message::Reload); let subscribe_finalized = ctx .link() .callback(|_| Message::ButtonClick(Button::SubscribeFinalized)); let fetch_constant = ctx .link() .callback(|_| Message::ButtonClick(Button::FetchConstant)); let fetch_events = ctx .link() .callback(|_| Message::ButtonClick(Button::FetchEvents)); html! {
if let Some(operation_title) = &self.operation_title{

{operation_title}

if self.lines.is_empty(){

{"Loading..."}

} else{ } { for self.lines.iter().map(|line| html! {

{line}

}) } } else{ <>

{"Subxt Examples"}

}
} } }