2023-09-02 15:24:30 +00:00
//! Example: Url query segments usage
//! ------------------------------------
//!
//! This example shows how to access and use multiple query segments present in an url on the web.
//!
//! Run `dx serve` and navigate to `http://localhost:8080/blog?name=John&surname=Doe`
use dioxus ::prelude ::* ;
2024-01-20 06:27:54 +00:00
use std ::fmt ::Display ;
2023-09-02 15:24:30 +00:00
#[ derive(Routable, Clone) ]
#[ rustfmt::skip ]
enum Route {
2023-12-01 21:31:45 +00:00
// segments that start with ?:.. are query segments that capture the entire query
#[ route( " /blog?:..query_params " ) ]
2023-09-02 15:24:30 +00:00
BlogPost {
// You must include query segments in child variants
2023-12-01 21:31:45 +00:00
query_params : ManualBlogQuerySegments ,
} ,
2024-01-20 06:27:54 +00:00
2023-12-01 21:31:45 +00:00
// segments that follow the ?:field&:other_field syntax are query segments that follow the standard url query syntax
#[ route( " /autoblog?:name&:surname " ) ]
AutomaticBlogPost {
name : String ,
surname : String ,
2023-09-02 15:24:30 +00:00
} ,
}
#[ derive(Debug, Clone, PartialEq) ]
2023-12-01 21:31:45 +00:00
struct ManualBlogQuerySegments {
2023-09-02 15:24:30 +00:00
name : String ,
surname : String ,
}
/// The display impl needs to display the query in a way that can be parsed:
2023-12-01 21:31:45 +00:00
impl Display for ManualBlogQuerySegments {
2023-09-02 15:24:30 +00:00
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
write! ( f , " name={}&surname={} " , self . name , self . surname )
}
}
2024-01-20 00:36:40 +00:00
/// The query segment is anything that implements <https://docs.rs/dioxus-router/latest/dioxus::router/routable/trait.FromQuery.html>. You can implement that trait for a struct if you want to parse multiple query parameters.
2023-12-01 21:31:45 +00:00
impl FromQuery for ManualBlogQuerySegments {
2023-09-02 15:24:30 +00:00
fn from_query ( query : & str ) -> Self {
let mut name = None ;
let mut surname = None ;
let pairs = form_urlencoded ::parse ( query . as_bytes ( ) ) ;
pairs . for_each ( | ( key , value ) | {
if key = = " name " {
name = Some ( value . clone ( ) . into ( ) ) ;
}
if key = = " surname " {
surname = Some ( value . clone ( ) . into ( ) ) ;
}
} ) ;
Self {
name : name . unwrap ( ) ,
surname : surname . unwrap ( ) ,
}
}
}
2023-09-15 14:13:36 +00:00
#[ component ]
2024-01-14 04:51:37 +00:00
fn BlogPost ( query_params : ManualBlogQuerySegments ) -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2024-01-16 20:39:48 +00:00
div { " This is your blogpost with a query segment: " }
div { " {query_params:?} " }
2023-09-02 15:24:30 +00:00
}
}
2023-12-01 21:31:45 +00:00
#[ component ]
2024-01-14 04:51:37 +00:00
fn AutomaticBlogPost ( name : String , surname : String ) -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2024-01-16 20:39:48 +00:00
div { " This is your blogpost with a query segment: " }
div { " name={name}&surname={surname} " }
2023-12-01 21:31:45 +00:00
}
}
2023-09-15 14:13:36 +00:00
#[ component ]
2024-01-14 04:51:37 +00:00
fn App ( ) -> Element {
2024-01-16 20:39:48 +00:00
rsx! { Router ::< Route > { } }
2023-09-02 15:24:30 +00:00
}
fn main ( ) {
2024-01-19 03:27:55 +00:00
launch ( App ) ;
2023-09-02 15:24:30 +00:00
}