2021-06-06 22:32:48 +00:00
|
|
|
use crate::api;
|
|
|
|
use crate::error::UiError;
|
2021-06-07 22:34:05 +00:00
|
|
|
use crate::state::{Action, Room, WebUiDispatcher};
|
2021-06-05 21:42:13 +00:00
|
|
|
use std::sync::Arc;
|
2021-06-06 22:32:48 +00:00
|
|
|
use wasm_bindgen_futures::spawn_local;
|
2021-06-05 21:42:13 +00:00
|
|
|
use web_sys::console;
|
|
|
|
use yew::prelude::*;
|
2021-06-07 22:34:05 +00:00
|
|
|
use yewdux::dispatch::Dispatcher;
|
2021-06-05 21:42:13 +00:00
|
|
|
use yewdux::prelude::*;
|
|
|
|
use yewtil::NeqAssign;
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub(crate) struct YewduxRoomList {
|
2021-06-07 22:34:05 +00:00
|
|
|
dispatch: WebUiDispatcher,
|
2021-06-05 21:42:13 +00:00
|
|
|
link: ComponentLink<YewduxRoomList>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) type RoomList = WithDispatch<YewduxRoomList>;
|
|
|
|
|
|
|
|
fn view_room(room: &Room) -> Html {
|
|
|
|
html! {
|
2021-06-07 22:34:05 +00:00
|
|
|
<li>
|
|
|
|
{&room.display_name} {" ("}{&room.room_id}{")"}
|
|
|
|
</li>
|
2021-06-05 21:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:34:05 +00:00
|
|
|
async fn load_rooms(dispatch: &WebUiDispatcher) -> Result<(), UiError> {
|
2021-06-11 14:24:49 +00:00
|
|
|
let jwt_token = dispatch
|
|
|
|
.state()
|
|
|
|
.jwt_token
|
|
|
|
.as_ref()
|
|
|
|
.ok_or(UiError::NotLoggedIn)?;
|
|
|
|
|
|
|
|
let rooms = api::dicebot::rooms_for_user(jwt_token, "@projectmoon:agnos.is").await?;
|
2021-06-06 15:45:43 +00:00
|
|
|
|
|
|
|
for room in rooms {
|
|
|
|
dispatch.send(Action::AddRoom(Room {
|
|
|
|
room_id: room.room_id,
|
|
|
|
display_name: room.display_name,
|
|
|
|
}));
|
|
|
|
}
|
2021-06-06 22:32:48 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-06-05 21:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 22:34:03 +00:00
|
|
|
async fn do_refresh_jwt(dispatch: &WebUiDispatcher) -> Result<(), UiError> {
|
|
|
|
let jwt = api::auth::refresh_jwt().await?;
|
|
|
|
dispatch.send(Action::UpdateJwt(jwt));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:42:13 +00:00
|
|
|
impl Component for YewduxRoomList {
|
|
|
|
type Message = ();
|
2021-06-07 22:34:05 +00:00
|
|
|
type Properties = WebUiDispatcher;
|
2021-06-05 21:42:13 +00:00
|
|
|
|
|
|
|
fn create(dispatch: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
Self { dispatch, link }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, dispatch: Self::Properties) -> ShouldRender {
|
|
|
|
self.dispatch.neq_assign(dispatch)
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:34:05 +00:00
|
|
|
fn rendered(&mut self, first_render: bool) {
|
|
|
|
if first_render {
|
|
|
|
let dispatch = Arc::new(self.dispatch.clone());
|
2021-06-05 21:42:13 +00:00
|
|
|
|
|
|
|
spawn_local(async move {
|
2021-06-06 22:32:48 +00:00
|
|
|
//TODO make macro to report errors in some common way:
|
|
|
|
//handle_errors!(do_things(&*dispatch).await)
|
|
|
|
match load_rooms(&*dispatch).await {
|
|
|
|
Err(e) => console::log_1(&format!("Error: {:?}", e).into()),
|
|
|
|
_ => (),
|
|
|
|
}
|
2021-06-05 21:42:13 +00:00
|
|
|
});
|
2021-06-07 22:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-06-11 13:46:05 +00:00
|
|
|
let dispatch = Arc::new(self.dispatch.clone());
|
2021-06-11 14:24:49 +00:00
|
|
|
let dispatch2 = dispatch.clone();
|
2021-06-11 22:34:03 +00:00
|
|
|
let dispatch3 = dispatch.clone();
|
2021-06-11 14:24:49 +00:00
|
|
|
|
|
|
|
let the_future = self.link.callback(move |_| {
|
2021-06-11 13:46:05 +00:00
|
|
|
let dispatch = dispatch.clone();
|
2021-06-11 14:24:49 +00:00
|
|
|
|
|
|
|
spawn_local(async move {
|
|
|
|
//TODO make macro to report errors in some common way:
|
|
|
|
//handle_errors!(do_things(&*dispatch).await)
|
|
|
|
match load_rooms(&*dispatch).await {
|
|
|
|
Err(e) => console::log_1(&format!("Error: {:?}", e).into()),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-11 22:34:03 +00:00
|
|
|
let refresh_jwt = self.link.callback(move |_| {
|
|
|
|
let dispatch = dispatch3.clone();
|
|
|
|
spawn_local(async move {
|
|
|
|
do_refresh_jwt(&*dispatch).await;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-05 21:42:13 +00:00
|
|
|
html! {
|
|
|
|
<div>
|
2021-06-12 14:52:55 +00:00
|
|
|
<button onclick=the_future>{ "Fetch Rooms" }</button>
|
2021-06-11 22:34:03 +00:00
|
|
|
<button onclick=refresh_jwt>{ "Refresh JWT" }</button>
|
2021-06-05 21:42:13 +00:00
|
|
|
<ul>
|
|
|
|
{
|
2021-06-07 22:34:05 +00:00
|
|
|
for self.dispatch.state().rooms.iter().map(|room| {
|
|
|
|
view_room(room)
|
2021-06-05 21:42:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//New oath form
|
|
|
|
|
|
|
|
//Edit oath
|