moved to auth_e functions for dropdown from edit mode file

This commit is contained in:
filipriec
2025-04-11 23:42:18 +02:00
parent 024a0de4af
commit 6b16068706
2 changed files with 58 additions and 90 deletions

View File

@@ -3,6 +3,7 @@
use crate::services::grpc_client::GrpcClient;
use crate::state::canvas_state::CanvasState;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::RegisterState;
use crate::tui::functions::common::form::{revert, save};
use crossterm::event::{KeyCode, KeyEvent};
use std::any::Any;
@@ -56,7 +57,7 @@ pub async fn execute_common_action<S: CanvasState + Any>(
}
}
pub async fn execute_edit_action<S: CanvasState>(
pub async fn execute_edit_action<S: CanvasState + Any + Send>(
action: &str,
key: KeyEvent,
state: &mut S,
@@ -297,6 +298,62 @@ pub async fn execute_edit_action<S: CanvasState>(
Ok("Moved to previous word end".to_string())
}
// --- Autocomplete Actions ---
"suggestion_down" | "suggestion_up" | "select_suggestion" | "hide_suggestions" => {
// Attempt to downcast to RegisterState
if let Some(register_state) = (state as &mut dyn Any).downcast_mut::<RegisterState>() {
// Only handle if it's the role field (index 4) and suggestions are shown (except for hide)
if register_state.current_field() == 4 {
match action {
"suggestion_down" if register_state.show_role_suggestions => {
let max_index = register_state.role_suggestions.len().saturating_sub(1);
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index >= max_index { 0 } else { current_index + 1 });
Ok("Suggestion changed".to_string())
}
"suggestion_up" if register_state.show_role_suggestions => {
let max_index = register_state.role_suggestions.len().saturating_sub(1);
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index == 0 { max_index } else { current_index.saturating_sub(1) });
Ok("Suggestion changed".to_string())
}
"select_suggestion" if register_state.show_role_suggestions => {
if let Some(selected_index) = register_state.selected_suggestion_index {
if let Some(selected_role) = register_state.role_suggestions.get(selected_index) {
register_state.role = selected_role.clone();
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
register_state.role_suggestions.clear();
let new_cursor_pos = register_state.role.len();
register_state.set_current_cursor_pos(new_cursor_pos);
*ideal_cursor_column = new_cursor_pos; // Update ideal column
Ok(format!("Selected role: {}", register_state.role))
} else {
Ok("Internal error: Invalid suggestion index".to_string())
}
} else {
Ok("No suggestion selected".to_string())
}
}
"hide_suggestions" => {
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
Ok("Suggestions hidden".to_string())
}
_ => Ok("".to_string()) // Action doesn't apply in this state (e.g., suggestions not shown)
}
} else {
// Action received but not applicable to the current field
Ok("".to_string())
}
} else {
// Downcast failed - this action is only for RegisterState
Ok(format!("Action '{}' not applicable for this form type.", action))
}
}
// --- End Autocomplete Actions ---
_ => Ok(format!("Unknown or unhandled edit action: {}", action)),
}
}

View File

@@ -80,95 +80,6 @@ pub async fn handle_edit_event(
// Edit-specific actions
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
// --- Autocomplete Dropdown Interaction Handling (PRIORITY) ---
if app_state.ui.show_register && register_state.current_field() == 4 && register_state.show_role_suggestions {
match action {
"suggestion_down" => {
let max_index = register_state.role_suggestions.len().saturating_sub(1);
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index >= max_index { 0 } else { current_index + 1 });
return Ok("Suggestion changed".to_string());
}
"suggestion_up" => {
let max_index = register_state.role_suggestions.len().saturating_sub(1);
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index == 0 { max_index } else { current_index.saturating_sub(1) });
return Ok("Suggestion changed".to_string());
}
"select_suggestion" => {
if let Some(selected_index) = register_state.selected_suggestion_index {
if let Some(selected_role) = register_state.role_suggestions.get(selected_index) {
register_state.role = selected_role.clone();
// Hide dropdown after selection
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
register_state.role_suggestions.clear();
// Move cursor to end of selected role
let new_cursor_pos = register_state.role.len();
register_state.set_current_cursor_pos(new_cursor_pos);
*ideal_cursor_column = new_cursor_pos;
return Ok(format!("Selected role: {}", register_state.role));
}
}
return Ok("No suggestion selected".to_string());
}
"hide_suggestions" => {
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
return Ok("Suggestions hidden".to_string());
}
// Allow field navigation to hide suggestions
"next_field" | "prev_field" => {
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
// Fall through to execute the navigation action below
}
_ => {} // Other edit actions might be ignored while dropdown is active
}
}
// --- End Autocomplete Interaction Handling ---
// --- Autocomplete Handling for Role Field ---
if app_state.ui.show_register && register_state.current_field() == 4 {
match action {
"suggestion_down" if register_state.show_role_suggestions => {
let max_index = register_state.role_suggestions.len() - 1;
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index >= max_index { 0 } else { current_index + 1 });
return Ok("Suggestion changed".to_string());
}
"suggestion_up" if register_state.show_role_suggestions => {
let max_index = register_state.role_suggestions.len() - 1;
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
register_state.selected_suggestion_index = Some(if current_index == 0 { max_index } else { current_index - 1 });
return Ok("Suggestion changed".to_string());
}
"select_suggestion" if register_state.show_role_suggestions => {
if let Some(selected_index) = register_state.selected_suggestion_index {
if let Some(selected_role) = register_state.role_suggestions.get(selected_index) {
register_state.role = selected_role.clone();
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
register_state.role_suggestions.clear();
// Optionally move to next field or exit edit mode here
return Ok(format!("Selected role: {}", register_state.role));
}
}
return Ok("No suggestion selected".to_string());
}
"hide_suggestions" => {
register_state.show_role_suggestions = false;
register_state.selected_suggestion_index = None;
return Ok("Suggestions hidden".to_string());
}
// Let other edit actions (like move_*) fall through for the role field if desired
// Or explicitly handle them here if they should behave differently
_ => {}
}
}
// --- End Autocomplete Handling ---
return if app_state.ui.show_login {
auth_e::execute_edit_action(
action,