diff --git a/src/client/components1/handlers/form.rs b/src/client/components1/handlers/form.rs index 68cf162..9cef66c 100644 --- a/src/client/components1/handlers/form.rs +++ b/src/client/components1/handlers/form.rs @@ -8,10 +8,12 @@ use ratatui::{ Frame, }; use crate::client::colors::Theme; +use crate::client::ui::form::FormState; pub fn render_form( f: &mut Frame, area: Rect, + form_state: &FormState, fields: &[&str], current_field: &usize, inputs: &[&String], diff --git a/src/client/terminal.rs b/src/client/terminal.rs index cb9d134..29d8993 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -53,8 +53,9 @@ impl AppTerminal { pub async fn handle_command( &mut self, - action: &str, // This should be the resolved action (e.g., "save") + action: &str, is_saved: &mut bool, + // form_state: &mut FormState, form_data: &PostAdresarRequest, ) -> Result<(bool, String), Box> { match action { @@ -63,6 +64,7 @@ impl AppTerminal { let request = tonic::Request::new(form_data.clone()); let response = self.grpc_client.post_adresar(request).await?; + // form_state.has_unsaved_changes = false; *is_saved = true; Ok((false, format!("State saved. Response: {:?}", response))) } diff --git a/src/client/ui/handlers/event.rs b/src/client/ui/handlers/event.rs index 969c6bb..bb014d5 100644 --- a/src/client/ui/handlers/event.rs +++ b/src/client/ui/handlers/event.rs @@ -1,5 +1,4 @@ // src/client/ui/handlers/event.rs - use crossterm::event::{Event, KeyCode, KeyModifiers}; use crate::client::terminal::AppTerminal; use crate::client::config::Config; @@ -42,6 +41,11 @@ impl EventHandler { self.command_message = "Edit mode".to_string(); return Ok((false, self.command_message.clone())); } else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) { + // Prevent exiting edit mode if there are unsaved changes + if form_state.has_unsaved_changes { + self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string(); + return Ok((false, self.command_message.clone())); + } self.is_edit_mode = false; self.edit_mode_cooldown = true; self.command_message = "Read-only mode".to_string(); @@ -120,6 +124,7 @@ impl EventHandler { if action == "save" && *is_saved { *current_position = total_count + 1; + form_state.has_unsaved_changes = false; // Reset unsaved changes flag } return Ok((should_exit, self.command_message.clone())); @@ -177,6 +182,10 @@ impl EventHandler { } KeyCode::Esc => { if config.is_exit_edit_mode(key.code, key.modifiers) { + if form_state.has_unsaved_changes { + self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string(); + return Ok((false, self.command_message.clone())); + } self.is_edit_mode = false; self.edit_mode_cooldown = true; self.command_message = "Read-only mode".to_string(); @@ -206,6 +215,7 @@ impl EventHandler { 14 => form_state.fax.push(c), _ => (), } + form_state.has_unsaved_changes = true; // Mark as unsaved } KeyCode::Backspace => { match form_state.current_field { @@ -226,6 +236,7 @@ impl EventHandler { 14 => form_state.fax.pop(), _ => None, }; + form_state.has_unsaved_changes = true; // Mark as unsaved } _ => {} } diff --git a/src/client/ui/handlers/form.rs b/src/client/ui/handlers/form.rs index 8725dc8..0e0fc75 100644 --- a/src/client/ui/handlers/form.rs +++ b/src/client/ui/handlers/form.rs @@ -21,6 +21,7 @@ pub struct FormState { pub telefon: String, pub skladu: String, pub fax: String, + pub current_field: usize, pub fields: Vec<&'static str>, pub has_unsaved_changes: bool, @@ -54,7 +55,7 @@ impl FormState { } pub fn render( - &mut self, + &self, f: &mut Frame, area: Rect, theme: &Theme, @@ -65,8 +66,9 @@ impl FormState { render_form( f, area, + self, &self.fields, - &mut self.current_field, + &self.current_field, &[ &self.firma, &self.kz, &self.drc, &self.ulica, &self.psc, &self.mesto, &self.stat, &self.banka, &self.ucet, &self.skladm, &self.ico, &self.kontakt, &self.telefon, &self.skladu, &self.fax,