Skip to content

Commit

Permalink
use js_sys::Array for search_params + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed May 15, 2021
1 parent 36d9beb commit d25c077
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 35 deletions.
6 changes: 3 additions & 3 deletions examples/router/src/pages/post_list.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::components::{pagination::Pagination, post_card::PostCard};
use crate::Route;
use serde::Serialize;
use yew::prelude::*;
use yew_router::service;
use serde::Serialize;

const ITEMS_PER_PAGE: u64 = 10;
const TOTAL_PAGES: u64 = std::u64::MAX / ITEMS_PER_PAGE;
const TOTAL_PAGES: u64 = u64::MAX / ITEMS_PER_PAGE;

pub enum Msg {
ShowPage(u64),
}

#[derive(Serialize)]
struct PageQuery {
page: u64
page: u64,
}

pub struct PostList {
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
None => {
weblog::console_log!("no route matched");
html! {}
},
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/yew-router/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::utils::build_path_with_base;
use crate::Routable;
use serde::Serialize;
use std::collections::HashMap;
use wasm_bindgen::JsValue;
use web_sys::Event;
use serde::Serialize;

/// Navigate to a specific route.
pub fn push(route: impl Routable) {
push_impl(route.to_route())

}

/// Navigate to a specific route with query parameters.
///
/// This should be used in cases where [`Link`](crate::prelude::Link) is insufficient.
pub fn push_with_query<S>(route: impl Routable, query: S) -> Result<(), serde_urlencoded::ser::Error>
where S: Serialize
pub fn push_with_query<S>(
route: impl Routable,
query: S,
) -> Result<(), serde_urlencoded::ser::Error>
where
S: Serialize,
{
let mut url = route.to_route();
let query = serde_urlencoded::to_string(query)?;
Expand Down
25 changes: 9 additions & 16 deletions packages/yew-router/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen::JsCast;

fn strip_slash(path: String) -> String {
if path != "/" {
Expand Down Expand Up @@ -34,22 +34,15 @@ pub fn build_path_with_base(to: &str) -> String {

pub fn get_query_params() -> HashMap<String, String> {
let url = web_sys::Url::new(&yew::utils::document().url().unwrap()).unwrap();

let iter = js_sys::try_iter(&JsValue::from(&url.search_params()))
.expect("try_iter failed")
.expect("try_iter failed")
let search_params = js_sys::Array::from(url.search_params().as_ref()).to_vec();
search_params
.into_iter()
.map(|it| it.unwrap().unchecked_into::<js_sys::Array>().to_vec())
.map(|it| {
let mut iter = it.into_iter();
// unwraps are unreachable
// there will be at least 2 values here
// both of them will be strings
.map(|value| js_sys::Array::from(&value).to_vec())
.map(|chunk| {
(
iter.next().unwrap().as_string().unwrap(),
iter.next().unwrap().as_string().unwrap(),
chunk[0].as_string().expect("0"),
chunk[1].as_string().expect("1"),
)
});

iter.collect()
})
.collect()
}
9 changes: 3 additions & 6 deletions packages/yew-router/tests/parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Serialize;
use std::collections::HashMap;
use wasm_bindgen_test::wasm_bindgen_test as test;
use yew::utils::*;
use yew_router::prelude::*;
use yew_router::utils::*;
use serde::Serialize;

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

Expand Down Expand Up @@ -37,7 +37,7 @@ fn test_base_url() {
#[derive(Serialize, Clone)]
struct QueryParams {
foo: String,
bar: u32
bar: u32,
}

#[test]
Expand All @@ -49,10 +49,7 @@ fn test_get_query_params() {
bar: 69,
};

service::push_with_query(
Routes::Home,
query.clone(),
).unwrap();
service::push_with_query(Routes::Home, query.clone()).unwrap();

let params = get_query_params();

Expand Down
7 changes: 2 additions & 5 deletions packages/yew-router/tests/router.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::Serialize;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use yew::prelude::*;
use yew_functional::function_component;
use yew_router::prelude::*;
use serde::Serialize;

mod utils;
use utils::*;
Expand Down Expand Up @@ -45,10 +45,7 @@ struct Query {
fn component() -> Html {
let switch = Router::render(|routes| {
let onclick = Callback::from(|_| {
service::push_with_query(
Routes::No { id: 2 },
Query { foo: "bar" },
).unwrap();
service::push_with_query(Routes::No { id: 2 }, Query { foo: "bar" }).unwrap();
});

match routes {
Expand Down

0 comments on commit d25c077

Please sign in to comment.