Skip to content

Commit f7c7c28

Browse files
committed
fix(app): repair next_url machinery
1 parent ac82c70 commit f7c7c28

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

crates/site-app/src/components/navbar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use leptos::{either::Either, prelude::*};
44
use models::AuthUser;
55

66
use self::org_selector::OrgSelectorPopover;
7-
use crate::{hooks::OrgHook, navigation::next_url_hook};
7+
use crate::{hooks::OrgHook, navigation::next_url_encoded_hook};
88

99
#[component]
1010
pub fn Navbar() -> impl IntoView {
@@ -46,7 +46,7 @@ fn NavbarUserArea() -> impl IntoView {
4646

4747
#[component]
4848
fn LoggedOutUserAuthActions() -> impl IntoView {
49-
let next_url = next_url_hook();
49+
let next_url = next_url_encoded_hook();
5050
let signup_url =
5151
Signal::derive(move || format!("/auth/signup?next={}", next_url()));
5252
let login_url =

crates/site-app/src/hooks/login_hook.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use leptos::{ev::Event, prelude::*};
44
use models::dvf::{EmailAddress, EmailAddressError};
55

66
use crate::{
7-
navigation::{navigate_to, next_url_hook},
7+
navigation::{navigate_to, next_url_string_hook},
88
reactive_utils::touched_input_bindings,
99
};
1010

@@ -13,7 +13,7 @@ pub struct LoginHook {
1313
email_signal: RwSignal<String>,
1414
password_signal: RwSignal<String>,
1515
submit_touched_signal: RwSignal<bool>,
16-
next_url_memo: Memo<String>,
16+
next_url_memo: Signal<String>,
1717
action: Action<(), Result<bool, String>>,
1818
}
1919

@@ -22,7 +22,7 @@ impl LoginHook {
2222
let email_signal = RwSignal::new(String::new());
2323
let password_signal = RwSignal::new(String::new());
2424
let submit_touched_signal = RwSignal::new(false);
25-
let next_url_memo = next_url_hook();
25+
let next_url_memo = next_url_string_hook();
2626

2727
let action = Action::new_local(move |(): &()| {
2828
// json body for authenticate endpoint
@@ -140,6 +140,10 @@ impl LoginHook {
140140
pub fn create_redirect_effect(&self) -> Effect<LocalStorage> {
141141
let (action, next_url_memo) = (self.action, self.next_url_memo);
142142
Effect::new(move || {
143+
leptos::logging::log!(
144+
"redirect effect set to navigate to {:?}",
145+
next_url_memo()
146+
);
143147
if action.value().get() == Some(Ok(true)) {
144148
navigate_to(&next_url_memo());
145149
}

crates/site-app/src/hooks/signup_hook.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use leptos::{ev::Event, prelude::*};
44
use models::dvf::{EmailAddress, EmailAddressError, HumanName, HumanNameError};
55

66
use crate::{
7-
navigation::{navigate_to, next_url_hook},
7+
navigation::{navigate_to, next_url_string_hook},
88
reactive_utils::touched_input_bindings,
99
};
1010

@@ -14,7 +14,7 @@ pub struct SignupHook {
1414
password_signal: RwSignal<String>,
1515
confirm_password_signal: RwSignal<String>,
1616
submit_touched_signal: RwSignal<bool>,
17-
next_url_memo: Memo<String>,
17+
next_url_memo: Signal<String>,
1818
action: Action<(), Result<bool, String>>,
1919
}
2020

@@ -25,7 +25,7 @@ impl SignupHook {
2525
let password_signal = RwSignal::new(String::new());
2626
let confirm_password_signal = RwSignal::new(String::new());
2727
let submit_touched_signal = RwSignal::new(false);
28-
let next_url_memo = next_url_hook();
28+
let next_url_memo = next_url_string_hook();
2929

3030
let action = Action::new_local(move |(): &()| {
3131
// json body for authenticate endpoint
@@ -203,6 +203,10 @@ impl SignupHook {
203203

204204
pub fn create_redirect_effect(&self) -> Effect<LocalStorage> {
205205
let (action, next_url_memo) = (self.action, self.next_url_memo);
206+
leptos::logging::log!(
207+
"redirect effect set to navigate to {:?}",
208+
next_url_memo()
209+
);
206210
Effect::new(move || {
207211
if action.value().get() == Some(Ok(true)) {
208212
navigate_to(&next_url_memo());

crates/site-app/src/navigation.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,34 @@ pub fn navigate_to(path: &str) {
4646
}
4747

4848
/// Gets the next URL if it's already set or sets it to the current page.
49-
#[allow(unused_variables)]
50-
pub fn next_url_hook() -> Memo<String> {
49+
pub fn next_url_string_hook() -> Signal<String> {
50+
#[cfg(not(feature = "ssr"))]
51+
use leptos_router::location::LocationProvider;
52+
5153
#[cfg(feature = "ssr")]
5254
let current_url = leptos_router::hooks::use_url()();
53-
#[cfg(feature = "hydrate")]
54-
let current_url = <leptos_router::location::BrowserUrl as leptos_router::location::LocationProvider>::current().unwrap();
55+
#[cfg(not(feature = "ssr"))]
56+
let current_url = leptos_router::location::BrowserUrl::current()
57+
.expect("failed to get current browser url");
58+
59+
leptos::logging::log!("current_url = {current_url:?}");
5560

56-
// set it to the existing next url or the current URL escaped
57-
Memo::new(move |_| {
61+
Signal::stored(
5862
current_url
5963
.search_params()
64+
.clone()
6065
.get("next")
61-
.unwrap_or(Url::escape(&url_to_full_path(&current_url)))
66+
.unwrap_or(url_to_full_path(&current_url)),
67+
)
68+
}
69+
70+
/// Url-enccodes the next URL if it's already set or sets it to the current
71+
/// page. Suitable for propagating the next URL by setting the param to this
72+
/// value in links.
73+
pub fn next_url_encoded_hook() -> Signal<String> {
74+
let next_url = next_url_string_hook();
75+
Signal::derive(move || {
76+
let next_url = next_url();
77+
Url::escape(&next_url)
6278
})
6379
}

0 commit comments

Comments
 (0)