create redirects

This commit is contained in:
Evan Almloff committed 2023-05-31 12:11:11 -05:00
1 parent 87794b5039
commit 58b74c1155
9 files changed
+307 -419

No files matched your search

+7 -5
View File
@@ -45,13 +45,13 @@ fn UserFrame(cx: Scope, user_id: usize) -> Element {
}
#[inline_props]
fn Route1(cx: Scope, user_id: usize, dynamic: usize, extra: String) -> Element {
fn Route1(cx: Scope, user_id: usize, dynamic: usize, query: String, extra: String) -> Element {
render! {
pre {
"Route1{{\n\tuser_id:{user_id},\n\tdynamic:{dynamic},\n\textra:{extra}\n}}"
"Route1{{\n\tuser_id:{user_id},\n\tdynamic:{dynamic},\n\tquery:{query},\n\textra:{extra}\n}}"
}
Link {
target: Route::Route1 { user_id: *user_id, dynamic: *dynamic, extra: extra.clone() + "." },
target: Route::Route1 { user_id: *user_id, dynamic: *dynamic, query: String::new(), extra: extra.clone() + "." },
"Route1 with extra+\".\""
}
p { "Footer" }
@@ -131,12 +131,13 @@ enum Route {
// 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)]
// Route1 is a non-layout component that will receive the `user_id: String` and `dynamic: String` parameters
#[route("/:dynamic", Route1)]
// Route1 is a non-layout component that will receive the `user_id: String` and `dynamic: String` parameters
#[route("/:dynamic?:query", Route1)]
Route1 {
// The type is taken from the first instance of the dynamic parameter
user_id: usize,
dynamic: usize,
query: String,
extra: String,
},
// Route2 is a non-layout component that will receive the `user_id: String` parameter
@@ -146,6 +147,7 @@ enum Route {
Route2 { user_id: usize },
#[end_layout]
#[end_nest]
#[redirect("/:id/user", |id: usize| Route::Route3 { dynamic: id.to_string()})]
#[route("/:dynamic", Route3)]
Route3 { dynamic: String },
}
+6 -6
View File
@@ -12,7 +12,7 @@ use super::{
HistoryProvider,
};
fn update_scroll<R: Serialize + DeserializeOwned>(window: &Window, history: &History) {
fn update_scroll<R: Serialize + DeserializeOwned + Routable>(window: &Window, history: &History) {
if let Some(WebHistoryState { state, .. }) = get_current::<WebHistoryState<R>>(history) {
let scroll = ScrollPosition::of_window(window);
let state = WebHistoryState { state, scroll };
@@ -42,7 +42,7 @@ struct WebHistoryState<R> {
/// in the URL. Otherwise, if a router navigation is triggered, the prefix will be added.
///
/// [History API]: https://developer.mozilla.org/en-US/docs/Web/API/History_API
pub struct WebHistory<R: Serialize + DeserializeOwned> {
pub struct WebHistory<R: Serialize + DeserializeOwned + Routable> {
do_scroll_restoration: bool,
history: History,
listener_navigation: Option<EventListener>,
@@ -102,10 +102,10 @@ impl<R: Serialize + DeserializeOwned + Routable> WebHistory<R> {
phantom: Default::default(),
};
let state = myself.current_route();
let state = myself.create_state(state);
let _ = replace_state_with_url(&myself.history, &state, None);
let current_route = myself.current_route();
let current_url = current_route.to_string();
let state = myself.create_state(current_route);
let _ = replace_state_with_url(&myself.history, &state, Some(&current_url));
myself
}