Switch to Cows in login page to store both &'static str and String

This commit is contained in:
pjht 2024-10-19 10:45:13 -05:00
parent 415236a53c
commit eee42be44b
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
3 changed files with 12 additions and 10 deletions

View File

@ -185,7 +185,7 @@ impl Component for ChatPage {
.push_with_query(
&Route::Login,
&LoginQuery {
redirect: "/chat".to_string(),
redirect: "/chat".into(),
},
)
.unwrap();

View File

@ -22,7 +22,7 @@ pub fn Home() -> Html {
.push_with_query(
&Route::Login,
&LoginQuery {
redirect: "/".to_string(),
redirect: "/".into(),
},
)
.unwrap();

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use crate::components::Nav;
use common::{LoggedInResponse, LoginRequest, LoginResponse};
use gloo::{
@ -12,13 +14,13 @@ use yew_hooks::use_mount;
#[derive(Serialize, Deserialize)]
pub struct LoginQuery {
pub redirect: String,
pub redirect: Cow<'static, str>,
}
impl Default for LoginQuery {
fn default() -> Self {
Self {
redirect: "/".to_string(),
redirect: "/".into(),
}
}
}
@ -29,7 +31,7 @@ pub fn Login() -> Html {
let username_ref = use_node_ref();
let password_ref = use_node_ref();
let disable_input = use_state(|| false);
let login_error = use_state(|| None);
let login_error = use_state(|| Option::<Cow<'static, str>>::None);
let query_params = BrowserHistory::new()
.location()
.query::<LoginQuery>()
@ -67,7 +69,7 @@ pub fn Login() -> Html {
Ok(val) => val,
Err(err) => {
error!(format!("Could not send login request to server: {err}"));
login_error.set(Some("Error logging in".to_string()));
login_error.set(Some("Error logging in".into()));
disable_input.set(false);
return;
}
@ -76,15 +78,15 @@ pub fn Login() -> Html {
Ok(val) => val,
Err(err) => {
error!(format!("Could not parse login response: {err}"));
login_error.set(Some("Error logging in".to_string()));
login_error.set(Some("Error logging in".into()));
disable_input.set(false);
return;
}
};
match resp {
Ok(_) => BrowserHistory::new().push(&redirect),
Ok(_) => BrowserHistory::new().push(redirect),
Err(err) => {
login_error.set(Some(err));
login_error.set(Some(err.into()));
password_ref
.cast::<HtmlInputElement>()
.unwrap()
@ -121,7 +123,7 @@ pub fn Login() -> Html {
.await
.unwrap();
if resp.logged_in {
BrowserHistory::new().push(&redirect);
BrowserHistory::new().push(redirect);
}
})
});