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

@@ -6,6 +6,7 @@ use crate::state::{
}; };
use crossterm::event::{KeyEvent}; use crossterm::event::{KeyEvent};
use ratatui::widgets::TableState; use ratatui::widgets::TableState;
use crate::tui::functions::common::add_table::handle_add_column_action;
/// Handles navigation events specifically for the Add Table view. /// Handles navigation events specifically for the Add Table view.
/// Returns true if the event was handled, false otherwise. /// Returns true if the event was handled, false otherwise.
@@ -150,8 +151,9 @@ pub fn handle_add_table_navigation(
Some("select") => { Some("select") => {
match current_focus { match current_focus {
AddTableFocus::AddColumnButton => { AddTableFocus::AddColumnButton => {
*command_message = "Action: Add Column (Not Implemented)".to_string(); if let Some(focus_after_add) = handle_add_column_action(add_table_state, command_message) {
// TODO: Implement logic new_focus = focus_after_add;
}
} }
AddTableFocus::SaveButton => { AddTableFocus::SaveButton => {
*command_message = "Action: Save Table (Not Implemented)".to_string(); *command_message = "Action: Save Table (Not Implemented)".to_string();

View File

@@ -3,3 +3,4 @@
pub mod form; pub mod form;
pub mod login; pub mod login;
pub mod register; pub mod register;
pub mod add_table;

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)
}
}