187 lines
4.9 KiB
Rust
187 lines
4.9 KiB
Rust
// src/state/pages/form.rs
|
|
|
|
use std::collections::HashMap;
|
|
use crate::config::colors::themes::Theme;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::Frame;
|
|
use crate::state::app::highlight::HighlightState;
|
|
use crate::state::pages::canvas_state::CanvasState;
|
|
|
|
pub struct FormState {
|
|
pub id: i64,
|
|
pub profile_name: String,
|
|
pub table_name: String,
|
|
pub total_count: u64,
|
|
pub current_position: u64,
|
|
pub fields: Vec<String>,
|
|
pub values: Vec<String>,
|
|
pub current_field: usize,
|
|
pub has_unsaved_changes: bool,
|
|
pub current_cursor_pos: usize,
|
|
}
|
|
|
|
impl FormState {
|
|
pub fn new(
|
|
profile_name: String,
|
|
table_name: String,
|
|
fields: Vec<String>,
|
|
) -> Self {
|
|
let values = vec![String::new(); fields.len()];
|
|
FormState {
|
|
id: 0,
|
|
profile_name,
|
|
table_name,
|
|
total_count: 0,
|
|
current_position: 1,
|
|
fields,
|
|
values,
|
|
current_field: 0,
|
|
has_unsaved_changes: false,
|
|
current_cursor_pos: 0,
|
|
}
|
|
}
|
|
|
|
// This signature is now correct and only deals with form-related state.
|
|
pub fn render(
|
|
&self,
|
|
f: &mut Frame,
|
|
area: Rect,
|
|
theme: &Theme,
|
|
is_edit_mode: bool,
|
|
highlight_state: &HighlightState,
|
|
) {
|
|
let fields_str_slice: Vec<&str> =
|
|
self.fields.iter().map(|s| s.as_str()).collect();
|
|
let values_str_slice: Vec<&String> = self.values.iter().collect();
|
|
|
|
crate::components::form::form::render_form(
|
|
f,
|
|
area,
|
|
self,
|
|
&fields_str_slice,
|
|
&self.current_field,
|
|
&values_str_slice,
|
|
&self.table_name,
|
|
theme,
|
|
is_edit_mode,
|
|
highlight_state,
|
|
self.total_count,
|
|
self.current_position,
|
|
);
|
|
}
|
|
|
|
// ... other methods are unchanged ...
|
|
pub fn reset_to_empty(&mut self) {
|
|
self.id = 0;
|
|
self.values.iter_mut().for_each(|v| v.clear());
|
|
self.current_field = 0;
|
|
self.current_cursor_pos = 0;
|
|
self.has_unsaved_changes = false;
|
|
if self.total_count > 0 {
|
|
self.current_position = self.total_count + 1;
|
|
} else {
|
|
self.current_position = 1;
|
|
}
|
|
}
|
|
|
|
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_data: &HashMap<String, String>,
|
|
new_position: u64,
|
|
) {
|
|
self.values = self.fields.iter().map(|field_from_schema| {
|
|
response_data
|
|
.iter()
|
|
.find(|(key_from_data, _)| key_from_data.eq_ignore_ascii_case(field_from_schema))
|
|
.map(|(_, value)| value.clone())
|
|
.unwrap_or_default()
|
|
}).collect();
|
|
|
|
let id_str_opt = response_data
|
|
.iter()
|
|
.find(|(k, _)| k.eq_ignore_ascii_case("id"))
|
|
.map(|(_, v)| v);
|
|
|
|
if let Some(id_str) = id_str_opt {
|
|
if let Ok(parsed_id) = id_str.parse::<i64>() {
|
|
self.id = parsed_id;
|
|
} else {
|
|
tracing::error!( "Failed to parse 'id' field '{}' for table {}.{}", id_str, self.profile_name, self.table_name);
|
|
self.id = 0;
|
|
}
|
|
} else {
|
|
self.id = 0;
|
|
}
|
|
|
|
self.current_position = new_position;
|
|
self.has_unsaved_changes = false;
|
|
self.current_field = 0;
|
|
self.current_cursor_pos = 0;
|
|
}
|
|
}
|
|
|
|
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 {
|
|
FormState::get_current_input(self)
|
|
}
|
|
|
|
fn get_current_input_mut(&mut self) -> &mut String {
|
|
FormState::get_current_input_mut(self)
|
|
}
|
|
|
|
fn fields(&self) -> Vec<&str> {
|
|
self.fields.iter().map(|s| s.as_str()).collect()
|
|
}
|
|
|
|
fn set_current_field(&mut self, index: usize) {
|
|
if index < self.fields.len() {
|
|
self.current_field = index;
|
|
}
|
|
}
|
|
|
|
fn set_current_cursor_pos(&mut self, pos: usize) {
|
|
self.current_cursor_pos = pos;
|
|
}
|
|
|
|
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
|
self.has_unsaved_changes = changed;
|
|
}
|
|
|
|
fn get_suggestions(&self) -> Option<&[String]> {
|
|
None
|
|
}
|
|
|
|
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
|
None
|
|
}
|
|
}
|