From c7627f0b93b8e9024063d5734409bf8d529cf800 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 2 Mar 2022 23:03:10 -0500 Subject: [PATCH] fix: tests pass --- examples/router.rs | 5 +++-- packages/router/Cargo.toml | 12 +++++++----- packages/router/README.md | 2 +- packages/router/src/components/link.rs | 4 ++-- packages/router/src/components/redirect.rs | 4 ++-- packages/router/src/components/route.rs | 2 +- packages/router/src/hooks/use_route.rs | 11 ++++++++++- packages/router/src/routecontext.rs | 4 ++-- packages/router/src/service.rs | 4 +++- packages/router/tests/ssr_router.rs | 3 ++- 10 files changed, 33 insertions(+), 18 deletions(-) diff --git a/examples/router.rs b/examples/router.rs index 216b69922..0dc09f566 100644 --- a/examples/router.rs +++ b/examples/router.rs @@ -31,7 +31,7 @@ fn app(cx: Scope) -> Element { } fn BlogPost(cx: Scope) -> Element { - let post = dioxus::router::use_route(&cx).last_segment(); + let post = dioxus::router::use_route(&cx).last_segment()?; cx.render(rsx! { div { @@ -47,7 +47,8 @@ struct Query { } fn User(cx: Scope) -> Element { - let post = dioxus::router::use_route(&cx).last_segment(); + let post = dioxus::router::use_route(&cx).last_segment()?; + let query = dioxus::router::use_route(&cx) .query::() .unwrap_or(Query { bold: false }); diff --git a/packages/router/Cargo.toml b/packages/router/Cargo.toml index b7eb6d64d..61c52341c 100644 --- a/packages/router/Cargo.toml +++ b/packages/router/Cargo.toml @@ -35,12 +35,13 @@ gloo-events = { version = "0.1.1", optional = true } log = "0.4.14" thiserror = "1.0.30" futures-util = "0.3.21" - +serde = { version = "1", optional = true } +serde_urlencoded = {version = "0.7.1", optional = true} [features] -default = ["web"] +default = ["query"] web = ["web-sys", "gloo-events", "js-sys", "wasm-bindgen"] -hash = [] +query = ["serde"] [dev-dependencies] console_error_panic_hook = "0.1.7" @@ -50,10 +51,11 @@ wasm-logger = "0.2.0" wasm-bindgen-test = "0.3" gloo-utils = "0.1.2" dioxus-ssr = { path = "../ssr"} + dioxus-router = { path = ".", default-features = false } -# [target.wasm32-unknown-unknown.dev-dependencies] -# dioxus-router = { path = ".", features = ["web"] } +[target.wasm32-unknown-unknown.dev-dependencies] +dioxus-router = { path = ".", features = ["web"] } diff --git a/packages/router/README.md b/packages/router/README.md index 7cfdb1332..d0c036bc9 100644 --- a/packages/router/README.md +++ b/packages/router/README.md @@ -51,7 +51,7 @@ Dioxus Router is a first-party Router for all your Dioxus Apps. It provides a React-Router style interface that works anywhere: across the browser, SSR, and natively. -```rust +```rust, ignore fn app() { cx.render(rsx! { Router { diff --git a/packages/router/src/components/link.rs b/packages/router/src/components/link.rs index f17a203af..3c03bb3bf 100644 --- a/packages/router/src/components/link.rs +++ b/packages/router/src/components/link.rs @@ -11,7 +11,7 @@ use dioxus_html as dioxus_elements; pub struct LinkProps<'a> { /// The route to link to. This can be a relative path, or a full URL. /// - /// ```rust + /// ```rust, ignore /// // Absolute path /// Link { to: "/home", "Go Home" } /// @@ -64,7 +64,7 @@ pub struct LinkProps<'a> { /// /// # Examples /// -/// ```rust +/// ```rust, ignore /// fn Header(cx: Scope) -> Element { /// cx.render(rsx!{ /// Link { to: "/home", "Go Home" } diff --git a/packages/router/src/components/redirect.rs b/packages/router/src/components/redirect.rs index b4261b0d4..aae8e6d92 100644 --- a/packages/router/src/components/redirect.rs +++ b/packages/router/src/components/redirect.rs @@ -9,7 +9,7 @@ use crate::use_router; pub struct RedirectProps<'a> { /// The route to link to. This can be a relative path, or a full URL. /// - /// ```rust + /// ```rust, ignore /// // Absolute path /// Redirect { from: "", to: "/home" } /// @@ -20,7 +20,7 @@ pub struct RedirectProps<'a> { /// The route to link from. This can be a relative path, or a full URL. /// - /// ```rust + /// ```rust, ignore /// // Absolute path /// Redirect { from: "", to: "/home" } /// diff --git a/packages/router/src/components/route.rs b/packages/router/src/components/route.rs index 00fa28af2..530ad77a0 100644 --- a/packages/router/src/components/route.rs +++ b/packages/router/src/components/route.rs @@ -22,7 +22,7 @@ pub struct RouteProps<'a> { /// /// # Example /// -///```rust +///```rust, ignore /// rsx!( /// Router { /// Route { to: "/home", Home {} } diff --git a/packages/router/src/hooks/use_route.rs b/packages/router/src/hooks/use_route.rs index 651a5dc1f..38fdd5fda 100644 --- a/packages/router/src/hooks/use_route.rs +++ b/packages/router/src/hooks/use_route.rs @@ -48,7 +48,16 @@ impl UseRoute { /// Get the first query parameter given the parameter name. /// /// If you need to get more than one parameter, use [`query_pairs`] on the [`Url`] instead. - pub fn query(&self, param: &str) -> Option> { + #[cfg(feature = "query")] + pub fn query(&self) -> Option { + let query = self.url().query()?; + serde_urlencoded::from_str(query.strip_prefix('?').unwrap_or("")).ok() + } + + /// Get the first query parameter given the parameter name. + /// + /// If you need to get more than one parameter, use [`query_pairs`] on the [`Url`] instead. + pub fn query_param(&self, param: &str) -> Option> { self.route .url .query_pairs() diff --git a/packages/router/src/routecontext.rs b/packages/router/src/routecontext.rs index 17d4cf850..81383227b 100644 --- a/packages/router/src/routecontext.rs +++ b/packages/router/src/routecontext.rs @@ -8,7 +8,7 @@ pub struct RouteContext { /// /// /// It follows this pattern: - /// ``` + /// ```ignore /// "name/:id" /// ``` pub declared_route: String, @@ -17,7 +17,7 @@ pub struct RouteContext { /// /// /// It follows this pattern: - /// ``` + /// ```ignore /// "/level0/level1/:id" /// ``` pub total_route: String, diff --git a/packages/router/src/service.rs b/packages/router/src/service.rs index 28e425430..5cf93c597 100644 --- a/packages/router/src/service.rs +++ b/packages/router/src/service.rs @@ -29,7 +29,7 @@ use url::Url; /// /// # Example /// -/// ```rust +/// ```rust, ignore /// let router = Router::new(); /// router.push_route("/home/custom"); /// cx.provide_context(router); @@ -280,6 +280,8 @@ mod hash { serialized_state: None, } } + + fn replace(&self, _route: &ParsedRoute) {} } } diff --git a/packages/router/tests/ssr_router.rs b/packages/router/tests/ssr_router.rs index 71ff38e6d..212daa7cc 100644 --- a/packages/router/tests/ssr_router.rs +++ b/packages/router/tests/ssr_router.rs @@ -12,13 +12,14 @@ fn generates_without_error() { let out = dioxus_ssr::render_vdom(&app); - assert_eq!(out, "

Home

"); + assert_eq!(out, "default"); } fn app(cx: Scope) -> Element { cx.render(rsx! { Router { nav { "navbar" } + Route { to: "/", "default" } Route { to: "/home", Home {} } } })