compiled
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
use async_trait::async_trait;
|
||||
use crate::modes::handlers::read_only::ReadOnlyHandler;
|
||||
use crate::tui::terminal::GrpcClient;
|
||||
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
@@ -12,6 +15,50 @@ pub struct FormState {
|
||||
pub current_cursor_pos: usize,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ReadOnlyHandler for FormState {
|
||||
async fn handle_read_only_action(
|
||||
&mut self,
|
||||
action: &str,
|
||||
grpc_client: &mut GrpcClient,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
let new_position = current_position.saturating_sub(1);
|
||||
if new_position >= 1 {
|
||||
*current_position = new_position;
|
||||
let response = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||
self.update_from_response(response);
|
||||
Ok(format!("Loaded entry {}", *current_position))
|
||||
} else {
|
||||
Ok("Already at first entry".into())
|
||||
}
|
||||
},
|
||||
"next_entry" => {
|
||||
if *current_position < total_count {
|
||||
*current_position += 1;
|
||||
let response = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||
self.update_from_response(response);
|
||||
Ok(format!("Loaded entry {}", *current_position))
|
||||
} else {
|
||||
self.reset_to_empty();
|
||||
Ok("New entry mode".into())
|
||||
}
|
||||
},
|
||||
_ => Ok(format!("Unknown action: {}", action))
|
||||
}
|
||||
}
|
||||
|
||||
fn adjust_cursor_position(&mut self, ideal_column: &mut usize) {
|
||||
let current_input = self.get_current_input();
|
||||
let max_pos = current_input.len().saturating_sub(1);
|
||||
self.current_cursor_pos = (*ideal_column).min(max_pos);
|
||||
*ideal_column = self.current_cursor_pos;
|
||||
}
|
||||
}
|
||||
|
||||
impl FormState {
|
||||
/// Create a new FormState with dynamic fields.
|
||||
pub fn new(fields: Vec<String>) -> Self {
|
||||
@@ -70,4 +117,15 @@ impl FormState {
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
|
||||
fn update_from_response(&mut self, response: common::proto::multieko2::adresar::AdresarResponse) {
|
||||
self.id = response.id;
|
||||
self.values = vec![
|
||||
response.firma, response.kz, response.drc,
|
||||
response.ulica, response.psc, response.mesto,
|
||||
response.stat, response.banka, response.ucet,
|
||||
response.skladm, response.ico, response.kontakt,
|
||||
response.telefon, response.skladu, response.fax,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user