add_table dialog is now working properly well

This commit is contained in:
filipriec
2025-04-18 12:20:08 +02:00
parent efa27cd2dd
commit 39dcf38462
6 changed files with 61 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ use ratatui::{
Frame,
};
use crate::components::handlers::canvas::render_canvas;
use crate::components::common::dialog;
/// Renders the Add New Table page layout, structuring the display of table information,
/// input fields, and action buttons. Adapts layout based on terminal width.
@@ -482,4 +483,18 @@ pub fn render_add_table(
)),
);
f.render_widget(cancel_button, bottom_button_chunks[2]);
// --- DIALOG ---
// Render the dialog overlay if it's active
if _app_state.ui.dialog.dialog_show { // Use the passed-in app_state
dialog::render_dialog(
f,
f.area(), // Render over the whole frame area
theme,
&_app_state.ui.dialog.dialog_title,
&_app_state.ui.dialog.dialog_message,
&_app_state.ui.dialog.dialog_buttons,
_app_state.ui.dialog.dialog_active_button_index,
);
}
}

View File

@@ -7,7 +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;
use crate::ui::handlers::context::DialogPurpose;
/// Handles navigation events specifically for the Add Table view.
/// Returns true if the event was handled, false otherwise.
@@ -255,7 +255,21 @@ pub fn handle_add_table_navigation(
// TODO: Implement logic
}
AddTableFocus::DeleteSelectedButton => {
handle_delete_selected_columns(add_table_state, command_message);
// --- Show Confirmation Dialog ---
let columns_to_delete: Vec<String> = add_table_state
.columns
.iter()
.filter(|col| col.selected)
.map(|col| col.name.clone())
.collect();
if columns_to_delete.is_empty() {
*command_message = "No columns selected for deletion.".to_string();
} else {
let message = format!("Delete the following columns?\n\n{}", columns_to_delete.join("\n"));
let buttons = vec!["Confirm".to_string(), "Cancel".to_string()];
app_state.show_dialog("Confirm Deletion", &message, buttons, DialogPurpose::ConfirmDeleteColumns);
}
}
AddTableFocus::CancelButton => {
*command_message = "Action: Cancel Add Table".to_string();

View File

@@ -6,10 +6,11 @@ use crate::ui::handlers::context::DialogPurpose;
use crate::state::app::state::AppState;
use crate::state::app::buffer::BufferState;
use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::auth::{LoginState, RegisterState};
use crate::state::pages::admin::AdminState;
use crate::modes::handlers::event::EventOutcome;
use crate::tui::functions::common::{login, register};
use crate::tui::functions::common::add_table::handle_delete_selected_columns;
/// Handles key events specifically when a dialog is active.
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
@@ -22,6 +23,7 @@ pub async fn handle_dialog_event(
login_state: &mut LoginState,
register_state: &mut RegisterState,
buffer_state: &mut BufferState,
admin_state: &mut AdminState,
) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> {
if let Event::Key(key) = event {
// Always allow Esc to dismiss
@@ -114,6 +116,20 @@ pub async fn handle_dialog_event(
}
}
}
DialogPurpose::ConfirmDeleteColumns => {
match selected_index {
0 => { // "Confirm" button selected
let outcome_message = handle_delete_selected_columns(&mut admin_state.add_table_state);
app_state.hide_dialog();
return Some(Ok(EventOutcome::Ok(outcome_message)));
}
1 => { // "Cancel" button selected
app_state.hide_dialog();
return Some(Ok(EventOutcome::Ok("Deletion cancelled.".to_string())));
}
_ => { /* Handle unexpected index */ }
}
}
}
}
_ => {} // Ignore other general actions when dialog is shown

View File

@@ -116,7 +116,14 @@ impl EventHandler {
if app_state.ui.dialog.dialog_show {
if let Some(dialog_result) = dialog::handle_dialog_event(
&event, config, app_state, auth_state, login_state, register_state, buffer_state
&event,
config,
app_state,
auth_state,
login_state,
register_state,
buffer_state,
admin_state,
).await {
return dialog_result;
}

View File

@@ -78,15 +78,13 @@ 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,
) {
) -> 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);
@@ -94,8 +92,9 @@ pub fn handle_delete_selected_columns(
// if !add_table_state.columns.is_empty() {
// add_table_state.column_table_state.select(Some(0));
// }
format!("Deleted {} selected column(s).", deleted_count)
} else {
*command_message = "No columns marked for deletion.".to_string();
"No columns marked for deletion.".to_string()
}
}

View File

@@ -15,6 +15,7 @@ pub enum DialogPurpose {
LoginFailed,
RegisterSuccess,
RegisterFailed,
ConfirmDeleteColumns, // add_table delete selected Columns
// TODO in the future:
// ConfirmQuit,
}