working warning border and restrictions

This commit is contained in:
filipriec
2025-02-19 15:25:07 +01:00
parent 78e76f8eb3
commit dc21be86e7
4 changed files with 21 additions and 4 deletions

View File

@@ -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],

View File

@@ -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<dyn std::error::Error>> {
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)))
}

View File

@@ -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
}
_ => {}
}

View File

@@ -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,