hardcoded adresar to general form
This commit is contained in:
@@ -90,110 +90,173 @@ impl UiService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn initialize_app_state(
|
||||
|
||||
// MODIFIED: To set initial view table in AppState and return initial column names
|
||||
pub async fn initialize_app_state_and_form(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<Vec<String>> {
|
||||
// Fetch profile tree
|
||||
let profile_tree = grpc_client.get_profile_tree().await.context("Failed to get profile tree")?;
|
||||
// Returns (initial_profile, initial_table, initial_columns)
|
||||
) -> Result<(String, String, Vec<String>)> {
|
||||
let profile_tree = grpc_client
|
||||
.get_profile_tree()
|
||||
.await
|
||||
.context("Failed to get profile tree")?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// TODO for general tables and not hardcoded
|
||||
let default_profile_name = "default".to_string();
|
||||
let default_table_name = "2025_test_schema3".to_string();
|
||||
// Determine initial table to load (e.g., first table of first profile, or a default)
|
||||
// For now, let's hardcode a default for simplicity, but this should be more dynamic
|
||||
let initial_profile_name = app_state
|
||||
.profile_tree
|
||||
.profiles
|
||||
.first()
|
||||
.map(|p| p.name.clone())
|
||||
.unwrap_or_else(|| "default".to_string());
|
||||
|
||||
let initial_table_name = app_state
|
||||
.profile_tree
|
||||
.profiles
|
||||
.first()
|
||||
.and_then(|p| p.tables.first().map(|t| t.name.clone()))
|
||||
.unwrap_or_else(|| "2025_company_data1".to_string()); // Fallback if no tables
|
||||
|
||||
app_state.set_current_view_table(
|
||||
initial_profile_name.clone(),
|
||||
initial_table_name.clone(),
|
||||
);
|
||||
|
||||
// Fetch table structure for the default table
|
||||
let table_structure = grpc_client
|
||||
.get_table_structure(default_profile_name, default_table_name)
|
||||
.get_table_structure(
|
||||
initial_profile_name.clone(),
|
||||
initial_table_name.clone(),
|
||||
)
|
||||
.await
|
||||
.context("Failed to get initial table structure")?;
|
||||
.context(format!(
|
||||
"Failed to get initial table structure for {}.{}",
|
||||
initial_profile_name, initial_table_name
|
||||
))?;
|
||||
|
||||
// Extract the column names from the response
|
||||
let column_names: Vec<String> = table_structure
|
||||
.columns
|
||||
.iter()
|
||||
.map(|col| col.name.clone())
|
||||
.collect();
|
||||
|
||||
Ok(column_names)
|
||||
Ok((initial_profile_name, initial_table_name, column_names))
|
||||
}
|
||||
|
||||
pub async fn initialize_adresar_count(
|
||||
// NEW: Fetches and sets count for the current table in FormState
|
||||
pub async fn fetch_and_set_table_count(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<()> {
|
||||
let total_count = grpc_client.get_adresar_count().await.context("Failed to get adresar count")?;
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_adresar_count(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<()> {
|
||||
let total_count = grpc_client.get_adresar_count().await.context("Failed to get adresar by position")?;
|
||||
app_state.update_total_count(total_count);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_adresar_by_position(
|
||||
grpc_client: &mut GrpcClient,
|
||||
_app_state: &mut AppState,
|
||||
form_state: &mut FormState,
|
||||
position: u64,
|
||||
) -> Result<()> {
|
||||
let total_count = grpc_client
|
||||
.get_table_data_count(
|
||||
form_state.profile_name.clone(),
|
||||
form_state.table_name.clone(),
|
||||
)
|
||||
.await
|
||||
.context(format!(
|
||||
"Failed to get count for table {}.{}",
|
||||
form_state.profile_name, form_state.table_name
|
||||
))?;
|
||||
form_state.total_count = total_count;
|
||||
|
||||
// Set initial position: if table has items, point to first, else point to new entry
|
||||
if total_count > 0 {
|
||||
form_state.current_position = 1;
|
||||
} else {
|
||||
form_state.current_position = 1; // For a new entry in an empty table
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// MODIFIED: Generic table data loading
|
||||
pub async fn load_table_data_by_position(
|
||||
grpc_client: &mut GrpcClient,
|
||||
form_state: &mut FormState, // Takes &mut FormState to update it
|
||||
// position is now read from form_state.current_position
|
||||
) -> Result<String> {
|
||||
match grpc_client.get_adresar_by_position(position).await {
|
||||
// Ensure current_position is valid before fetching
|
||||
if form_state.current_position == 0 || (form_state.total_count > 0 && form_state.current_position > form_state.total_count) {
|
||||
// This indicates a "new entry" state, no data to load from server.
|
||||
// The caller should handle this by calling form_state.reset_to_empty()
|
||||
// or ensuring this function isn't called for a new entry position.
|
||||
// For now, let's assume reset_to_empty was called if needed.
|
||||
form_state.reset_to_empty(); // Ensure fields are clear for new entry
|
||||
return Ok(format!(
|
||||
"New entry mode for table {}.{}",
|
||||
form_state.profile_name, form_state.table_name
|
||||
));
|
||||
}
|
||||
if form_state.total_count == 0 && form_state.current_position == 1 {
|
||||
// Table is empty, this is the position for a new entry
|
||||
form_state.reset_to_empty();
|
||||
return Ok(format!(
|
||||
"New entry mode for empty table {}.{}",
|
||||
form_state.profile_name, form_state.table_name
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
match grpc_client
|
||||
.get_table_data_by_position(
|
||||
form_state.profile_name.clone(),
|
||||
form_state.table_name.clone(),
|
||||
form_state.current_position as i32,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
// Set the ID properly
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update form values dynamically
|
||||
form_state.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,
|
||||
];
|
||||
|
||||
form_state.has_unsaved_changes = false;
|
||||
Ok(format!("Loaded entry {}", position))
|
||||
form_state.update_from_response(&response.data);
|
||||
// ID, values, current_field, current_cursor_pos, has_unsaved_changes are set by update_from_response
|
||||
Ok(format!(
|
||||
"Loaded entry {}/{} for table {}.{}",
|
||||
form_state.current_position,
|
||||
form_state.total_count,
|
||||
form_state.profile_name,
|
||||
form_state.table_name
|
||||
))
|
||||
}
|
||||
Err(e) => {
|
||||
Ok(format!("Error loading entry: {}", e))
|
||||
// If loading fails (e.g., record deleted, network error), what should happen?
|
||||
// Maybe reset to a new entry state or show an error and keep current data.
|
||||
// For now, log error and return error message.
|
||||
tracing::error!(
|
||||
"Error loading entry {} for table {}.{}: {}",
|
||||
form_state.current_position,
|
||||
form_state.profile_name,
|
||||
form_state.table_name,
|
||||
e
|
||||
);
|
||||
// Potentially clear form or revert to a safe state
|
||||
// form_state.reset_to_empty();
|
||||
Err(anyhow::anyhow!(
|
||||
"Error loading entry {}: {}",
|
||||
form_state.current_position,
|
||||
e
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles the consequences of a save operation, like updating counts.
|
||||
// MODIFIED: To work with FormState's count and position
|
||||
pub async fn handle_save_outcome(
|
||||
save_outcome: SaveOutcome,
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
_grpc_client: &mut GrpcClient, // May not be needed if count is fetched separately
|
||||
_app_state: &mut AppState, // May not be needed directly
|
||||
form_state: &mut FormState,
|
||||
) -> Result<()> {
|
||||
match save_outcome {
|
||||
SaveOutcome::CreatedNew(new_id) => {
|
||||
// A new record was created, update the count!
|
||||
UiService::update_adresar_count(grpc_client, app_state).await?;
|
||||
// Navigate to the new record (now that count is updated)
|
||||
app_state.update_current_position(app_state.total_count);
|
||||
form_state.id = new_id; // Ensure ID is set (might be redundant if save already did it)
|
||||
// form_state.total_count and form_state.current_position should have been updated
|
||||
// by the `save` function itself.
|
||||
// Ensure form_state.id is set.
|
||||
form_state.id = new_id;
|
||||
// Potentially, re-fetch count to be absolutely sure, but save should be authoritative.
|
||||
// UiService::fetch_and_set_table_count(grpc_client, form_state).await?;
|
||||
}
|
||||
SaveOutcome::UpdatedExisting | SaveOutcome::NoChange => {
|
||||
// No count update needed for these outcomes
|
||||
// No changes to total_count or current_position needed from here.
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user