From 422b9452a22b2be1b0702f155a50ab389130ea75 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 30 Aug 2021 15:55:08 +0200 Subject: [PATCH] Add yew-router. --- Cargo.toml | 1 + source/main.rs | 31 ++++++++++++++++++++----- source/routes/home.rs | 35 +++++++++++++++++++++++++++++ source/routes/mod.rs | 4 ++++ source/scss/components/_errors.scss | 8 +++++++ source/scss/index.scss | 1 + 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 source/routes/home.rs create mode 100644 source/routes/mod.rs create mode 100644 source/scss/components/_errors.scss diff --git a/Cargo.toml b/Cargo.toml index 9d1448b..700ccda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4.14" rand = "0.8.4" wasm-logger = "0.2.0" yew = "0.18.0" +yew-router = "0.15.0" [dependencies.getrandom] version = "0.2.3" diff --git a/source/main.rs b/source/main.rs index b3eff73..86da266 100644 --- a/source/main.rs +++ b/source/main.rs @@ -4,11 +4,21 @@ //! # [bauke.xyz](https://bauke.xyz) use yew::prelude::*; +use yew_router::router::Router; /// Components collection. pub(crate) mod components; +/// Routes collection. +pub(crate) mod routes; -use components::{PageFooter, PageHeader, PageMain}; +/// All routes. +#[derive(Clone, yew_router::Switch)] +pub(crate) enum Route { + #[to = "/{}"] + NotFound(String), + #[to = "/"] + Home, +} /// The main component. pub(crate) struct Model; @@ -31,11 +41,20 @@ impl Component for Model { fn view(&self) -> Html { html! { - <> - - - - + + render = Router::render(|route: Route| { + match route { + Route::NotFound(_) => html! { +
+

{"🤷"}

+
+ }, + Route::Home => html! { + + } + } + }) + /> } } } diff --git a/source/routes/home.rs b/source/routes/home.rs new file mode 100644 index 0000000..495876b --- /dev/null +++ b/source/routes/home.rs @@ -0,0 +1,35 @@ +use yew::prelude::*; + +use crate::components::{PageFooter, PageHeader, PageMain}; + +/// The route for `/`. +pub(crate) struct HomeRoute; + +impl Component for HomeRoute { + type Message = (); + type Properties = (); + + fn create(_props: Self::Properties, _link: ComponentLink) -> Self { + log::trace!("Creating HomeRoute"); + + Self + } + + fn update(&mut self, _msg: Self::Message) -> ShouldRender { + unimplemented!() + } + + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + html! { + <> + + + + + } + } +} diff --git a/source/routes/mod.rs b/source/routes/mod.rs new file mode 100644 index 0000000..7eedb86 --- /dev/null +++ b/source/routes/mod.rs @@ -0,0 +1,4 @@ +/// The route for `/`. +mod home; + +pub(crate) use home::HomeRoute; diff --git a/source/scss/components/_errors.scss b/source/scss/components/_errors.scss new file mode 100644 index 0000000..326a5c8 --- /dev/null +++ b/source/scss/components/_errors.scss @@ -0,0 +1,8 @@ +.error-404 { + @include responsive-container; + align-items: center; + display: flex; + height: 100vh; + justify-content: center; + padding: 1rem; +} diff --git a/source/scss/index.scss b/source/scss/index.scss index 7d5c0b6..405db7e 100644 --- a/source/scss/index.scss +++ b/source/scss/index.scss @@ -25,6 +25,7 @@ button { font-family: monospace; } +@import 'components/errors'; @import 'components/page-footer'; @import 'components/page-header'; @import 'components/page-main';