2022-12-09 00:15:04 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
2023-05-24 00:09:24 +00:00
|
|
|
use dioxus_router::prelude::*;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::str::FromStr;
|
2022-12-09 00:15:04 +00:00
|
|
|
|
|
|
|
fn main() {
|
2023-05-24 00:09:24 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
dioxus_desktop::launch(root);
|
2022-12-09 00:15:04 +00:00
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
dioxus_web::launch(root);
|
|
|
|
}
|
2022-12-15 19:09:53 +00:00
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
fn root(cx: Scope) -> Element {
|
|
|
|
render! {
|
2023-05-24 00:31:30 +00:00
|
|
|
Router {
|
2023-05-24 00:09:24 +00:00
|
|
|
config: RouterConfiguration {
|
|
|
|
history: {
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
let history = Box::<MemoryHistory::<Route>>::default();
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
let history = Box::<WebHistory::<Route>>::default();
|
|
|
|
history
|
|
|
|
},
|
2022-12-15 19:09:53 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
2023-05-24 00:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-09 00:15:04 +00:00
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
#[inline_props]
|
|
|
|
fn UserFrame(cx: Scope, user_id: usize) -> Element {
|
2022-12-09 00:15:04 +00:00
|
|
|
render! {
|
2023-05-24 00:09:24 +00:00
|
|
|
pre {
|
|
|
|
"UserFrame{{\n\tuser_id:{user_id}\n}}"
|
|
|
|
}
|
|
|
|
div {
|
|
|
|
background_color: "rgba(0,0,0,50%)",
|
|
|
|
"children:"
|
2023-05-24 00:31:30 +00:00
|
|
|
Outlet {}
|
2022-12-09 03:11:31 +00:00
|
|
|
}
|
2022-12-09 00:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
#[inline_props]
|
2023-05-31 17:11:11 +00:00
|
|
|
fn Route1(cx: Scope, user_id: usize, dynamic: usize, query: String, extra: String) -> Element {
|
2022-12-09 00:15:04 +00:00
|
|
|
render! {
|
2023-05-24 00:09:24 +00:00
|
|
|
pre {
|
2023-05-31 17:11:11 +00:00
|
|
|
"Route1{{\n\tuser_id:{user_id},\n\tdynamic:{dynamic},\n\tquery:{query},\n\textra:{extra}\n}}"
|
2023-05-24 00:09:24 +00:00
|
|
|
}
|
2023-05-24 00:31:30 +00:00
|
|
|
Link {
|
2023-05-31 17:11:11 +00:00
|
|
|
target: Route::Route1 { user_id: *user_id, dynamic: *dynamic, query: String::new(), extra: extra.clone() + "." },
|
2023-05-24 00:09:24 +00:00
|
|
|
"Route1 with extra+\".\""
|
|
|
|
}
|
|
|
|
p { "Footer" }
|
2023-05-24 00:31:30 +00:00
|
|
|
Link {
|
2023-05-24 00:09:24 +00:00
|
|
|
target: Route::Route3 { dynamic: String::new() },
|
|
|
|
"Home"
|
2022-12-09 03:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
#[inline_props]
|
|
|
|
fn Route2(cx: Scope, user_id: usize) -> Element {
|
2022-12-09 03:11:31 +00:00
|
|
|
render! {
|
2023-05-24 00:09:24 +00:00
|
|
|
pre {
|
|
|
|
"Route2{{\n\tuser_id:{user_id}\n}}"
|
|
|
|
}
|
|
|
|
(0..*user_id).map(|i| rsx!{ p { "{i}" } }),
|
|
|
|
p { "Footer" }
|
2023-05-24 00:31:30 +00:00
|
|
|
Link {
|
2023-05-24 00:09:24 +00:00
|
|
|
target: Route::Route3 { dynamic: String::new() },
|
|
|
|
"Home"
|
2022-12-09 03:11:31 +00:00
|
|
|
}
|
2022-12-09 00:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 17:07:04 +00:00
|
|
|
|
2023-05-24 00:09:24 +00:00
|
|
|
#[inline_props]
|
|
|
|
fn Route3(cx: Scope, dynamic: String) -> Element {
|
|
|
|
let router = use_router(cx);
|
|
|
|
let router_route = router.current();
|
|
|
|
let current_route = use_ref(cx, String::new);
|
|
|
|
let parsed = Route::from_str(¤t_route.read());
|
|
|
|
|
|
|
|
let site_map = Route::SITE_MAP
|
|
|
|
.iter()
|
|
|
|
.flat_map(|seg| seg.flatten().into_iter())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-12-14 17:07:04 +00:00
|
|
|
render! {
|
2023-05-24 00:09:24 +00:00
|
|
|
input {
|
|
|
|
oninput: move |evt| {
|
|
|
|
*current_route.write() = evt.value.clone();
|
|
|
|
},
|
|
|
|
value: "{current_route.read()}"
|
|
|
|
}
|
|
|
|
"dynamic: {dynamic}"
|
2023-05-24 00:31:30 +00:00
|
|
|
Link {
|
2023-05-24 00:09:24 +00:00
|
|
|
target: Route::Route2 { user_id: 8888 },
|
|
|
|
"hello world link"
|
|
|
|
}
|
|
|
|
p { "Site Map" }
|
|
|
|
pre { "{site_map:#?}" }
|
|
|
|
p { "Dynamic link" }
|
|
|
|
if let Ok(route) = parsed {
|
|
|
|
if route != router_route {
|
|
|
|
render! {
|
2023-05-24 00:31:30 +00:00
|
|
|
Link {
|
2023-05-24 00:09:24 +00:00
|
|
|
target: route.clone(),
|
|
|
|
"{route}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 00:39:18 +00:00
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
2023-05-24 00:09:24 +00:00
|
|
|
}
|
2022-12-14 17:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 00:09:24 +00:00
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Routable)]
|
|
|
|
enum Route {
|
|
|
|
// Nests with parameters have types taken from child routes
|
|
|
|
#[nest("/user/:user_id")]
|
|
|
|
// Everything inside the nest has the added parameter `user_id: String`
|
|
|
|
// UserFrame is a layout component that will receive the `user_id: String` parameter
|
|
|
|
#[layout(UserFrame)]
|
2023-05-31 17:11:11 +00:00
|
|
|
// Route1 is a non-layout component that will receive the `user_id: String` and `dynamic: String` parameters
|
|
|
|
#[route("/:dynamic?:query", Route1)]
|
2023-05-24 00:09:24 +00:00
|
|
|
Route1 {
|
|
|
|
// The type is taken from the first instance of the dynamic parameter
|
|
|
|
user_id: usize,
|
|
|
|
dynamic: usize,
|
2023-05-31 17:11:11 +00:00
|
|
|
query: String,
|
2023-05-24 00:09:24 +00:00
|
|
|
extra: String,
|
|
|
|
},
|
|
|
|
// Route2 is a non-layout component that will receive the `user_id: String` parameter
|
|
|
|
#[route("/hello_world", Route2)]
|
|
|
|
// You can opt out of the layout by using the `!` prefix
|
|
|
|
#[layout(!UserFrame)]
|
|
|
|
Route2 { user_id: usize },
|
|
|
|
#[end_layout]
|
|
|
|
#[end_nest]
|
2023-05-31 17:11:11 +00:00
|
|
|
#[redirect("/:id/user", |id: usize| Route::Route3 { dynamic: id.to_string()})]
|
2023-05-24 00:09:24 +00:00
|
|
|
#[route("/:dynamic", Route3)]
|
|
|
|
Route3 { dynamic: String },
|
|
|
|
}
|