canvas required fields
This commit is contained in:
@@ -14,34 +14,62 @@ pub fn handle_add_column_action(
|
||||
) -> Option<AddTableFocus> {
|
||||
|
||||
// Trim and create owned Strings from inputs
|
||||
let table_name_val = add_table_state.table_name_input.trim().to_string();
|
||||
let column_name_val = add_table_state.column_name_input.trim().to_string();
|
||||
let column_type_val = add_table_state.column_type_input.trim().to_string();
|
||||
let table_name_in = add_table_state.table_name_input.trim();
|
||||
let column_name_in = add_table_state.column_name_input.trim();
|
||||
let column_type_in = add_table_state.column_type_input.trim();
|
||||
|
||||
// Validate all inputs needed for this combined action
|
||||
if table_name_val.is_empty() {
|
||||
*command_message = "Table Name cannot be empty.".to_string();
|
||||
None
|
||||
} else if column_name_val.is_empty() || column_type_val.is_empty() {
|
||||
*command_message = "Column Name and Type cannot be empty.".to_string();
|
||||
None // Indicate failure, no focus change desired from here
|
||||
} else {
|
||||
add_table_state.table_name = table_name_val.clone();
|
||||
let new_column = ColumnDefinition {
|
||||
name: column_name_val.clone(),
|
||||
data_type: column_type_val,
|
||||
};
|
||||
add_table_state.columns.push(new_column);
|
||||
add_table_state.table_name_input.clear();
|
||||
add_table_state.column_name_input.clear();
|
||||
add_table_state.column_type_input.clear();
|
||||
add_table_state.table_name_cursor_pos = 0;
|
||||
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!("Table name set to '{}'. Column '{}' added.", table_name_val, column_name_val);
|
||||
// Indicate success and suggest moving focus back to the name input
|
||||
Some(AddTableFocus::InputColumnName)
|
||||
let has_table_name = !table_name_in.is_empty();
|
||||
let has_column_name = !column_name_in.is_empty();
|
||||
let has_column_type = !column_type_in.is_empty();
|
||||
|
||||
match (has_table_name, has_column_name, has_column_type) {
|
||||
// Case 1: Both column fields have input (Table name is optional here)
|
||||
(_, true, true) => {
|
||||
let mut msg = String::new();
|
||||
// Optionally update table name if provided
|
||||
if has_table_name {
|
||||
add_table_state.table_name = table_name_in.to_string();
|
||||
msg.push_str(&format!("Table name set to '{}'. ", add_table_state.table_name));
|
||||
}
|
||||
// Add the column
|
||||
let new_column = ColumnDefinition {
|
||||
name: column_name_in.to_string(),
|
||||
data_type: column_type_in.to_string(),
|
||||
};
|
||||
add_table_state.columns.push(new_column.clone()); // Clone for msg
|
||||
msg.push_str(&format!("Column '{}' added.", new_column.name));
|
||||
*command_message = msg;
|
||||
|
||||
// Clear all inputs and reset cursors
|
||||
add_table_state.table_name_input.clear();
|
||||
add_table_state.column_name_input.clear();
|
||||
add_table_state.column_type_input.clear();
|
||||
add_table_state.table_name_cursor_pos = 0;
|
||||
add_table_state.column_name_cursor_pos = 0;
|
||||
add_table_state.column_type_cursor_pos = 0;
|
||||
add_table_state.has_unsaved_changes = true;
|
||||
Some(AddTableFocus::InputColumnName) // Focus for next column
|
||||
}
|
||||
// Case 2: Only one column field has input (Error)
|
||||
(_, true, false) | (_, false, true) => {
|
||||
*command_message = "Both Column Name and Type are required to add a column.".to_string();
|
||||
None // Indicate validation failure
|
||||
}
|
||||
// Case 3: Only Table name has input (No column input)
|
||||
(true, false, false) => {
|
||||
add_table_state.table_name = table_name_in.to_string();
|
||||
*command_message = format!("Table name set to '{}'.", add_table_state.table_name);
|
||||
// Clear only table name input
|
||||
add_table_state.table_name_input.clear();
|
||||
add_table_state.table_name_cursor_pos = 0;
|
||||
add_table_state.has_unsaved_changes = true;
|
||||
Some(AddTableFocus::InputTableName) // Keep focus here
|
||||
}
|
||||
// Case 4: All fields are empty
|
||||
(false, false, false) => {
|
||||
*command_message = "No input provided.".to_string();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user