canvas properly updating table name
This commit is contained in:
@@ -34,6 +34,7 @@ pub enum AddTableFocus {
|
|||||||
pub struct AddTableState {
|
pub struct AddTableState {
|
||||||
pub profile_name: String,
|
pub profile_name: String,
|
||||||
pub table_name: String,
|
pub table_name: String,
|
||||||
|
pub table_name_input: String,
|
||||||
pub column_name_input: String,
|
pub column_name_input: String,
|
||||||
pub column_type_input: String,
|
pub column_type_input: String,
|
||||||
pub columns: Vec<ColumnDefinition>,
|
pub columns: Vec<ColumnDefinition>,
|
||||||
@@ -53,8 +54,9 @@ impl Default for AddTableState {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
// Initialize with some dummy data for demonstration
|
// Initialize with some dummy data for demonstration
|
||||||
AddTableState {
|
AddTableState {
|
||||||
profile_name: "default".to_string(), // Should be set dynamically
|
profile_name: "default".to_string(),
|
||||||
table_name: String::new(), // Start empty
|
table_name: String::new(),
|
||||||
|
table_name_input: String::new(),
|
||||||
column_name_input: String::new(),
|
column_name_input: String::new(),
|
||||||
column_type_input: String::new(),
|
column_type_input: String::new(),
|
||||||
columns: vec![
|
columns: vec![
|
||||||
@@ -108,12 +110,12 @@ impl CanvasState for AddTableState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
fn inputs(&self) -> Vec<&String> {
|
||||||
vec![&self.table_name, &self.column_name_input, &self.column_type_input]
|
vec![&self.table_name_input, &self.column_name_input, &self.column_type_input]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
fn get_current_input(&self) -> &str {
|
||||||
match self.current_focus {
|
match self.current_focus {
|
||||||
AddTableFocus::InputTableName => &self.table_name,
|
AddTableFocus::InputTableName => &self.table_name_input,
|
||||||
AddTableFocus::InputColumnName => &self.column_name_input,
|
AddTableFocus::InputColumnName => &self.column_name_input,
|
||||||
AddTableFocus::InputColumnType => &self.column_type_input,
|
AddTableFocus::InputColumnType => &self.column_type_input,
|
||||||
_ => "", // Should not happen if called correctly
|
_ => "", // Should not happen if called correctly
|
||||||
@@ -122,14 +124,10 @@ impl CanvasState for AddTableState {
|
|||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
fn get_current_input_mut(&mut self) -> &mut String {
|
||||||
match self.current_focus {
|
match self.current_focus {
|
||||||
AddTableFocus::InputTableName => &mut self.table_name,
|
AddTableFocus::InputTableName => &mut self.table_name_input,
|
||||||
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
||||||
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
||||||
// This case needs careful handling. If focus isn't on an input,
|
_ => &mut self.table_name_input,
|
||||||
// which mutable string should we return? Returning the first one
|
|
||||||
// might be unexpected. Consider panicking or returning Option if this state is invalid.
|
|
||||||
// For now, returning the first field to avoid panics during rendering.
|
|
||||||
_ => &mut self.table_name,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,25 +12,34 @@ pub fn handle_add_column_action(
|
|||||||
add_table_state: &mut AddTableState,
|
add_table_state: &mut AddTableState,
|
||||||
command_message: &mut String,
|
command_message: &mut String,
|
||||||
) -> Option<AddTableFocus> {
|
) -> 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() {
|
// 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();
|
||||||
|
|
||||||
|
// 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();
|
*command_message = "Column Name and Type cannot be empty.".to_string();
|
||||||
None // Indicate failure, no focus change desired from here
|
None // Indicate failure, no focus change desired from here
|
||||||
} else {
|
} else {
|
||||||
|
add_table_state.table_name = table_name_val.clone();
|
||||||
let new_column = ColumnDefinition {
|
let new_column = ColumnDefinition {
|
||||||
name: name.clone(), // Clone the owned string for the message
|
name: column_name_val.clone(),
|
||||||
data_type, // Move the owned string
|
data_type: column_type_val,
|
||||||
};
|
};
|
||||||
add_table_state.columns.push(new_column);
|
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_name_input.clear();
|
||||||
add_table_state.column_type_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_name_cursor_pos = 0;
|
||||||
add_table_state.column_type_cursor_pos = 0;
|
add_table_state.column_type_cursor_pos = 0;
|
||||||
add_table_state.has_unsaved_changes = true;
|
add_table_state.has_unsaved_changes = true;
|
||||||
*command_message = format!("Column '{}' added.", name);
|
*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
|
// Indicate success and suggest moving focus back to the name input
|
||||||
Some(AddTableFocus::InputColumnName)
|
Some(AddTableFocus::InputColumnName)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user