working dialog now better
This commit is contained in:
@@ -3,6 +3,8 @@ use crate::services::auth::AuthClient;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::state::AppState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
// Remove unused import if CanvasState is not directly used here
|
||||
// use crate::state::canvas_state::CanvasState;
|
||||
|
||||
/// Attempts to log the user in using the provided credentials via gRPC.
|
||||
/// Updates AuthState and AppState on success or failure.
|
||||
@@ -16,19 +18,18 @@ pub async fn save(
|
||||
|
||||
// Clear previous error/dialog state before attempting
|
||||
auth_state.error_message = None;
|
||||
app_state.ui.dialog.show_dialog = false;
|
||||
// Use the helper to ensure dialog is hidden and cleared properly
|
||||
app_state.hide_dialog();
|
||||
|
||||
// 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.clone()); // Clone response fields
|
||||
auth_state.auth_token = Some(response.access_token.clone());
|
||||
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"
|
||||
auth_state.set_has_unsaved_changes(false);
|
||||
|
||||
// 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\
|
||||
@@ -38,36 +39,42 @@ pub async fn save(
|
||||
Role: {}",
|
||||
response.access_token,
|
||||
response.token_type,
|
||||
response.expires_in, // Ensure this type implements Display or use .to_string()
|
||||
response.expires_in,
|
||||
response.user_id,
|
||||
response.role
|
||||
);
|
||||
|
||||
// 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
|
||||
// Use the helper method to configure and show the dialog
|
||||
app_state.show_dialog(
|
||||
"Login Success",
|
||||
&success_message,
|
||||
vec!["OK".to_string()], // Pass buttons here
|
||||
);
|
||||
// REMOVE these lines:
|
||||
// app_state.ui.dialog.dialog_title = "Login Success".to_string();
|
||||
// app_state.ui.dialog.dialog_message = success_message;
|
||||
// app_state.ui.dialog.dialog_show = true;
|
||||
// app_state.ui.dialog.dialog_button_active = true;
|
||||
|
||||
// Return a simple success indicator; the dialog shows details
|
||||
Ok("Login successful, details shown in dialog.".to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
// 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
|
||||
// Use the helper method to configure and show the dialog
|
||||
app_state.show_dialog(
|
||||
"Login Failed",
|
||||
&error_message,
|
||||
vec!["OK".to_string()], // Pass buttons here
|
||||
);
|
||||
// REMOVE these lines:
|
||||
// app_state.ui.dialog.dialog_title = "Login Failed".to_string();
|
||||
// app_state.ui.dialog.dialog_message = error_message.clone();
|
||||
// app_state.ui.dialog.dialog_show = true;
|
||||
// app_state.ui.dialog.dialog_button_active = true;
|
||||
|
||||
// Keep unsaved changes true if login fails, allowing retry/revert
|
||||
auth_state.set_has_unsaved_changes(true);
|
||||
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
@@ -76,17 +83,20 @@ pub async fn save(
|
||||
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
||||
pub async fn revert(
|
||||
auth_state: &mut AuthState,
|
||||
app_state: &mut AppState,
|
||||
_app_state: &mut AppState, // Prefix unused variable
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
auth_state.username.clear();
|
||||
auth_state.password.clear();
|
||||
auth_state.error_message = None; // Clear any previous error
|
||||
auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes
|
||||
auth_state.error_message = None;
|
||||
auth_state.set_has_unsaved_changes(false);
|
||||
|
||||
// TODO REDIRECT is now disabled
|
||||
// app_state.ui.show_login = false;
|
||||
// app_state.ui.show_intro = true;
|
||||
// Ensure dialog is hidden if revert is called
|
||||
// _app_state.hide_dialog(); // Uncomment if needed
|
||||
|
||||
// Navigation logic (currently disabled in original code)
|
||||
// _app_state.ui.show_login = false;
|
||||
// _app_state.ui.show_intro = true;
|
||||
|
||||
"Login reverted".to_string()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user