use crate::components::error_message::ErrorMessage; use crate::components::login::Login; use error::UiError; use rooms::RoomList; use rooms::YewduxRoomList; use state::{Action, AuthState, WebUiDispatcher}; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::spawn_local; use yew::prelude::*; use yew_router::prelude::*; use yew_router::{components::RouterAnchor, prelude::*, switch::Permissive}; use yewdux::prelude::*; use yewtil::NeqAssign; pub mod api; pub mod components; pub mod error; pub mod grpc; pub mod logic; pub mod rooms; pub mod state; #[derive(Clone, Debug, Switch)] pub enum AppRoute { #[to = "/rooms"] Rooms, #[to = "/rooms/{room_id}"] Room { room_id: String }, #[to = "/"] Index, } type AppRouter = Router; type AppAnchor = RouterAnchor; //For rendering clickable links. fn render_route(routes: AppRoute) -> Html { match routes { AppRoute::Rooms => { html! { } } AppRoute::Room { room_id } => { html! {
{"This is the specific room page."}
} } AppRoute::Index => { html! {
} } } } struct YewduxApp { link: ComponentLink, dispatch: WebUiDispatcher, } type App = WithDispatch; async fn refresh_jwt(dispatch: &WebUiDispatcher) { match api::auth::refresh_jwt().await { Ok(jwt) => { dispatch.send(Action::Login(jwt)); } Err(e) => { web_sys::console::log_1(&e.to_string().into()); dispatch.send(Action::ChangeAuthState(AuthState::NotLoggedIn)); } } } impl Component for YewduxApp { type Message = (); type Properties = WebUiDispatcher; fn create(dispatch: Self::Properties, link: ComponentLink) -> Self { Self { dispatch, link } } fn update(&mut self, _: Self::Message) -> ShouldRender { false } fn change(&mut self, dispatch: Self::Properties) -> ShouldRender { self.dispatch.neq_assign(dispatch) } fn rendered(&mut self, first_render: bool) { if first_render { let auth_state = self.dispatch.state().auth_state; if auth_state == AuthState::RefreshNeeded { let dispatch = self.dispatch.clone(); spawn_local(async move { refresh_jwt(&dispatch).await; }); } } } fn view(&self) -> Html { let auth_state = self.dispatch.state().auth_state; match auth_state { AuthState::RefreshNeeded => { html! {
{"Loading..."}
} } AuthState::NotLoggedIn => { html! { } } AuthState::LoggedIn => { html! {
} } } } } #[wasm_bindgen(start)] pub fn run_app() { yew::start_app::(); }