From 41a0b8537611c20efae1bb417faf71064ccded3f Mon Sep 17 00:00:00 2001 From: Priec Date: Sat, 23 Aug 2025 00:16:07 +0200 Subject: [PATCH] forms page moved more2 --- client/src/modes/canvas/common_mode.rs | 2 +- client/src/modes/common/command_mode.rs | 3 +- client/src/modes/handlers/event.rs | 9 ++-- .../common/form.rs => pages/forms/logic.rs} | 46 ++++++++++++++++--- client/src/pages/forms/mod.rs | 2 + client/src/services/ui_service.rs | 2 +- client/src/tui/functions.rs | 2 - client/src/tui/functions/common.rs | 1 - client/src/tui/functions/form.rs | 44 ------------------ 9 files changed, 50 insertions(+), 61 deletions(-) rename client/src/{tui/functions/common/form.rs => pages/forms/logic.rs} (78%) delete mode 100644 client/src/tui/functions/form.rs diff --git a/client/src/modes/canvas/common_mode.rs b/client/src/modes/canvas/common_mode.rs index 388bec7..a80f7ea 100644 --- a/client/src/modes/canvas/common_mode.rs +++ b/client/src/modes/canvas/common_mode.rs @@ -6,7 +6,7 @@ use crate::state::app::state::AppState; use crate::services::grpc_client::GrpcClient; use crate::services::auth::AuthClient; use crate::modes::handlers::event::EventOutcome; -use crate::tui::functions::common::form::SaveOutcome; +crate::pages::forms::logic::SaveOutcome; use anyhow::{Context, Result}; use crate::tui::functions::common::{ form::{save as form_save, revert as form_revert}, diff --git a/client/src/modes/common/command_mode.rs b/client/src/modes/common/command_mode.rs index 9eb7c74..793d342 100644 --- a/client/src/modes/common/command_mode.rs +++ b/client/src/modes/common/command_mode.rs @@ -6,9 +6,8 @@ use crate::services::grpc_client::GrpcClient; use crate::state::app::state::AppState; use crate::modes::common::commands::CommandHandler; use crate::tui::terminal::core::TerminalCore; -use crate::tui::functions::common::form::{save, revert}; +use crate::pages::forms::logic::{save, revert ,SaveOutcome}; use crate::modes::handlers::event::EventOutcome; -use crate::tui::functions::common::form::SaveOutcome; use crate::pages::routing::{Router, Page}; use anyhow::Result; diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 5ced2e6..f631b79 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -29,13 +29,14 @@ use crate::state::{ intro::IntroState, }, }; +use crate::tui::common::{register, login}; use crate::pages::routing::{Router, Page}; use crate::pages::forms::FormState; +use crate::pages::forms::logic::{save, revert, SaveOutcome}; use crate::search::state::SearchState; use crate::tui::functions::common::login::LoginResult; use crate::tui::functions::common::register::RegisterResult; use crate::tui::{ - functions::common::{form::SaveOutcome, login, register}, terminal::core::TerminalCore, {admin, intro}, }; @@ -816,7 +817,7 @@ impl EventHandler { Ok(EventOutcome::Ok(message)) } else { let save_outcome = if let Page::Form(_) = &router.current { - crate::tui::functions::common::form::save( + save( app_state, &mut self.grpc_client, ) @@ -851,7 +852,7 @@ impl EventHandler { ) .await? } else { - let save_outcome = crate::tui::functions::common::form::save( + let save_outcome = save( app_state, &mut self.grpc_client, ).await?; @@ -882,7 +883,7 @@ impl EventHandler { .await } else { if let Page::Form(_) = &router.current { - crate::tui::functions::common::form::revert( + revert( app_state, &mut self.grpc_client, ) diff --git a/client/src/tui/functions/common/form.rs b/client/src/pages/forms/logic.rs similarity index 78% rename from client/src/tui/functions/common/form.rs rename to client/src/pages/forms/logic.rs index 8916d59..d4da864 100644 --- a/client/src/tui/functions/common/form.rs +++ b/client/src/pages/forms/logic.rs @@ -1,6 +1,7 @@ -// src/tui/functions/common/form.rs +// src/pages/forms/logic.rs use crate::services::grpc_client::GrpcClient; use crate::state::app::state::AppState; +use crate::pages::forms::FormState; use crate::utils::data_converter; use anyhow::{anyhow, Context, Result}; use std::collections::HashMap; @@ -21,7 +22,6 @@ pub async fn save( return Ok(SaveOutcome::NoChange); } - // Copy out what we need before dropping the mutable borrow let profile_name = fs.profile_name.clone(); let table_name = fs.table_name.clone(); let fields = fs.fields.clone(); @@ -75,7 +75,7 @@ pub async fn save( } else { if id == 0 { return Err(anyhow!( - "Cannot update record: ID is 0, but not classified as new entry." + "Cannot update record: ID is 0, but not classified as new entry." )); } let response = grpc_client @@ -106,7 +106,7 @@ pub async fn revert( if let Some(fs) = app_state.form_state_mut() { if fs.id == 0 || (fs.total_count > 0 && fs.current_position > fs.total_count) - || (fs.total_count == 0 && fs.current_position == 1) + || (fs.total_count == 0 && fs.current_position == 1) { let old_total_count = fs.total_count; fs.reset_to_empty(); @@ -136,8 +136,8 @@ pub async fn revert( ) .await .context(format!( - "Failed to get table data by position {} for table {}.{}", - fs.current_position, fs.profile_name, fs.table_name + "Failed to get table data by position {} for table {}.{}", + fs.current_position, fs.profile_name, fs.table_name ))?; fs.update_from_response(&response.data, fs.current_position); @@ -146,3 +146,37 @@ pub async fn revert( Ok("Nothing to revert".to_string()) } } + +pub async fn handle_action( + action: &str, + form_state: &mut FormState, + _grpc_client: &mut GrpcClient, + ideal_cursor_column: &mut usize, +) -> Result { + if form_state.has_unsaved_changes() { + return Ok( + "Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating." + .to_string(), + ); + } + + let total_count = form_state.total_count; + + match action { + "previous_entry" => { + if form_state.current_position > 1 { + form_state.current_position -= 1; + *ideal_cursor_column = 0; + } + } + "next_entry" => { + if form_state.current_position <= total_count { + form_state.current_position += 1; + *ideal_cursor_column = 0; + } + } + _ => return Err(anyhow!("Unknown form action: {}", action)), + } + + Ok(String::new()) +} diff --git a/client/src/pages/forms/mod.rs b/client/src/pages/forms/mod.rs index 52b7526..1afcdce 100644 --- a/client/src/pages/forms/mod.rs +++ b/client/src/pages/forms/mod.rs @@ -2,6 +2,8 @@ pub mod ui; pub mod state; +pub mod logic; pub use ui::*; pub use state::*; +pub use logic::*; diff --git a/client/src/services/ui_service.rs b/client/src/services/ui_service.rs index 13de71a..075de60 100644 --- a/client/src/services/ui_service.rs +++ b/client/src/services/ui_service.rs @@ -3,7 +3,7 @@ use crate::services::grpc_client::GrpcClient; use crate::state::app::state::AppState; use crate::state::pages::add_logic::AddLogicState; -use crate::tui::functions::common::form::SaveOutcome; +use crate::pages::forms::logic::SaveOutcome; use crate::utils::columns::filter_user_columns; use crate::pages::forms::{FieldDefinition, FormState}; use anyhow::{anyhow, Context, Result}; diff --git a/client/src/tui/functions.rs b/client/src/tui/functions.rs index 6ffe1c1..4324756 100644 --- a/client/src/tui/functions.rs +++ b/client/src/tui/functions.rs @@ -3,9 +3,7 @@ pub mod admin; pub mod intro; pub mod login; -pub mod form; pub mod common; pub use admin::*; pub use intro::*; -pub use form::*; diff --git a/client/src/tui/functions/common.rs b/client/src/tui/functions/common.rs index 2b349a6..dc10611 100644 --- a/client/src/tui/functions/common.rs +++ b/client/src/tui/functions/common.rs @@ -1,6 +1,5 @@ // src/tui/functions/common.rs -pub mod form; pub mod login; pub mod logout; pub mod register; diff --git a/client/src/tui/functions/form.rs b/client/src/tui/functions/form.rs deleted file mode 100644 index ab1b633..0000000 --- a/client/src/tui/functions/form.rs +++ /dev/null @@ -1,44 +0,0 @@ -// src/tui/functions/form.rs -use crate::services::grpc_client::GrpcClient; -use crate::pages::forms::FormState; -use anyhow::{anyhow, Result}; - -pub async fn handle_action( - action: &str, - form_state: &mut FormState, - _grpc_client: &mut GrpcClient, - ideal_cursor_column: &mut usize, -) -> Result { - if form_state.has_unsaved_changes() { - return Ok( - "Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating." - .to_string(), - ); - } - - let total_count = form_state.total_count; - - match action { - "previous_entry" => { - // Only decrement if the current position is greater than the first record. - // This prevents wrapping from 1 to total_count. - // It also correctly handles moving from "New Entry" (total_count + 1) to the last record. - if form_state.current_position > 1 { - form_state.current_position -= 1; - *ideal_cursor_column = 0; - } - } - "next_entry" => { - // Only increment if the current position is not yet at the "New Entry" stage. - // The "New Entry" position is total_count + 1. - // This allows moving from the last record to "New Entry", but stops there. - if form_state.current_position <= total_count { - form_state.current_position += 1; - *ideal_cursor_column = 0; - } - } - _ => return Err(anyhow!("Unknown form action: {}", action)), - } - - Ok(String::new()) -}