From c7e7f6d682a3e45e41698f3f47ab9439b36145c3 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 May 2023 13:48:28 +0300 Subject: feat(corp/rih): implement routing support for privacy policy Mounts the privacy policy at `/privacy-policy`. Using yew_router "properly" is difficult in components that don't make use of macros and context magic, so I've opted to use the gloo history handling directly to parse the location here. Change-Id: Icde11485f9947bc860a7b2c43772bb0f4cdf2ea1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8653 Tested-by: BuildkiteCI Reviewed-by: tazjin --- corp/rih/src/main.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'corp') diff --git a/corp/rih/src/main.rs b/corp/rih/src/main.rs index 5cad294a8e02..7360a6979d76 100644 --- a/corp/rih/src/main.rs +++ b/corp/rih/src/main.rs @@ -1,6 +1,8 @@ use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::FuzzyMatcher; use gloo::console; +use gloo::history::BrowserHistory; +use gloo::history::History; use gloo::storage::{LocalStorage, Storage}; use rand::seq::IteratorRandom; use rand::thread_rng; @@ -12,6 +14,7 @@ use wasm_bindgen::JsCast; use web_sys::{HtmlInputElement, HtmlTextAreaElement, KeyboardEvent}; use yew::html::Scope; use yew::prelude::*; +use yew_router::prelude::*; /// This code ends up being compiled for the native and for the /// webassembly architectures during the build & test process. @@ -59,6 +62,17 @@ impl CountryCodeAccess for rust_iso3166::CountryCode { const VISTA_URL: &'static str = "https://vista-immigration.ru/"; +#[derive(Debug, Clone, Copy, PartialEq, Routable)] +enum Route { + #[at("/")] + Home, + #[at("/privacy-policy")] + PrivacyPolicy, + #[not_found] + #[at("/404")] + NotFound, +} + /// Represents a single record as filled in by a user. This is the /// primary data structure we want to populate and persist somewhere. #[derive(Default, Debug, Deserialize, Serialize)] @@ -86,6 +100,9 @@ struct App { // Current query in the citizenship field. citizenship_query: String, + + // History handler. + history: BrowserHistory, } #[derive(Clone, Debug)] @@ -360,8 +377,16 @@ impl Component for App { fn view(&self, ctx: &Context) -> Html { let link = ctx.link(); - - include!("home.html") + let location = self.history.location(); + let route = Route::recognize(location.path()).unwrap_or(Route::NotFound); + + match route { + Route::Home => include!("home.html"), + Route::PrivacyPolicy => html! { +
{include!("privacy-policy.md")}
+ }, + Route::NotFound => todo!(), + } } } -- cgit 1.4.1