76 lines
1.7 KiB
Rust
76 lines
1.7 KiB
Rust
use crate::event_bus::EventBus;
|
|
use crate::services::websocket::WebsocketService;
|
|
use crate::AppState;
|
|
use crate::Route;
|
|
use common::Achievement;
|
|
use std::rc::Rc;
|
|
use yew::functional::*;
|
|
use yew::prelude::*;
|
|
use yew_agent::Bridge;
|
|
use yew_agent::Bridged;
|
|
use yew_router::prelude::*;
|
|
|
|
pub struct Root {
|
|
wss: WebsocketService,
|
|
_producer: Box<dyn Bridge<EventBus>>,
|
|
achievements: Vec<Achievement>,
|
|
}
|
|
|
|
pub enum Msg {
|
|
HandleMsg(String),
|
|
}
|
|
|
|
impl Component for Root {
|
|
type Message = Msg;
|
|
|
|
type Properties = ();
|
|
|
|
fn create(ctx: &Context<Self>) -> Self {
|
|
// let ctx_link = ctx
|
|
// .link()
|
|
// .context::<AppState>(Callback::noop())
|
|
// .expect("context to be set");
|
|
|
|
let wss = WebsocketService::new();
|
|
|
|
let cb = {
|
|
let link = ctx.link().clone();
|
|
move |e| link.send_message(Msg::HandleMsg(e))
|
|
};
|
|
|
|
Self {
|
|
wss,
|
|
_producer: EventBus::bridge(Rc::new(cb)),
|
|
achievements: vec![],
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::HandleMsg(s) => {
|
|
let msg: common::WebSocketMessage = serde_json::from_str(&s).unwrap();
|
|
self.achievements = msg.achievements;
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
let achievements = self
|
|
.achievements
|
|
.iter()
|
|
.map(|a| {
|
|
html! {
|
|
<p>{format!("{}", a.goal)}</p>
|
|
}
|
|
})
|
|
.collect::<Html>();
|
|
|
|
html! {
|
|
<div>
|
|
{achievements}
|
|
</div>
|
|
}
|
|
}
|
|
}
|