working warning border and restrictions
This commit is contained in:
@@ -8,10 +8,12 @@ use ratatui::{
|
|||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use crate::client::colors::Theme;
|
use crate::client::colors::Theme;
|
||||||
|
use crate::client::ui::form::FormState;
|
||||||
|
|
||||||
pub fn render_form(
|
pub fn render_form(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
|
form_state: &FormState,
|
||||||
fields: &[&str],
|
fields: &[&str],
|
||||||
current_field: &usize,
|
current_field: &usize,
|
||||||
inputs: &[&String],
|
inputs: &[&String],
|
||||||
|
|||||||
@@ -53,8 +53,9 @@ impl AppTerminal {
|
|||||||
|
|
||||||
pub async fn handle_command(
|
pub async fn handle_command(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: &str, // This should be the resolved action (e.g., "save")
|
action: &str,
|
||||||
is_saved: &mut bool,
|
is_saved: &mut bool,
|
||||||
|
// form_state: &mut FormState,
|
||||||
form_data: &PostAdresarRequest,
|
form_data: &PostAdresarRequest,
|
||||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
@@ -63,6 +64,7 @@ impl AppTerminal {
|
|||||||
let request = tonic::Request::new(form_data.clone());
|
let request = tonic::Request::new(form_data.clone());
|
||||||
let response = self.grpc_client.post_adresar(request).await?;
|
let response = self.grpc_client.post_adresar(request).await?;
|
||||||
|
|
||||||
|
// form_state.has_unsaved_changes = false;
|
||||||
*is_saved = true;
|
*is_saved = true;
|
||||||
Ok((false, format!("State saved. Response: {:?}", response)))
|
Ok((false, format!("State saved. Response: {:?}", response)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// src/client/ui/handlers/event.rs
|
// src/client/ui/handlers/event.rs
|
||||||
|
|
||||||
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||||
use crate::client::terminal::AppTerminal;
|
use crate::client::terminal::AppTerminal;
|
||||||
use crate::client::config::Config;
|
use crate::client::config::Config;
|
||||||
@@ -42,6 +41,11 @@ impl EventHandler {
|
|||||||
self.command_message = "Edit mode".to_string();
|
self.command_message = "Edit mode".to_string();
|
||||||
return Ok((false, self.command_message.clone()));
|
return Ok((false, self.command_message.clone()));
|
||||||
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
|
} 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.is_edit_mode = false;
|
||||||
self.edit_mode_cooldown = true;
|
self.edit_mode_cooldown = true;
|
||||||
self.command_message = "Read-only mode".to_string();
|
self.command_message = "Read-only mode".to_string();
|
||||||
@@ -120,6 +124,7 @@ impl EventHandler {
|
|||||||
|
|
||||||
if action == "save" && *is_saved {
|
if action == "save" && *is_saved {
|
||||||
*current_position = total_count + 1;
|
*current_position = total_count + 1;
|
||||||
|
form_state.has_unsaved_changes = false; // Reset unsaved changes flag
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok((should_exit, self.command_message.clone()));
|
return Ok((should_exit, self.command_message.clone()));
|
||||||
@@ -177,6 +182,10 @@ impl EventHandler {
|
|||||||
}
|
}
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
if config.is_exit_edit_mode(key.code, key.modifiers) {
|
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.is_edit_mode = false;
|
||||||
self.edit_mode_cooldown = true;
|
self.edit_mode_cooldown = true;
|
||||||
self.command_message = "Read-only mode".to_string();
|
self.command_message = "Read-only mode".to_string();
|
||||||
@@ -206,6 +215,7 @@ impl EventHandler {
|
|||||||
14 => form_state.fax.push(c),
|
14 => form_state.fax.push(c),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
form_state.has_unsaved_changes = true; // Mark as unsaved
|
||||||
}
|
}
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => {
|
||||||
match form_state.current_field {
|
match form_state.current_field {
|
||||||
@@ -226,6 +236,7 @@ impl EventHandler {
|
|||||||
14 => form_state.fax.pop(),
|
14 => form_state.fax.pop(),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
form_state.has_unsaved_changes = true; // Mark as unsaved
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ pub struct FormState {
|
|||||||
pub telefon: String,
|
pub telefon: String,
|
||||||
pub skladu: String,
|
pub skladu: String,
|
||||||
pub fax: String,
|
pub fax: String,
|
||||||
|
|
||||||
pub current_field: usize,
|
pub current_field: usize,
|
||||||
pub fields: Vec<&'static str>,
|
pub fields: Vec<&'static str>,
|
||||||
pub has_unsaved_changes: bool,
|
pub has_unsaved_changes: bool,
|
||||||
@@ -54,7 +55,7 @@ impl FormState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(
|
pub fn render(
|
||||||
&mut self,
|
&self,
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
@@ -65,8 +66,9 @@ impl FormState {
|
|||||||
render_form(
|
render_form(
|
||||||
f,
|
f,
|
||||||
area,
|
area,
|
||||||
|
self,
|
||||||
&self.fields,
|
&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.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,
|
&self.ucet, &self.skladm, &self.ico, &self.kontakt, &self.telefon, &self.skladu, &self.fax,
|
||||||
|
|||||||
Reference in New Issue
Block a user