From bd7c97ca91e5abbef68675a96c8abe1500e369da Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 25 May 2025 17:53:06 +0200 Subject: [PATCH] required table to access logic --- .../functions/modes/navigation/admin_nav.rs | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/client/src/functions/modes/navigation/admin_nav.rs b/client/src/functions/modes/navigation/admin_nav.rs index dda65e4..e4fec81 100644 --- a/client/src/functions/modes/navigation/admin_nav.rs +++ b/client/src/functions/modes/navigation/admin_nav.rs @@ -5,7 +5,7 @@ use crate::config::binds::config::Config; use crate::state::app::buffer::{BufferState, AppView}; use crate::state::pages::add_table::{AddTableState, LinkDefinition}; use ratatui::widgets::ListState; -use crate::state::pages::add_logic::AddLogicState; +use crate::state::pages::add_logic::{AddLogicState, AddLogicFocus}; // Added AddLogicFocus import // Helper functions list_select_next and list_select_previous remain the same fn list_select_next(list_state: &mut ListState, item_count: usize) { @@ -90,11 +90,11 @@ pub fn handle_admin_navigation( } Some("select") => { admin_state.selected_profile_index = admin_state.profile_list_state.selected(); - admin_state.selected_table_index = None; + admin_state.selected_table_index = None; // Deselect table when profile changes if let Some(profile_idx) = admin_state.selected_profile_index { if let Some(profile) = app_state.profile_tree.profiles.get(profile_idx) { if !profile.tables.is_empty() { - admin_state.table_list_state.select(Some(0)); + admin_state.table_list_state.select(Some(0)); // Auto-select first table for nav } else { admin_state.table_list_state.select(None); } @@ -147,7 +147,7 @@ pub fn handle_admin_navigation( } else { *command_message = "No tables in selected profile.".to_string(); } - admin_state.current_focus = AdminFocus::Tables; + admin_state.current_focus = AdminFocus::Tables; // Stay in Tables pane if no tables to enter } handled = true; } @@ -205,10 +205,9 @@ pub fn handle_admin_navigation( handled = true; } } - Some("select") => { + Some("select") => { // This is for persistently selecting a table with [*] admin_state.selected_table_index = admin_state.table_list_state.selected(); let table_name = admin_state.selected_profile_index - .or_else(|| admin_state.profile_list_state.selected()) .and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx)) .and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) .map_or("N/A", |t| t.name.as_str()); @@ -224,30 +223,44 @@ pub fn handle_admin_navigation( } } - AdminFocus::Button1 => { + AdminFocus::Button1 => { // Add Logic Button match action.as_deref() { - Some("select") => { - let mut logic_state_profile_name = "None (Global)".to_string(); - let mut selected_table_name_for_logic: Option = None; + Some("select") => { // Typically "Enter" key if let Some(p_idx) = admin_state.selected_profile_index { if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { - logic_state_profile_name = profile.name.clone(); if let Some(t_idx) = admin_state.selected_table_index { if let Some(table) = profile.tables.get(t_idx) { - selected_table_name_for_logic = Some(table.name.clone()); + // Both profile and table are selected, proceed + admin_state.add_logic_state = AddLogicState { + profile_name: profile.name.clone(), + selected_table_name: Some(table.name.clone()), + // selected_table_id: table.id, // If you have table IDs + editor_keybinding_mode: config.editor.keybinding_mode.clone(), + current_focus: AddLogicFocus::default(), // Reset focus for the new screen + ..AddLogicState::default() + }; + buffer_state.update_history(AppView::AddLogic); // Switch view + app_state.ui.focus_outside_canvas = false; // Ensure canvas focus + *command_message = format!( + "Opening Add Logic for table '{}' in profile '{}'...", + table.name, profile.name + ); + } else { + // This case should ideally not be reached if indices are managed correctly + *command_message = "Error: Selected table data not found.".to_string(); } + } else { + // Profile is selected, but table is not + *command_message = "Select a table first!".to_string(); } + } else { + // This case should ideally not be reached if p_idx is valid + *command_message = "Error: Selected profile data not found.".to_string(); } + } else { + // Profile is not selected + *command_message = "Select a profile first!".to_string(); } - admin_state.add_logic_state = AddLogicState { - profile_name: logic_state_profile_name.clone(), - selected_table_name: selected_table_name_for_logic, - editor_keybinding_mode: config.editor.keybinding_mode.clone(), - ..AddLogicState::default() - }; - buffer_state.update_history(AppView::AddLogic); - app_state.ui.focus_outside_canvas = false; - *command_message = "Opening Add Logic...".to_string(); handled = true; } Some("previous_option") | Some("move_up") => { @@ -264,20 +277,24 @@ pub fn handle_admin_navigation( } } - AdminFocus::Button2 => { + AdminFocus::Button2 => { // Add Table Button match action.as_deref() { Some("select") => { if let Some(p_idx) = admin_state.selected_profile_index { if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { let selected_profile_name = profile.name.clone(); + // Prepare links from the selected profile's existing tables let available_links: Vec = profile.tables.iter() .map(|table| LinkDefinition { linked_table_name: table.name.clone(), - is_required: false, selected: false, + is_required: false, // Default, can be changed in AddTable screen + selected: false, }).collect(); + admin_state.add_table_state = AddTableState { - profile_name: selected_profile_name, links: available_links, - ..AddTableState::default() + profile_name: selected_profile_name, + links: available_links, + ..AddTableState::default() // Reset other fields }; buffer_state.update_history(AppView::AddTable); app_state.ui.focus_outside_canvas = false; @@ -306,9 +323,10 @@ pub fn handle_admin_navigation( } } - AdminFocus::Button3 => { + AdminFocus::Button3 => { // Change Table Button match action.as_deref() { Some("select") => { + // Future: Logic to load selected table into AddTableState for editing *command_message = "Action: Change Table (Not Implemented)".to_string(); handled = true; }