anyhow used

This commit is contained in:
filipriec
2025-04-18 19:04:05 +02:00
parent 09ccad2bd4
commit 5a029283a1
12 changed files with 69 additions and 74 deletions

View File

@@ -9,6 +9,7 @@ use crate::state::pages::canvas_state::CanvasState;
use crate::ui::handlers::context::DialogPurpose;
use crate::modes::handlers::event::EventOutcome;
use common::proto::multieko2::auth::LoginResponse;
use anyhow::{Context, Result};
#[derive(Debug)]
pub enum LoginResult {
@@ -19,13 +20,12 @@ pub enum LoginResult {
/// Attempts to log the user in using the provided credentials via gRPC.
/// Updates AuthState and AppState on success or failure.
/// (This is your existing function - remains unchanged)
pub async fn save(
auth_state: &mut AuthState,
login_state: &mut LoginState,
auth_client: &mut AuthClient,
app_state: &mut AppState,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<String> {
let identifier = login_state.username.clone();
let password = login_state.password.clone();
@@ -40,7 +40,7 @@ pub async fn save(
DialogPurpose::LoginFailed,
);
login_state.error_message = Some(error_message.clone());
return Ok(error_message);
return Err(anyhow::anyhow!(error_message));
}
// Clear previous error/dialog state before attempting
@@ -48,7 +48,9 @@ pub async fn save(
app_state.hide_dialog(); // Hide any previous dialog
// Call the gRPC login method
match auth_client.login(identifier, password).await {
match auth_client.login(identifier.clone(), password).await
.with_context(|| format!("gRPC login attempt failed for identifier: {}", identifier))
{
Ok(response) => {
// Store authentication details using correct field names
auth_state.auth_token = Some(response.access_token.clone());
@@ -92,28 +94,11 @@ pub async fn save(
login_state.set_has_unsaved_changes(true);
login_state.username.clear();
login_state.password.clear();
Ok(format!("Login failed: {}", error_message))
Err(e)
}
}
}
// --- Add this new function ---
/// Sets the stage for login: shows loading dialog and sets the pending flag.
/// Call this from the event handler when login is triggered.
pub async fn initiate_login(
app_state: &mut AppState,
login_state: &mut LoginState,
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
// Show the loading dialog immediately
app_state.show_loading_dialog("Logging In", "Please wait...");
// Set the flag in LoginState to indicate the actual save should run next loop
login_state.login_request_pending = true;
// Return immediately to allow redraw
Ok(EventOutcome::Ok("Login initiated.".to_string()))
}
// --- End of new function ---
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
pub async fn revert(
login_state: &mut LoginState,