From d7d7fd614bcb0b81ec5457ccfaa2b90f751de640 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 7 Apr 2025 22:46:00 +0200 Subject: [PATCH] fixing more errors, last to go --- client/src/functions/modes/edit/auth_e.rs | 11 ++++----- client/src/functions/modes/edit/form_e.rs | 1 - client/src/modes/canvas/edit.rs | 7 ------ client/src/modes/common/commands.rs | 29 ++++++++++++++++------- client/src/modes/general/navigation.rs | 25 +++++++++---------- client/src/modes/handlers/event.rs | 2 +- client/src/state/state.rs | 2 -- 7 files changed, 40 insertions(+), 37 deletions(-) diff --git a/client/src/functions/modes/edit/auth_e.rs b/client/src/functions/modes/edit/auth_e.rs index 2becc47..ece6811 100644 --- a/client/src/functions/modes/edit/auth_e.rs +++ b/client/src/functions/modes/edit/auth_e.rs @@ -2,7 +2,7 @@ use crate::services::grpc_client::GrpcClient; use crate::state::canvas_state::CanvasState; -use crate::state::pages::form::FormState; +use crate::state::pages::{auth::AuthState, form::FormState}; use crate::tui::functions::common::form::{revert, save}; use crossterm::event::{KeyCode, KeyEvent}; use std::any::Any; @@ -11,7 +11,6 @@ pub async fn execute_common_action( action: &str, state: &mut S, grpc_client: &mut GrpcClient, - is_saved: &mut bool, current_position: &mut u64, total_count: u64, ) -> Result> { @@ -25,14 +24,15 @@ pub async fn execute_common_action( { match action { "save" => { - save( + let outcome = save( form_state, grpc_client, - is_saved, current_position, total_count, ) - .await + .await?; + let message = format!("Save successful: {:?}", outcome); // Simple message for now + Ok(message) } "revert" => { revert( @@ -62,7 +62,6 @@ pub async fn execute_edit_action( state: &mut S, ideal_cursor_column: &mut usize, grpc_client: &mut GrpcClient, - is_saved: &mut bool, current_position: &mut u64, total_count: u64, ) -> Result> { diff --git a/client/src/functions/modes/edit/form_e.rs b/client/src/functions/modes/edit/form_e.rs index 9a54397..bd9dc48 100644 --- a/client/src/functions/modes/edit/form_e.rs +++ b/client/src/functions/modes/edit/form_e.rs @@ -77,7 +77,6 @@ pub async fn execute_edit_action( state: &mut S, ideal_cursor_column: &mut usize, grpc_client: &mut GrpcClient, - is_saved: &mut bool, current_position: &mut u64, total_count: u64, ) -> Result> { diff --git a/client/src/modes/canvas/edit.rs b/client/src/modes/canvas/edit.rs index 4c249ee..6274f51 100644 --- a/client/src/modes/canvas/edit.rs +++ b/client/src/modes/canvas/edit.rs @@ -14,7 +14,6 @@ pub async fn handle_edit_event( auth_state: &mut AuthState, ideal_cursor_column: &mut usize, command_message: &mut String, - is_saved: &mut bool, current_position: &mut u64, total_count: u64, grpc_client: &mut GrpcClient, @@ -42,7 +41,6 @@ pub async fn handle_edit_event( action, auth_state, // Concrete AuthState grpc_client, - is_saved, current_position, total_count ).await @@ -51,7 +49,6 @@ pub async fn handle_edit_event( action, form_state, // Concrete FormState grpc_client, - is_saved, current_position, total_count ).await @@ -68,7 +65,6 @@ pub async fn handle_edit_event( auth_state, // Full access to AuthState fields ideal_cursor_column, grpc_client, - is_saved, current_position, total_count ).await @@ -79,7 +75,6 @@ pub async fn handle_edit_event( form_state, // Full access to FormState fields ideal_cursor_column, grpc_client, - is_saved, current_position, total_count ).await @@ -95,7 +90,6 @@ pub async fn handle_edit_event( auth_state, ideal_cursor_column, grpc_client, - is_saved, current_position, total_count ).await @@ -106,7 +100,6 @@ pub async fn handle_edit_event( form_state, ideal_cursor_column, grpc_client, - is_saved, current_position, total_count ).await diff --git a/client/src/modes/common/commands.rs b/client/src/modes/common/commands.rs index e12b145..ad212c3 100644 --- a/client/src/modes/common/commands.rs +++ b/client/src/modes/common/commands.rs @@ -1,22 +1,26 @@ -// src/tui/controls/commands.rs +// src/modes/common/commands.rs use crate::tui::terminal::core::TerminalCore; +use crate::state::state::AppState; +use crate::state::pages::{form::FormState, auth::AuthState}; +use crate::state::canvas_state::CanvasState; -pub struct CommandHandler { - pub is_saved: bool, -} +pub struct CommandHandler; impl CommandHandler { pub fn new() -> Self { - Self { is_saved: false } + Self } pub async fn handle_command( &mut self, action: &str, terminal: &mut TerminalCore, + app_state: &AppState, + form_state: &FormState, + auth_state: &AuthState, ) -> Result<(bool, String), Box> { match action { - "quit" => self.handle_quit(terminal).await, + "quit" => self.handle_quit(terminal, app_state, form_state, auth_state).await, "force_quit" => self.handle_force_quit(terminal).await, "save_and_quit" => self.handle_save_quit(terminal).await, _ => Ok((false, format!("Unknown command: {}", action))), @@ -26,8 +30,18 @@ impl CommandHandler { async fn handle_quit( &self, terminal: &mut TerminalCore, + app_state: &AppState, + form_state: &FormState, + auth_state: &AuthState, ) -> Result<(bool, String), Box> { - if self.is_saved { + // Use actual unsaved changes state instead of is_saved flag + let has_unsaved = if app_state.ui.show_login { + auth_state.has_unsaved_changes() + } else { + form_state.has_unsaved_changes + }; + + if !has_unsaved { terminal.cleanup()?; Ok((true, "Exiting.".into())) } else { @@ -47,7 +61,6 @@ impl CommandHandler { &mut self, terminal: &mut TerminalCore, ) -> Result<(bool, String), Box> { - self.is_saved = true; terminal.cleanup()?; Ok((true, "State saved. Exiting.".into())) } diff --git a/client/src/modes/general/navigation.rs b/client/src/modes/general/navigation.rs index 94752f3..528671d 100644 --- a/client/src/modes/general/navigation.rs +++ b/client/src/modes/general/navigation.rs @@ -7,6 +7,7 @@ use crate::state::pages::form::FormState; use crate::state::pages::auth::AuthState; use crate::state::canvas_state::CanvasState; use crate::tui::functions::{intro, admin}; +use crate::modes::handlers::event::EventOutcome; pub async fn handle_navigation_event( key: KeyEvent, @@ -17,51 +18,51 @@ pub async fn handle_navigation_event( command_mode: &mut bool, command_input: &mut String, command_message: &mut String, -) -> Result<(bool, String), Box> { +) -> Result> { if let Some(action) = config.get_general_action(key.code, key.modifiers) { match action { "move_up" => { move_up(app_state, auth_state); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } "move_down" => { move_down(app_state); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } "next_option" => { - next_option(app_state); // Intro has 2 options - return Ok((false, String::new())); + next_option(app_state); + return Ok(EventOutcome::Ok(String::new())); } "previous_option" => { previous_option(app_state); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } "select" => { select(app_state); - return Ok((false, "Selected".to_string())); + return Ok(EventOutcome::Ok("Selected".to_string())); } "toggle_sidebar" => { toggle_sidebar(app_state); - return Ok((false, format!("Sidebar {}", + return Ok(EventOutcome::Ok(format!("Sidebar {}", if app_state.ui.show_sidebar { "shown" } else { "hidden" } ))); } "next_field" => { next_field(form_state); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } "prev_field" => { prev_field(form_state); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } "enter_command_mode" => { handle_enter_command_mode(command_mode, command_input, command_message); - return Ok((false, String::new())); + return Ok(EventOutcome::Ok(String::new())); } _ => {} } } - Ok((false, String::new())) + Ok(EventOutcome::Ok(String::new())) } pub fn move_up(app_state: &mut AppState, auth_state: &mut AuthState) { diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index fcb95a1..0232c0b 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -164,7 +164,7 @@ impl EventHandler { } } - let message = read_only::handle_read_only_event( + let (_should_exit, message) = read_only::handle_read_only_event( app_state, key, config, diff --git a/client/src/state/state.rs b/client/src/state/state.rs index 84c3f41..d4893f9 100644 --- a/client/src/state/state.rs +++ b/client/src/state/state.rs @@ -15,7 +15,6 @@ pub struct DialogState { pub struct UiState { pub show_sidebar: bool, - pub is_saved: bool, pub show_intro: bool, pub show_admin: bool, pub show_form: bool, @@ -134,7 +133,6 @@ impl Default for UiState { fn default() -> Self { Self { show_sidebar: true, - is_saved: false, show_intro: true, show_admin: false, show_form: false,