expected it to work like this, finally does

This commit is contained in:
filipriec
2025-02-19 22:02:08 +01:00
parent 29b5000b5c
commit 9dc2f4b1f9
2 changed files with 40 additions and 34 deletions

View File

@@ -89,8 +89,12 @@ impl EventHandler {
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} }
} else if key.code == KeyCode::Right { } else if key.code == KeyCode::Right {
if *current_position < total_count { // Allow navigation to total_count + 1 for new entries
if *current_position <= total_count {
*current_position += 1; *current_position += 1;
// Only load data if position is valid
if *current_position <= total_count {
match app_terminal.get_adresar_by_position(*current_position).await { match app_terminal.get_adresar_by_position(*current_position).await {
Ok(response) => { Ok(response) => {
// Update all form fields // Update all form fields
@@ -119,6 +123,12 @@ impl EventHandler {
self.command_message = format!("Error loading entry: {}", e); self.command_message = format!("Error loading entry: {}", e);
} }
} }
} else {
// Clear form when entering new entry position
form_state.reset_to_empty();
form_state.current_field = 0;
self.command_message = "New entry mode".to_string();
}
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} }
} else { } else {

View File

@@ -27,14 +27,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
form_state.reset_to_empty(); form_state.reset_to_empty();
// Load initial data if there are existing entries
if total_count > 0 {
let response = app_terminal
.get_adresar_by_position(total_count)
.await?;
update_form_state_from_response(&mut form_state, response);
}
loop { loop {
let total_count = app_terminal.get_adresar_count().await?; let total_count = app_terminal.get_adresar_count().await?;
app_state.update_total_count(total_count); app_state.update_total_count(total_count);
@@ -67,6 +59,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// Handle position changes and update form state // Handle position changes and update form state
if !event_handler.is_edit_mode { if !event_handler.is_edit_mode {
// Ensure position never exceeds total_count + 1
if app_state.current_position > total_count + 1 {
app_state.current_position = total_count + 1;
}
if app_state.current_position > total_count { if app_state.current_position > total_count {
// New entry - reset form // New entry - reset form
form_state.reset_to_empty(); form_state.reset_to_empty();