logic of the add button, needs redesign
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
pub mod form;
|
||||
pub mod login;
|
||||
pub mod register;
|
||||
pub mod add_table;
|
||||
|
||||
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