From 1baf89dde6bc32b185c38efa1d95ff729d339002 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 5 Apr 2025 17:52:37 +0200 Subject: [PATCH] login with dialog menu --- client/src/tui/functions/common/login.rs | 56 +++++++++++++++++++----- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/client/src/tui/functions/common/login.rs b/client/src/tui/functions/common/login.rs index 5e72045..986266c 100644 --- a/client/src/tui/functions/common/login.rs +++ b/client/src/tui/functions/common/login.rs @@ -14,29 +14,61 @@ pub async fn save( let identifier = auth_state.username.clone(); let password = auth_state.password.clone(); + // Clear previous error/dialog state before attempting + auth_state.error_message = None; + app_state.ui.dialog.show_dialog = false; + // Call the gRPC login method match auth_client.login(identifier, password).await { Ok(response) => { // Store authentication details on success - auth_state.auth_token = Some(response.access_token); - auth_state.user_id = Some(response.user_id); - auth_state.role = Some(response.role); - auth_state.error_message = None; + auth_state.auth_token = Some(response.access_token.clone()); // Clone response fields + auth_state.user_id = Some(response.user_id.clone()); + auth_state.role = Some(response.role.clone()); auth_state.set_has_unsaved_changes(false); // Mark as "saved" - // Update app state to transition from login to the main form view - app_state.ui.show_login = false; - app_state.ui.show_form = true; // Assuming form is the next view + // Format the successful response for the dialog + // Note: Long tokens might make the dialog wide. Consider truncating if needed. + let success_message = format!( + "Login Successful!\n\n\ + Access Token: {}\n\ + Token Type: {}\n\ + Expires In: {}\n\ + User ID: {}\n\ + Role: {}", + response.access_token, + response.token_type, + response.expires_in, // Ensure this type implements Display or use .to_string() + response.user_id, + response.role + ); - Ok("Login successful!".to_string()) + // Configure and show the success dialog + app_state.ui.dialog.dialog_title = "Login Success".to_string(); + app_state.ui.dialog.dialog_message = success_message; + app_state.ui.dialog.show_dialog = true; + app_state.ui.dialog.dialog_button_active = true; // Make OK button active + + // Return a simple success indicator; the dialog shows details + Ok("Login successful, details shown in dialog.".to_string()) } Err(e) => { - // Store error message on failure - let error_message = format!("Login failed: {}", e); - auth_state.error_message = Some(error_message.clone()); + // Format the error message for the dialog + let error_message = format!("{}", e); + + // Configure and show the error dialog + app_state.ui.dialog.dialog_title = "Login Failed".to_string(); + app_state.ui.dialog.dialog_message = error_message.clone(); // Clone for return + app_state.ui.dialog.show_dialog = true; + app_state.ui.dialog.dialog_button_active = true; // Make OK button active + // Keep unsaved changes true if login fails, allowing retry/revert auth_state.set_has_unsaved_changes(true); - Ok(error_message) // Return error message to display + + // Return the error message; the dialog also shows it + // Using Ok here because the 'save' operation itself didn't panic, + // even though the underlying login failed. + Ok(format!("Login failed: {}", error_message)) } } }