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.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);
};
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 std::sync::Arc;
use wasm_bindgen::prelude::Closure;
@@ -100,34 +100,48 @@ impl Auth {
});
ui.horizontal(|ui| {
if ui.button("Login").clicked() {
let state = self.auth_state.clone();
let (show_spinner, err_text) = match *self.auth_state.lock() {
AuthState::Error(ref e) => {
// self.login_button(ui);
(false, e.clone())
}
AuthState::RequestSent => (true, "".into()),
_ => (false, "".into()),
};
crate::httpPost(
"/auth",
&json!({
"username": self.username.clone(),
"password": self.password.clone()
})
.to_string(),
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)
if !show_spinner {
if ui.button("Login").clicked() {
let state = self.auth_state.clone();
crate::httpPost(
"/auth",
&json!({
"username": self.username.clone(),
"password": self.password.clone()
})
.to_string(),
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 {
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);
});
});
});