File tree Expand file tree Collapse file tree 6 files changed +25
-35
lines changed
examples/router/src/pages Expand file tree Collapse file tree 6 files changed +25
-35
lines changed Original file line number Diff line number Diff line change 11use crate :: components:: { pagination:: Pagination , post_card:: PostCard } ;
22use crate :: Route ;
3+ use serde:: Serialize ;
34use yew:: prelude:: * ;
45use yew_router:: service;
5- use serde:: Serialize ;
66
77const ITEMS_PER_PAGE : u64 = 10 ;
8- const TOTAL_PAGES : u64 = std :: u64:: MAX / ITEMS_PER_PAGE ;
8+ const TOTAL_PAGES : u64 = u64:: MAX / ITEMS_PER_PAGE ;
99
1010pub enum Msg {
1111 ShowPage ( u64 ) ,
1212}
1313
1414#[ derive( Serialize ) ]
1515struct PageQuery {
16- page : u64
16+ page : u64 ,
1717}
1818
1919pub struct PostList {
Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ where
122122 None => {
123123 weblog:: console_log!( "no route matched" ) ;
124124 html ! { }
125- } ,
125+ }
126126 }
127127 }
128128
Original file line number Diff line number Diff line change 11use crate :: utils:: build_path_with_base;
22use crate :: Routable ;
3+ use serde:: Serialize ;
34use std:: collections:: HashMap ;
45use wasm_bindgen:: JsValue ;
56use web_sys:: Event ;
6- use serde:: Serialize ;
77
88/// Navigate to a specific route.
99pub fn push ( route : impl Routable ) {
1010 push_impl ( route. to_route ( ) )
11-
1211}
1312
1413/// Navigate to a specific route with query parameters.
1514///
1615/// This should be used in cases where [`Link`](crate::prelude::Link) is insufficient.
17- pub fn push_with_query < S > ( route : impl Routable , query : S ) -> Result < ( ) , serde_urlencoded:: ser:: Error >
18- where S : Serialize
16+ pub fn push_with_query < S > (
17+ route : impl Routable ,
18+ query : S ,
19+ ) -> Result < ( ) , serde_urlencoded:: ser:: Error >
20+ where
21+ S : Serialize ,
1922{
2023 let mut url = route. to_route ( ) ;
2124 let query = serde_urlencoded:: to_string ( query) ?;
Original file line number Diff line number Diff line change 11use std:: collections:: HashMap ;
2- use wasm_bindgen:: { JsCast , JsValue } ;
2+ use wasm_bindgen:: JsCast ;
33
44fn strip_slash ( path : String ) -> String {
55 if path != "/" {
@@ -34,22 +34,15 @@ pub fn build_path_with_base(to: &str) -> String {
3434
3535pub fn get_query_params ( ) -> HashMap < String , String > {
3636 let url = web_sys:: Url :: new ( & yew:: utils:: document ( ) . url ( ) . unwrap ( ) ) . unwrap ( ) ;
37-
38- let iter = js_sys:: try_iter ( & JsValue :: from ( & url. search_params ( ) ) )
39- . expect ( "try_iter failed" )
40- . expect ( "try_iter failed" )
37+ let search_params = js_sys:: Array :: from ( url. search_params ( ) . as_ref ( ) ) . to_vec ( ) ;
38+ search_params
4139 . into_iter ( )
42- . map ( |it| it. unwrap ( ) . unchecked_into :: < js_sys:: Array > ( ) . to_vec ( ) )
43- . map ( |it| {
44- let mut iter = it. into_iter ( ) ;
45- // unwraps are unreachable
46- // there will be at least 2 values here
47- // both of them will be strings
40+ . map ( |value| js_sys:: Array :: from ( & value) . to_vec ( ) )
41+ . map ( |chunk| {
4842 (
49- iter . next ( ) . unwrap ( ) . as_string ( ) . unwrap ( ) ,
50- iter . next ( ) . unwrap ( ) . as_string ( ) . unwrap ( ) ,
43+ chunk [ 0 ] . as_string ( ) . expect ( "0" ) ,
44+ chunk [ 1 ] . as_string ( ) . expect ( "1" ) ,
5145 )
52- } ) ;
53-
54- iter. collect ( )
46+ } )
47+ . collect ( )
5548}
Original file line number Diff line number Diff line change 1+ use serde:: Serialize ;
12use std:: collections:: HashMap ;
23use wasm_bindgen_test:: wasm_bindgen_test as test;
34use yew:: utils:: * ;
45use yew_router:: prelude:: * ;
56use yew_router:: utils:: * ;
6- use serde:: Serialize ;
77
88wasm_bindgen_test:: wasm_bindgen_test_configure!( run_in_browser) ;
99
@@ -37,7 +37,7 @@ fn test_base_url() {
3737#[ derive( Serialize , Clone ) ]
3838struct QueryParams {
3939 foo : String ,
40- bar : u32
40+ bar : u32 ,
4141}
4242
4343#[ test]
@@ -49,10 +49,7 @@ fn test_get_query_params() {
4949 bar : 69 ,
5050 } ;
5151
52- service:: push_with_query (
53- Routes :: Home ,
54- query. clone ( ) ,
55- ) . unwrap ( ) ;
52+ service:: push_with_query ( Routes :: Home , query. clone ( ) ) . unwrap ( ) ;
5653
5754 let params = get_query_params ( ) ;
5855
Original file line number Diff line number Diff line change 1+ use serde:: Serialize ;
12use wasm_bindgen_test:: { wasm_bindgen_test as test, wasm_bindgen_test_configure} ;
23use yew:: prelude:: * ;
34use yew_functional:: function_component;
45use yew_router:: prelude:: * ;
5- use serde:: Serialize ;
66
77mod utils;
88use utils:: * ;
@@ -45,10 +45,7 @@ struct Query {
4545fn component ( ) -> Html {
4646 let switch = Router :: render ( |routes| {
4747 let onclick = Callback :: from ( |_| {
48- service:: push_with_query (
49- Routes :: No { id : 2 } ,
50- Query { foo : "bar" } ,
51- ) . unwrap ( ) ;
48+ service:: push_with_query ( Routes :: No { id : 2 } , Query { foo : "bar" } ) . unwrap ( ) ;
5249 } ) ;
5350
5451 match routes {
You can’t perform that action at this time.
0 commit comments