logic of the add button, needs redesign

This commit is contained in:
filipriec
2025-04-17 16:48:03 +02:00
parent ff8b4eb0f6
commit 4f39b93edd
3 changed files with 43 additions and 2 deletions

View File

@@ -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<AddTableFocus> {
// 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)
}
}