From 305bcfcf627c80e067da41962334aa27e3c4a7c7 Mon Sep 17 00:00:00 2001 From: filipriec Date: Fri, 18 Apr 2025 11:15:15 +0200 Subject: [PATCH] deletion of the selected works --- client/src/components/admin/add_table.rs | 3 ++- .../modes/navigation/add_table_nav.rs | 23 ++++++++++------ client/src/state/pages/add_table.rs | 1 + client/src/tui/functions/common/add_table.rs | 26 +++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/client/src/components/admin/add_table.rs b/client/src/components/admin/add_table.rs index db6a26b..73870f8 100644 --- a/client/src/components/admin/add_table.rs +++ b/client/src/components/admin/add_table.rs @@ -197,13 +197,14 @@ pub fn render_add_table( .iter() .map(|col_def| { Row::new(vec![ + Cell::from(if col_def.selected { "[*]" } else { "[ ]" }), Cell::from(col_def.name.clone()), Cell::from(col_def.data_type.clone()), ]) .style(Style::default().fg(theme.fg)) }) .collect(); - let header_cells = ["Name", "Type"] + let header_cells = ["Sel", "Name", "Type"] .iter() .map(|h| Cell::from(*h).style(Style::default().fg(theme.accent))); let header = Row::new(header_cells).height(1).bottom_margin(1); diff --git a/client/src/functions/modes/navigation/add_table_nav.rs b/client/src/functions/modes/navigation/add_table_nav.rs index e9ceabd..e966d26 100644 --- a/client/src/functions/modes/navigation/add_table_nav.rs +++ b/client/src/functions/modes/navigation/add_table_nav.rs @@ -7,6 +7,7 @@ use crate::state::{ use crossterm::event::{KeyEvent}; use ratatui::widgets::TableState; use crate::tui::functions::common::add_table::handle_add_column_action; +use crate::tui::functions::common::add_table::handle_delete_selected_columns; /// Handles navigation events specifically for the Add Table view. /// Returns true if the event was handled, false otherwise. @@ -199,12 +200,19 @@ pub fn handle_add_table_navigation( *command_message = "Entered Links Table (Scroll with Up/Down, Select to toggle/exit)".to_string(); } AddTableFocus::InsideColumnsTable => { - // Select does nothing here anymore, only Esc exits. - if let Some(index) = add_table_state.column_table_state.selected() { - *command_message = format!("Selected column index {} (Press Esc to exit scroll mode)", index); - } else { - *command_message = "No column selected (Press Esc to exit scroll mode)".to_string(); - } + // Toggle selection when pressing select *inside* the columns table + if let Some(index) = add_table_state.column_table_state.selected() { + if let Some(col) = add_table_state.columns.get_mut(index) { + col.selected = !col.selected; + add_table_state.has_unsaved_changes = true; + *command_message = format!( + "Toggled selection for column: {} to {}", + col.name, col.selected + ); + } + } else { + *command_message = "No column highlighted to toggle selection".to_string(); + } } AddTableFocus::InsideIndexesTable => { // Select does nothing here anymore, only Esc exits. @@ -247,8 +255,7 @@ pub fn handle_add_table_navigation( // TODO: Implement logic } AddTableFocus::DeleteSelectedButton => { - *command_message = "Action: Delete selected".to_string(); - // TODO: Implement logic + handle_delete_selected_columns(add_table_state, command_message); } AddTableFocus::CancelButton => { *command_message = "Action: Cancel Add Table".to_string(); diff --git a/client/src/state/pages/add_table.rs b/client/src/state/pages/add_table.rs index 9657d9c..d38cc37 100644 --- a/client/src/state/pages/add_table.rs +++ b/client/src/state/pages/add_table.rs @@ -6,6 +6,7 @@ use ratatui::widgets::TableState; pub struct ColumnDefinition { pub name: String, pub data_type: String, + pub selected: bool, } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/client/src/tui/functions/common/add_table.rs b/client/src/tui/functions/common/add_table.rs index bfa99eb..0b38e05 100644 --- a/client/src/tui/functions/common/add_table.rs +++ b/client/src/tui/functions/common/add_table.rs @@ -36,6 +36,7 @@ pub fn handle_add_column_action( let new_column = ColumnDefinition { name: column_name_in.to_string(), data_type: column_type_in.to_string(), + selected: false, }; add_table_state.columns.push(new_column.clone()); // Clone for msg msg.push_str(&format!("Column '{}' added.", new_column.name)); @@ -73,3 +74,28 @@ pub fn handle_add_column_action( } } } + +/// Handles deleting columns marked as selected in the AddTableState. +pub fn handle_delete_selected_columns( + add_table_state: &mut AddTableState, + command_message: &mut String, +) { + let initial_count = add_table_state.columns.len(); + // Keep only the columns that are NOT selected + add_table_state.columns.retain(|col| !col.selected); + let deleted_count = initial_count - add_table_state.columns.len(); + + if deleted_count > 0 { + *command_message = format!("Deleted {} selected column(s).", deleted_count); + add_table_state.has_unsaved_changes = true; + // Reset selection highlight as indices have changed + add_table_state.column_table_state.select(None); + // Optionally, select the first item if the list is not empty + // if !add_table_state.columns.is_empty() { + // add_table_state.column_table_state.select(Some(0)); + // } + } else { + *command_message = "No columns marked for deletion.".to_string(); + } +} +