centralizing logic in the formstate
This commit is contained in:
@@ -23,7 +23,9 @@ pub struct FormState {
|
||||
}
|
||||
|
||||
impl FormState {
|
||||
// MODIFIED constructor
|
||||
/// Creates a new, empty FormState for a given table.
|
||||
/// The position defaults to 1, representing either the first record
|
||||
/// or the position for a new entry if the table is empty.
|
||||
pub fn new(
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
@@ -34,8 +36,9 @@ impl FormState {
|
||||
id: 0, // Default to 0, indicating a new or unloaded record
|
||||
profile_name,
|
||||
table_name,
|
||||
total_count: 0, // Will be fetched after initialization
|
||||
current_position: 0, // Will be set after count is fetched (e.g., 1 or total_count + 1)
|
||||
total_count: 0, // Will be fetched after initialization
|
||||
// FIX: Default to 1. A position of 0 is an invalid state.
|
||||
current_position: 1,
|
||||
fields,
|
||||
values,
|
||||
current_field: 0,
|
||||
@@ -67,21 +70,20 @@ impl FormState {
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
self.total_count, // MODIFIED: Use self.total_count
|
||||
self.current_position, // MODIFIED: Use self.current_position
|
||||
self.total_count,
|
||||
self.current_position,
|
||||
);
|
||||
}
|
||||
|
||||
// MODIFIED: Reset now also considers table context for counts
|
||||
/// Resets the form to a state for creating a new entry.
|
||||
/// It clears all values and sets the position to be one after the last record.
|
||||
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;
|
||||
// current_position should be set to total_count + 1 for a new entry
|
||||
// This might be better handled by the logic that calls reset_to_empty
|
||||
// For now, let's ensure it's consistent with a "new" state.
|
||||
// Set the position for a new entry.
|
||||
if self.total_count > 0 {
|
||||
self.current_position = self.total_count + 1;
|
||||
} else {
|
||||
@@ -102,10 +104,13 @@ impl FormState {
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
|
||||
// MODIFIED: Update from a generic HashMap response
|
||||
/// Updates the form's values from a data response and sets its position.
|
||||
/// This is the single source of truth for populating the form after a data fetch.
|
||||
pub fn update_from_response(
|
||||
&mut self,
|
||||
response_data: &HashMap<String, String>,
|
||||
// FIX: Add new_position to make this method authoritative.
|
||||
new_position: u64,
|
||||
) {
|
||||
// Create a new vector for the values, ensuring they are in the correct order.
|
||||
self.values = self.fields.iter().map(|field_from_schema| {
|
||||
@@ -135,6 +140,8 @@ impl FormState {
|
||||
self.id = 0;
|
||||
}
|
||||
|
||||
// FIX: Set the position from the provided parameter.
|
||||
self.current_position = new_position;
|
||||
self.has_unsaved_changes = false;
|
||||
self.current_field = 0;
|
||||
self.current_cursor_pos = 0;
|
||||
|
||||
Reference in New Issue
Block a user