146 lines
4.0 KiB
Rust
146 lines
4.0 KiB
Rust
// src/state/pages/form.rs
|
|
use crate::config::colors::themes::Theme;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::Frame;
|
|
use crate::state::pages::canvas_state::CanvasState;
|
|
|
|
pub struct FormState {
|
|
pub id: i64,
|
|
pub fields: Vec<String>, // Use Vec<String> for dynamic field names
|
|
pub values: Vec<String>, // Store field values dynamically
|
|
pub current_field: usize,
|
|
pub has_unsaved_changes: bool,
|
|
pub current_cursor_pos: usize,
|
|
}
|
|
|
|
impl FormState {
|
|
/// Create a new FormState with dynamic fields.
|
|
pub fn new(fields: Vec<String>) -> Self {
|
|
let values = vec![String::new(); fields.len()]; // Initialize values for each field
|
|
FormState {
|
|
id: 0,
|
|
fields,
|
|
values,
|
|
current_field: 0,
|
|
has_unsaved_changes: false,
|
|
current_cursor_pos: 0,
|
|
}
|
|
}
|
|
|
|
pub fn render(
|
|
&self,
|
|
f: &mut Frame,
|
|
area: Rect,
|
|
theme: &Theme,
|
|
is_edit_mode: bool,
|
|
total_count: u64,
|
|
current_position: u64,
|
|
) {
|
|
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
|
|
let values: Vec<&String> = self.values.iter().collect();
|
|
|
|
crate::components::form::form::render_form(
|
|
f,
|
|
area,
|
|
self,
|
|
&fields,
|
|
&self.current_field,
|
|
&values,
|
|
theme,
|
|
is_edit_mode,
|
|
total_count,
|
|
current_position,
|
|
);
|
|
}
|
|
|
|
pub fn reset_to_empty(&mut self) {
|
|
self.id = 0; // Reset ID to 0 for new entries
|
|
self.values.iter_mut().for_each(|v| v.clear()); // Clear all values
|
|
self.has_unsaved_changes = false;
|
|
}
|
|
|
|
pub fn get_current_input(&self) -> &str {
|
|
self.values
|
|
.get(self.current_field)
|
|
.map(|s| s.as_str())
|
|
.unwrap_or("")
|
|
}
|
|
|
|
pub fn get_current_input_mut(&mut self) -> &mut String {
|
|
self.values
|
|
.get_mut(self.current_field)
|
|
.expect("Invalid current_field index")
|
|
}
|
|
|
|
pub 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,
|
|
];
|
|
}
|
|
}
|
|
|
|
impl CanvasState for FormState {
|
|
fn current_field(&self) -> usize {
|
|
self.current_field
|
|
}
|
|
|
|
fn current_cursor_pos(&self) -> usize {
|
|
self.current_cursor_pos
|
|
}
|
|
|
|
fn has_unsaved_changes(&self) -> bool {
|
|
self.has_unsaved_changes
|
|
}
|
|
|
|
fn inputs(&self) -> Vec<&String> {
|
|
self.values.iter().collect()
|
|
}
|
|
|
|
fn get_current_input(&self) -> &str {
|
|
self.values
|
|
.get(self.current_field)
|
|
.map(|s| s.as_str())
|
|
.unwrap_or("")
|
|
}
|
|
|
|
fn get_current_input_mut(&mut self) -> &mut String {
|
|
self.values
|
|
.get_mut(self.current_field)
|
|
.expect("Invalid current_field index")
|
|
}
|
|
|
|
fn fields(&self) -> Vec<&str> {
|
|
self.fields.iter().map(|s| s.as_str()).collect()
|
|
}
|
|
|
|
// --- Implement the setter methods ---
|
|
fn set_current_field(&mut self, index: usize) {
|
|
if index < self.fields.len() { // Basic bounds check
|
|
self.current_field = index;
|
|
}
|
|
}
|
|
|
|
fn set_current_cursor_pos(&mut self, pos: usize) {
|
|
// Optional: Add validation based on current input length if needed
|
|
self.current_cursor_pos = pos;
|
|
}
|
|
|
|
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
|
self.has_unsaved_changes = changed;
|
|
}
|
|
|
|
// --- Autocomplete Support (Not Used for FormState) ---
|
|
fn get_suggestions(&self) -> Option<&[String]> {
|
|
None // FormState doesn't provide suggestions
|
|
}
|
|
|
|
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
|
None // FormState doesn't have selected suggestions
|
|
}
|
|
}
|