Compare commits

...

6 Commits

Author SHA1 Message Date
filipriec
060cff3f0f compiled 2025-03-31 17:10:51 +02:00
filipriec
07c48985e4 proper pass of the arguments 2025-03-31 16:25:11 +02:00
filipriec
3d266c2ad0 switching of the views done 2025-03-31 16:23:25 +02:00
filipriec
e2c326bf1e login gRPC 2025-03-31 16:16:07 +02:00
filipriec
7f5b671084 switch between login or form in the save request 2025-03-31 15:55:54 +02:00
filipriec
3ccf71ed0f switcher, have bugs 2025-03-31 15:42:48 +02:00
5 changed files with 105 additions and 30 deletions

View File

@@ -1,16 +1,21 @@
// src/modes/canvas/common.rs
use crate::tui::terminal::core::TerminalCore;
use crate::state::pages::form::FormState;
use crate::state::pages::{form::FormState, auth::AuthState};
use crate::state::state::AppState;
use crate::services::grpc_client::GrpcClient;
use crate::tui::functions::common::form::{save, revert};
use crate::services::auth::AuthClient;
use crate::tui::functions::common::{
form::{save as form_save, revert},
login::{save as login_save, cancel}
};
/// Main handler for common core actions
pub async fn handle_core_action(
action: &str,
form_state: &mut FormState,
auth_state: &mut AuthState,
grpc_client: &mut GrpcClient,
auth_client: &mut AuthClient,
terminal: &mut TerminalCore,
app_state: &mut AppState,
current_position: &mut u64,
@@ -18,38 +23,52 @@ pub async fn handle_core_action(
) -> Result<(bool, String), Box<dyn std::error::Error>> {
match action {
"save" => {
let message = save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
if app_state.ui.show_login {
let message = login_save(auth_state, auth_client, app_state).await?;
Ok((false, message))
} else {
let message = form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
}
},
"force_quit" => {
terminal.cleanup()?;
Ok((true, "Force exiting without saving.".to_string()))
},
"save_and_quit" => {
let message = save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
let message = if app_state.ui.show_login {
login_save(auth_state, auth_client, app_state).await?
} else {
form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?
};
terminal.cleanup()?;
Ok((true, format!("{}. Exiting application.", message)))
},
"revert" => {
let message = revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
Ok((false, message))
if app_state.ui.show_login {
let message = cancel(auth_state, app_state).await;
Ok((false, message))
} else {
let message = revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
Ok((false, message))
}
},
_ => Ok((false, format!("Core action not handled: {}", action))),
}

View File

@@ -5,6 +5,7 @@ use crate::tui::terminal::{
core::TerminalCore,
};
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::tui::functions::common::commands::CommandHandler;
use crate::config::binds::config::Config;
use crate::state::pages::form::FormState;
@@ -27,11 +28,12 @@ pub struct EventHandler {
pub ideal_cursor_column: usize,
pub key_sequence_tracker: KeySequenceTracker,
pub auth_state: AuthState,
pub auth_client: AuthClient,
}
impl EventHandler {
pub fn new() -> Self {
EventHandler {
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
Ok(EventHandler {
command_mode: false,
command_input: String::new(),
command_message: String::new(),
@@ -40,7 +42,8 @@ impl EventHandler {
ideal_cursor_column: 0,
key_sequence_tracker: KeySequenceTracker::new(800),
auth_state: AuthState::new(),
}
auth_client: AuthClient::new().await?,
})
}
pub async fn handle_event(
@@ -131,7 +134,9 @@ impl EventHandler {
return common::handle_core_action(
action,
form_state,
&mut self.auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
@@ -191,7 +196,9 @@ impl EventHandler {
return common::handle_core_action(
action,
form_state,
&mut self.auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,

View File

@@ -1,6 +1,8 @@
// src/tui/functions/common.rs
pub mod commands;
pub mod form;
pub mod login;
pub use commands::*;
pub use form::*;
pub use form::{revert, save as form_save};
pub use login::{cancel, save as login_save};

View File

@@ -0,0 +1,45 @@
// src/tui/functions/common/login.rs
use crate::services::auth::AuthClient;
use crate::state::pages::auth::AuthState;
use crate::state::state::AppState;
pub async fn save(
auth_state: &mut AuthState,
auth_client: &mut AuthClient,
app_state: &mut AppState,
) -> Result<String, Box<dyn std::error::Error>> {
let identifier = auth_state.username.clone();
let password = auth_state.password.clone();
match auth_client.login(identifier, password).await {
Ok(response) => {
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;
// Update app state to show main interface
app_state.ui.show_login = false;
app_state.ui.show_form = true;
Ok("Login successful!".to_string())
}
Err(e) => {
let error_message = format!("Login failed: {}", e);
auth_state.error_message = Some(error_message.clone());
Ok(error_message)
}
}
}
pub async fn cancel(
auth_state: &mut AuthState,
app_state: &mut AppState,
) -> String {
auth_state.username.clear();
auth_state.password.clear();
auth_state.error_message = None;
app_state.ui.show_login = false;
app_state.ui.show_intro = true;
"Login canceled".to_string()
}

View File

@@ -2,6 +2,7 @@
use crate::tui::terminal::TerminalCore;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::services::ui_service::UiService; // Add this import
use crate::tui::terminal::EventReader;
use crate::tui::functions::common::CommandHandler;
@@ -17,6 +18,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
let mut terminal = TerminalCore::new()?;
let mut grpc_client = GrpcClient::new().await?;
let mut auth_client = AuthClient::new().await?;
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut auth_state = AuthState::default();
@@ -31,7 +33,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut form_state = FormState::new(column_names);
// The rest of your UI initialization remains the same
let mut event_handler = EventHandler::new();
let mut event_handler = EventHandler::new().await?;
let event_reader = EventReader::new();
// Fetch the total count of Adresar entries