Add spinner to login page

This commit is contained in:
Michael Mikovsky
2025-12-01 09:03:17 -07:00
parent 22650e5668
commit b321528fcd
3 changed files with 39 additions and 25 deletions
+2
View File
@@ -37,6 +37,8 @@ function startHttpRequest(callback) {
if (xmlHttp.readyState !== 4) return; if (xmlHttp.readyState !== 4) return;
if (xmlHttp.status == 200) callback(true, xmlHttp.responseText); if (xmlHttp.status == 200) callback(true, xmlHttp.responseText);
else if (xmlHttp.status == 401) callback(false, "Unauthorized");
else if (xmlHttp.status == 500) callback(false, "Internal Server Error");
else callback(false, xmlHttp.responseText); else callback(false, xmlHttp.responseText);
}; };
return xmlHttp; return xmlHttp;
+37 -23
View File
@@ -1,4 +1,4 @@
use egui::{Align2, Area, Frame, Order, Sense, UiKind, Vec2, mutex::Mutex}; use egui::{Align2, Area, Color32, Frame, Order, Sense, UiKind, Vec2, mutex::Mutex};
use serde_json::json; use serde_json::json;
use std::sync::Arc; use std::sync::Arc;
use wasm_bindgen::prelude::Closure; use wasm_bindgen::prelude::Closure;
@@ -100,34 +100,48 @@ impl Auth {
}); });
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button("Login").clicked() { let (show_spinner, err_text) = match *self.auth_state.lock() {
let state = self.auth_state.clone(); AuthState::Error(ref e) => {
// self.login_button(ui);
(false, e.clone())
}
AuthState::RequestSent => (true, "".into()),
_ => (false, "".into()),
};
crate::httpPost( if !show_spinner {
"/auth", if ui.button("Login").clicked() {
&json!({ let state = self.auth_state.clone();
"username": self.username.clone(),
"password": self.password.clone() crate::httpPost(
}) "/auth",
.to_string(), &json!({
Closure::once_into_js(move |ok: bool, response: String| { "username": self.username.clone(),
*(state.lock()) = if ok { "password": self.password.clone()
if let Ok(token) = serde_json::from_str::<Token>(&response) })
{ .to_string(),
AuthState::Authorised(token) Closure::once_into_js(move |ok: bool, response: String| {
*(state.lock()) = if ok {
if let Ok(token) =
serde_json::from_str::<Token>(&response)
{
AuthState::Authorised(token)
} else {
AuthState::Error("Malformed Response".into())
}
} else { } else {
AuthState::Error("Malformed Response".into()) AuthState::Error(response)
} }
} else { }),
AuthState::Error(response) );
}
}),
);
*(self.auth_state.lock()) = AuthState::RequestSent; *(self.auth_state.lock()) = AuthState::RequestSent;
}
} else {
ui.spinner();
} }
ui.label(format!("{:?}", self.auth_state.lock())); ui.colored_label(Color32::RED, err_text);
}); });
}); });
}); });
-2
View File
@@ -1,6 +1,4 @@
// #![macro_use] // #![macro_use]
#[cfg(feature = "run")]
mod api; mod api;
#[cfg(feature = "run")]
pub use api::app::start_api; pub use api::app::start_api;