Started with yew router

This commit is contained in:
Marko Korhonen 2020-04-24 10:54:04 +03:00
parent 3243f5d79b
commit a42d8557a8
No known key found for this signature in database
GPG key ID: 911B85FBC6003FE5
2 changed files with 42 additions and 26 deletions

View file

@ -1,5 +1,5 @@
APP_NAME=thesis APP_NAME=thesis
BIND=127.0.0.1 BIND=0.0.0.0
PORT=3880 PORT=3880
DOMAIN=localhost DOMAIN=localhost
DATABASE_URL=mysql://diesel:fuel@localhost/thesis DATABASE_URL=mysql://diesel:fuel@localhost/thesis

View file

@ -1,44 +1,60 @@
#![recursion_limit = "256"]
mod component; mod component;
use component::login::LoginComponent; use component::login::LoginComponent;
use yew::prelude::*; use yew::prelude::*;
use yew::virtual_dom::VNode;
use yew_router::{prelude::*, switch::Permissive, Switch};
struct App { struct App {}
clicked: bool,
onclick: Callback<ClickEvent>,
}
enum Msg { #[derive(Debug, Switch, Clone)]
Click, enum AppRoute {
#[to = "/"]
Root,
#[to = "/login"]
Login,
PageNotFound(Permissive<String>),
} }
impl Component for App { impl Component for App {
type Message = Msg; type Message = ();
type Properties = (); type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
App { App {}
clicked: false,
onclick: link.callback(|_| Msg::Click),
}
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, _msg: Self::Message) -> ShouldRender {
match msg { true
Msg::Click => {
if self.clicked {
self.clicked = false;
} else {
self.clicked = true
}
true
}
}
} }
fn view(&self) -> Html { fn view(&self) -> VNode {
html! { html! {
<LoginComponent:/> <div>
<nav class="menu",>
<RouterButton<AppRoute> route=AppRoute::Root>{"Go to Root"}</RouterButton<AppRoute>>
<RouterButton<AppRoute> route=AppRoute::Login>{"Go to Login"}</RouterButton<AppRoute>>
</nav>
<div>
<Router<AppRoute>
render = Router::render(|switch: AppRoute| {
match switch {
AppRoute::Login => html!{<LoginComponent />},
AppRoute::Root => {
html!{"hello there!"}
},
AppRoute::PageNotFound(Permissive(None)) => html!{"Page not found"},
AppRoute::PageNotFound(Permissive(Some(missed_route))) => html!{format!("Page '{}' not found", missed_route)}
}
})
redirect = Router::redirect(|route: Route| {
AppRoute::PageNotFound(Permissive(Some(route.route)))
})
/>
</div>
</div>
} }
} }
} }