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
BIND=127.0.0.1
BIND=0.0.0.0
PORT=3880
DOMAIN=localhost
DATABASE_URL=mysql://diesel:fuel@localhost/thesis

View file

@ -1,44 +1,60 @@
#![recursion_limit = "256"]
mod component;
use component::login::LoginComponent;
use yew::prelude::*;
use yew::virtual_dom::VNode;
use yew_router::{prelude::*, switch::Permissive, Switch};
struct App {
clicked: bool,
onclick: Callback<ClickEvent>,
}
struct App {}
enum Msg {
Click,
#[derive(Debug, Switch, Clone)]
enum AppRoute {
#[to = "/"]
Root,
#[to = "/login"]
Login,
PageNotFound(Permissive<String>),
}
impl Component for App {
type Message = Msg;
type Message = ();
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
App {
clicked: false,
onclick: link.callback(|_| Msg::Click),
}
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
App {}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Click => {
if self.clicked {
self.clicked = false;
} else {
self.clicked = true
}
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
true
}
}
}
fn view(&self) -> Html {
fn view(&self) -> VNode {
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>
}
}
}