From 4f39b93eddfeb67abdd3ac2f4bb2d21d00968aa9 Mon Sep 17 00:00:00 2001 From: filipriec Date: Thu, 17 Apr 2025 16:48:03 +0200 Subject: [PATCH] logic of the add button, needs redesign --- .../modes/navigation/add_table_nav.rs | 6 ++- client/src/tui/functions/common.rs | 1 + client/src/tui/functions/common/add_table.rs | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 client/src/tui/functions/common/add_table.rs diff --git a/client/src/functions/modes/navigation/add_table_nav.rs b/client/src/functions/modes/navigation/add_table_nav.rs index 1def714..cc822b6 100644 --- a/client/src/functions/modes/navigation/add_table_nav.rs +++ b/client/src/functions/modes/navigation/add_table_nav.rs @@ -6,6 +6,7 @@ use crate::state::{ }; use crossterm::event::{KeyEvent}; use ratatui::widgets::TableState; +use crate::tui::functions::common::add_table::handle_add_column_action; /// Handles navigation events specifically for the Add Table view. /// Returns true if the event was handled, false otherwise. @@ -150,8 +151,9 @@ pub fn handle_add_table_navigation( Some("select") => { match current_focus { AddTableFocus::AddColumnButton => { - *command_message = "Action: Add Column (Not Implemented)".to_string(); - // TODO: Implement logic + if let Some(focus_after_add) = handle_add_column_action(add_table_state, command_message) { + new_focus = focus_after_add; + } } AddTableFocus::SaveButton => { *command_message = "Action: Save Table (Not Implemented)".to_string(); diff --git a/client/src/tui/functions/common.rs b/client/src/tui/functions/common.rs index 54b3140..c8fd3fa 100644 --- a/client/src/tui/functions/common.rs +++ b/client/src/tui/functions/common.rs @@ -3,3 +3,4 @@ pub mod form; pub mod login; pub mod register; +pub mod add_table; diff --git a/client/src/tui/functions/common/add_table.rs b/client/src/tui/functions/common/add_table.rs new file mode 100644 index 0000000..d8339d7 --- /dev/null +++ b/client/src/tui/functions/common/add_table.rs @@ -0,0 +1,38 @@ +// src/tui/functions/common/add_table.rs +use crate::state::pages::add_table::{ + AddTableFocus, AddTableState, ColumnDefinition, +}; + +/// Handles the logic for adding a column when the "Add" button is activated. +/// +/// Takes the mutable state and command message string. +/// Returns `Some(AddTableFocus)` indicating the desired focus state after a successful add, +/// or `None` if the action failed (e.g., validation error). +pub fn handle_add_column_action( + add_table_state: &mut AddTableState, + command_message: &mut String, +) -> Option { + // Trim and create owned Strings immediately to avoid borrow issues later + let name = add_table_state.column_name_input.trim().to_string(); + let data_type = add_table_state.column_type_input.trim().to_string(); + + if name.is_empty() || data_type.is_empty() { + *command_message = "Column Name and Type cannot be empty.".to_string(); + None // Indicate failure, no focus change desired from here + } else { + let new_column = ColumnDefinition { + name: name.clone(), // Clone the owned string for the message + data_type, // Move the owned string + }; + add_table_state.columns.push(new_column); + add_table_state.column_name_input.clear(); + add_table_state.column_type_input.clear(); + add_table_state.column_name_cursor_pos = 0; + add_table_state.column_type_cursor_pos = 0; + add_table_state.has_unsaved_changes = true; + *command_message = format!("Column '{}' added.", name); + // Indicate success and suggest moving focus back to the name input + Some(AddTableFocus::InputColumnName) + } +} +