add logic, not working tho
This commit is contained in:
215
client/src/functions/modes/navigation/add_logic_nav.rs
Normal file
215
client/src/functions/modes/navigation/add_logic_nav.rs
Normal file
@@ -0,0 +1,215 @@
|
||||
// client/src/functions/modes/navigation/add_logic_nav.rs
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::{
|
||||
app::state::AppState,
|
||||
pages::add_logic::{AddLogicFocus, AddLogicState},
|
||||
app::buffer::AppView,
|
||||
app::buffer::BufferState,
|
||||
};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crossterm::event::{KeyEvent};
|
||||
use crate::services::GrpcClient;
|
||||
use tokio::sync::mpsc;
|
||||
use anyhow::Result;
|
||||
use common::proto::multieko2::table_script::{PostTableScriptRequest};
|
||||
|
||||
pub type SaveLogicResultSender = mpsc::Sender<Result<String>>;
|
||||
|
||||
pub fn handle_add_logic_navigation(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
add_logic_state: &mut AddLogicState,
|
||||
is_edit_mode: &mut bool,
|
||||
buffer_state: &mut BufferState,
|
||||
grpc_client: GrpcClient,
|
||||
save_logic_sender: SaveLogicResultSender,
|
||||
command_message: &mut String,
|
||||
) -> bool {
|
||||
let action = config.get_general_action(key.code, key.modifiers).map(String::from);
|
||||
let mut handled = false;
|
||||
|
||||
// Check if focus is on canvas input fields
|
||||
let focus_on_canvas_inputs = matches!(
|
||||
add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription
|
||||
);
|
||||
|
||||
// Handle script content editing separately (multiline)
|
||||
if *is_edit_mode && add_logic_state.current_focus == AddLogicFocus::InputScriptContent {
|
||||
match key.code {
|
||||
crossterm::event::KeyCode::Char(c) => {
|
||||
add_logic_state.script_content_input.push(c);
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
handled = true;
|
||||
}
|
||||
crossterm::event::KeyCode::Enter => {
|
||||
add_logic_state.script_content_input.push('\n');
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
add_logic_state.script_content_scroll.0 = add_logic_state.script_content_scroll.0.saturating_add(1);
|
||||
handled = true;
|
||||
}
|
||||
crossterm::event::KeyCode::Backspace => {
|
||||
if !add_logic_state.script_content_input.is_empty() {
|
||||
add_logic_state.script_content_input.pop();
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if !handled {
|
||||
match action.as_deref() {
|
||||
Some("exit_view") | Some("cancel_action") => {
|
||||
buffer_state.update_history(AppView::Admin); // Fixed: was AdminPanel
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*command_message = "Exited Add Logic".to_string();
|
||||
handled = true;
|
||||
}
|
||||
Some("next_field") => {
|
||||
let previous_focus = add_logic_state.current_focus;
|
||||
add_logic_state.current_focus = match add_logic_state.current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::InputScriptContent,
|
||||
AddLogicFocus::InputScriptContent => AddLogicFocus::SaveButton,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::InputLogicName,
|
||||
};
|
||||
|
||||
// Update canvas field index only when moving between canvas inputs
|
||||
if matches!(previous_focus, AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn) {
|
||||
if matches!(add_logic_state.current_focus, AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription) {
|
||||
let new_field = match add_logic_state.current_focus {
|
||||
AddLogicFocus::InputTargetColumn => 1,
|
||||
AddLogicFocus::InputDescription => 2,
|
||||
_ => 0,
|
||||
};
|
||||
add_logic_state.set_current_field(new_field);
|
||||
}
|
||||
}
|
||||
|
||||
// Update focus outside canvas flag
|
||||
app_state.ui.focus_outside_canvas = !matches!(
|
||||
add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription
|
||||
);
|
||||
|
||||
*command_message = format!("Focus: {:?}", add_logic_state.current_focus);
|
||||
*is_edit_mode = matches!(add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn |
|
||||
AddLogicFocus::InputDescription | AddLogicFocus::InputScriptContent);
|
||||
handled = true;
|
||||
}
|
||||
Some("prev_field") => {
|
||||
let previous_focus = add_logic_state.current_focus;
|
||||
add_logic_state.current_focus = match add_logic_state.current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputLogicName,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::InputScriptContent => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::InputScriptContent,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::SaveButton,
|
||||
};
|
||||
|
||||
// Update canvas field index only when moving between canvas inputs
|
||||
if matches!(previous_focus, AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription) {
|
||||
if matches!(add_logic_state.current_focus, AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn) {
|
||||
let new_field = match add_logic_state.current_focus {
|
||||
AddLogicFocus::InputLogicName => 0,
|
||||
AddLogicFocus::InputTargetColumn => 1,
|
||||
_ => 0,
|
||||
};
|
||||
add_logic_state.set_current_field(new_field);
|
||||
}
|
||||
}
|
||||
|
||||
// Update focus outside canvas flag
|
||||
app_state.ui.focus_outside_canvas = !matches!(
|
||||
add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription
|
||||
);
|
||||
|
||||
*command_message = format!("Focus: {:?}", add_logic_state.current_focus);
|
||||
*is_edit_mode = matches!(add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn |
|
||||
AddLogicFocus::InputDescription | AddLogicFocus::InputScriptContent);
|
||||
handled = true;
|
||||
}
|
||||
Some("select") => {
|
||||
match add_logic_state.current_focus {
|
||||
AddLogicFocus::SaveButton => {
|
||||
if let Some(table_def_id) = add_logic_state.selected_table_id {
|
||||
if add_logic_state.target_column_input.trim().is_empty() {
|
||||
*command_message = "Cannot save: Target Column cannot be empty.".to_string();
|
||||
} else if add_logic_state.script_content_input.trim().is_empty() {
|
||||
*command_message = "Cannot save: Script Content cannot be empty.".to_string();
|
||||
} else {
|
||||
*command_message = "Saving logic script...".to_string();
|
||||
app_state.show_loading_dialog("Saving Script", "Please wait...");
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: table_def_id,
|
||||
target_column: add_logic_state.target_column_input.trim().to_string(),
|
||||
script: add_logic_state.script_content_input.trim().to_string(),
|
||||
description: add_logic_state.description_input.trim().to_string(),
|
||||
};
|
||||
|
||||
let mut client_clone = grpc_client.clone();
|
||||
let sender_clone = save_logic_sender.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let result = client_clone.post_table_script(request).await
|
||||
.map(|res| format!("Script saved with ID: {}", res.id))
|
||||
.map_err(|e| anyhow::anyhow!("gRPC call failed: {}", e));
|
||||
let _ = sender_clone.send(result).await;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
*command_message = "Cannot save: Table Definition ID is missing.".to_string();
|
||||
}
|
||||
handled = true;
|
||||
}
|
||||
AddLogicFocus::CancelButton => {
|
||||
buffer_state.update_history(AppView::Admin); // Fixed: was AdminPanel
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*command_message = "Cancelled Add Logic".to_string();
|
||||
handled = true;
|
||||
}
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn |
|
||||
AddLogicFocus::InputDescription | AddLogicFocus::InputScriptContent => {
|
||||
if !*is_edit_mode {
|
||||
*is_edit_mode = true;
|
||||
*command_message = "Edit mode: ON".to_string();
|
||||
}
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some("toggle_edit_mode") => {
|
||||
*is_edit_mode = !*is_edit_mode;
|
||||
*command_message = format!("Edit mode: {}", if *is_edit_mode { "ON" } else { "OFF" });
|
||||
handled = true;
|
||||
}
|
||||
// Handle script content scrolling when not in edit mode
|
||||
_ if !*is_edit_mode && add_logic_state.current_focus == AddLogicFocus::InputScriptContent => {
|
||||
match action.as_deref() {
|
||||
Some("move_up") => {
|
||||
add_logic_state.script_content_scroll.0 = add_logic_state.script_content_scroll.0.saturating_sub(1);
|
||||
handled = true;
|
||||
}
|
||||
Some("move_down") => {
|
||||
add_logic_state.script_content_scroll.0 = add_logic_state.script_content_scroll.0.saturating_add(1);
|
||||
handled = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
handled
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user