logic of the add button, needs redesign
This commit is contained in:
38
client/src/tui/functions/common/add_table.rs
Normal file
38
client/src/tui/functions/common/add_table.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user