From b321528fcda6be894d3ad8fa6ae19c2e99b44639 Mon Sep 17 00:00:00 2001 From: Michael Mikovsky <77305074+Astatin3@users.noreply.github.com> Date: Mon, 1 Dec 2025 09:03:17 -0700 Subject: [PATCH] Add spinner to login page --- unshell-gui/assets/sw.js | 2 ++ unshell-gui/src/auth/mod.rs | 60 +++++++++++++++++++++++-------------- unshell-server/src/lib.rs | 2 -- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/unshell-gui/assets/sw.js b/unshell-gui/assets/sw.js index eaa1025..09b39e7 100644 --- a/unshell-gui/assets/sw.js +++ b/unshell-gui/assets/sw.js @@ -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; diff --git a/unshell-gui/src/auth/mod.rs b/unshell-gui/src/auth/mod.rs index 1137229..fb5fb17 100644 --- a/unshell-gui/src/auth/mod.rs +++ b/unshell-gui/src/auth/mod.rs @@ -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::(&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::(&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); }); }); }); diff --git a/unshell-server/src/lib.rs b/unshell-server/src/lib.rs index b9ddaf5..3f25ad4 100644 --- a/unshell-server/src/lib.rs +++ b/unshell-server/src/lib.rs @@ -1,6 +1,4 @@ // #![macro_use] -#[cfg(feature = "run")] mod api; -#[cfg(feature = "run")] pub use api::app::start_api;