time for changing it all

This commit is contained in:
filipriec
2025-04-18 18:11:12 +02:00
parent bdcc10bd40
commit 09ccad2bd4
4 changed files with 125 additions and 55 deletions

View File

@@ -8,6 +8,7 @@ use crate::ui::handlers::rat_state::UiStateHandler;
use crate::ui::handlers::context::UiContext;
use crate::ui::handlers::context::DialogPurpose;
use crate::functions::common::buffer;
use std::error::Error;
use crate::tui::{
terminal::core::TerminalCore,
functions::{
@@ -41,6 +42,9 @@ use crate::modes::{
};
use crate::functions::modes::navigation::{admin_nav, add_table_nav};
use crate::config::binds::key_sequences::KeySequenceTracker;
use tokio::spawn;
use tokio::sync::mpsc;
use crate::tui::functions::common::login::LoginResult;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventOutcome {
@@ -60,10 +64,11 @@ pub struct EventHandler {
pub ideal_cursor_column: usize,
pub key_sequence_tracker: KeySequenceTracker,
pub auth_client: AuthClient,
pub login_result_sender: mpsc::Sender<LoginResult>,
}
impl EventHandler {
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
pub async fn new(login_result_sender: mpsc::Sender<LoginResult>) -> Result<Self, Box<dyn Error + Send + Sync>> {
Ok(EventHandler {
command_mode: false,
command_input: String::new(),
@@ -74,6 +79,7 @@ impl EventHandler {
ideal_cursor_column: 0,
key_sequence_tracker: KeySequenceTracker::new(800),
auth_client: AuthClient::new().await?,
login_result_sender,
})
}
@@ -222,14 +228,47 @@ impl EventHandler {
}
UiContext::Login => {
let login_action_message = match index {
0 => { // Index 0 corresponds to the "Login" button
match login::initiate_login(app_state, login_state).await {
Ok(outcome) => return Ok(outcome),
Err(e) => {
app_state.show_dialog("Error", &format!("Failed to initiate login: {}", e), vec!["OK".to_string()], DialogPurpose::LoginFailed);
login_state.login_request_pending = false;
"Error initiating login".to_string()
}
0 => { // "Login" button pressed
let username = login_state.username.clone();
let password = login_state.password.clone();
// 1. Client-side validation
if username.trim().is_empty() {
app_state.show_dialog(
"Login Failed",
"Username/Email cannot be empty.",
vec!["OK".to_string()],
DialogPurpose::LoginFailed,
);
// Return a message, no need to modify login_state here
// as it will be cleared when result is processed
"Username cannot be empty.".to_string()
} else {
// 2. Show Loading Dialog
app_state.show_loading_dialog("Logging In", "Please wait...");
// 3. Clone sender for the task (needs sender from ui.rs)
// NOTE: We need access to login_result_sender here.
// This requires passing it into EventHandler or handle_event.
// Let's assume it's added to EventHandler state for now.
let sender = self.login_result_sender.clone(); // Assumes sender is part of EventHandler state
// 4. Spawn the login task
spawn(async move {
let login_outcome = match AuthClient::new().await {
Ok(mut auth_client) => {
match auth_client.login(username, password).await {
Ok(response) => login::LoginResult::Success(response),
Err(e) => login::LoginResult::Failure(format!("{}", e)),
}
}
Err(e) => login::LoginResult::ConnectionError(format!("Failed to create AuthClient: {}", e)),
};
let _ = sender.send(login_outcome).await; // Handle error?
});
// 5. Return immediately
"Login initiated.".to_string()
}
},
1 => login::back_to_main(login_state, app_state, buffer_state).await,