revert working properly well

This commit is contained in:
filipriec
2025-02-28 23:27:16 +01:00
parent f1d2e5c43d
commit a79fa45a6d
4 changed files with 64 additions and 1 deletions

View File

@@ -4,6 +4,9 @@
[keybindings.common]
save = ["ctrl+s"]
quit = ["ctrl+q"]
# !!!change to space b r in the future and from edit mode
revert = ["ctrl+r"]
force_quit = ["ctrl+shift+q"]
save_and_quit = ["ctrl+shift+s"]
move_up = ["Up"]
@@ -48,6 +51,7 @@ save = ["w"]
quit = ["q"]
force_quit = ["q!"]
save_and_quit = ["wq"]
revert = ["r"]
[colors]
theme = "dark"

View File

@@ -110,7 +110,16 @@ async fn process_command(
command_input.clear();
return Ok((should_exit, message, true));
},
"revert" => {
let message = common::revert(
form_state,
app_terminal,
current_position,
total_count,
).await?;
command_input.clear();
return Ok((false, message, true));
},
"unknown" => {
let message = format!("Unknown command: {}", command);
command_input.clear();

View File

@@ -122,3 +122,44 @@ pub async fn save_and_quit(
Ok((true, "Saved and exiting application".to_string()))
}
/// Discard changes since last save
pub async fn revert(
form_state: &mut FormState,
app_terminal: &mut AppTerminal,
current_position: &mut u64,
total_count: u64,
) -> Result<String, Box<dyn std::error::Error>> {
let is_new = *current_position == total_count + 1;
if is_new {
// Clear all fields for new entries
form_state.values.iter_mut().for_each(|v| *v = String::new());
form_state.has_unsaved_changes = false;
return Ok("New entry cleared".to_string());
}
let data = app_terminal.get_adresar_by_position(*current_position).await?;
// Update form fields with saved values
form_state.values = vec![
data.firma,
data.kz,
data.drc,
data.ulica,
data.psc,
data.mesto,
data.stat,
data.banka,
data.ucet,
data.skladm,
data.ico,
data.kontakt,
data.telefon,
data.skladu,
data.fax,
];
form_state.has_unsaved_changes = false;
Ok("Changes discarded, reloaded last saved version".to_string())
}

View File

@@ -72,6 +72,15 @@ impl EventHandler {
).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
app_terminal,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
}
}