add table3

This commit is contained in:
Priec
2025-09-01 07:12:00 +02:00
committed by filipriec
parent 62c54dc1eb
commit ab81434c4e
3 changed files with 118 additions and 239 deletions

View File

@@ -1,51 +1,130 @@
// src/pages/admin_panel/add_table/event.rs
use anyhow::Result;
use crate::config::binds::config::Config;
use crate::movement::MovementAction;
use crate::pages::admin_panel::add_table::nav;
use crate::movement::{move_focus, MovementAction};
use crate::pages::admin_panel::add_table::logic::{
handle_add_column_action, handle_delete_selected_columns, handle_save_table_action,
};
use crate::pages::admin_panel::add_table::nav::SaveTableResultSender;
use crate::pages::admin_panel::add_table::state::{AddTableFocus, AddTableFormState};
use crate::services::GrpcClient;
use crate::state::app::state::AppState;
use crate::modes::handlers::event::EventOutcome;
use crossterm::event::KeyEvent;
/// Handle all AddTable page-specific events in one place.
/// - First try movement actions (Up/Down/Select/Esc) via AddTableState::handle_movement
/// - Then, if not handled, try rich actions via nav::handle_add_table_navigation
///
/// Returns true if the key was handled (caller should stop propagation).
/// Focus traversal order for AddTable (outside canvas)
const ADD_TABLE_FOCUS_ORDER: [AddTableFocus; 7] = [
AddTableFocus::InputTableName,
AddTableFocus::InputColumnName,
AddTableFocus::InputColumnType,
AddTableFocus::AddColumnButton,
AddTableFocus::SaveButton,
AddTableFocus::DeleteSelectedButton,
AddTableFocus::CancelButton,
];
/// Unified AddTable event handler (like AddLogic)
pub fn handle_add_table_event(
key_event: KeyEvent,
movement_action: Option<MovementAction>,
config: &Config,
movement: Option<MovementAction>,
_config: &Config,
app_state: &mut AppState,
page: &mut AddTableFormState,
grpc_client: GrpcClient,
mut grpc_client: GrpcClient,
save_result_sender: SaveTableResultSender,
command_message: &mut String,
) -> bool {
// 1) Try movement first
if let Some(ma) = movement_action {
if page.state.handle_movement(ma) {
let is_canvas_input = matches!(
page.current_focus(),
AddTableFocus::InputTableName
| AddTableFocus::InputColumnName
| AddTableFocus::InputColumnType
);
app_state.ui.focus_outside_canvas = !is_canvas_input;
return true;
) -> Result<EventOutcome> {
// 1) Inside canvas (FormEditor)
let inside_canvas_inputs = matches!(
page.current_focus(),
AddTableFocus::InputTableName
| AddTableFocus::InputColumnName
| AddTableFocus::InputColumnType
);
if inside_canvas_inputs {
match page.editor.handle_key_event(key_event) {
canvas::keymap::KeyEventOutcome::Consumed(Some(msg)) => {
page.sync_from_editor();
return Ok(EventOutcome::Ok(msg));
}
canvas::keymap::KeyEventOutcome::Consumed(None) => {
page.sync_from_editor();
return Ok(EventOutcome::Ok("Input updated".into()));
}
canvas::keymap::KeyEventOutcome::Pending => {
return Ok(EventOutcome::Ok(String::new()));
}
canvas::keymap::KeyEventOutcome::NotMatched => {
// fall through
}
}
}
// 2) Rich actions/navigation
nav::handle_add_table_navigation(
key_event,
config,
app_state,
&mut page.state,
grpc_client,
save_result_sender,
command_message,
)
// 2) Movement outside canvas
if let Some(ma) = movement {
let mut current = page.current_focus();
if move_focus(&ADD_TABLE_FOCUS_ORDER, &mut current, ma) {
page.set_current_focus(current);
app_state.ui.focus_outside_canvas = !matches!(
page.current_focus(),
AddTableFocus::InputTableName
| AddTableFocus::InputColumnName
| AddTableFocus::InputColumnType
);
return Ok(EventOutcome::Ok(String::new()));
}
// 3) Rich actions
match ma {
MovementAction::Select => match page.current_focus() {
AddTableFocus::AddColumnButton => {
if let Some(focus_after_add) =
crate::pages::admin_panel::add_table::logic::handle_add_column_action(
&mut page.state,
&mut String::new(),
)
{
page.set_current_focus(focus_after_add);
return Ok(EventOutcome::Ok("Column added".into()));
}
}
AddTableFocus::SaveButton => {
if page.state.table_name.is_empty() {
return Ok(EventOutcome::Ok("Cannot save: Table name is empty".into()));
}
if page.state.columns.is_empty() {
return Ok(EventOutcome::Ok("Cannot save: No columns defined".into()));
}
app_state.show_loading_dialog("Saving", "Please wait...");
let state_clone = page.state.clone();
let sender_clone = save_result_sender.clone();
tokio::spawn(async move {
let result =
crate::pages::admin_panel::add_table::logic::handle_save_table_action(
&mut grpc_client,
&state_clone,
)
.await;
let _ = sender_clone.send(result).await;
});
return Ok(EventOutcome::Ok("Saving table...".into()));
}
AddTableFocus::DeleteSelectedButton => {
let msg =
crate::pages::admin_panel::add_table::logic::handle_delete_selected_columns(
&mut page.state,
);
return Ok(EventOutcome::Ok(msg));
}
AddTableFocus::CancelButton => {
return Ok(EventOutcome::Ok("Cancelled Add Table".into()));
}
_ => {}
},
_ => {}
}
}
Ok(EventOutcome::Ok(String::new()))
}