forms page moved more2
This commit is contained in:
@@ -6,7 +6,7 @@ use crate::state::app::state::AppState;
|
|||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::services::auth::AuthClient;
|
use crate::services::auth::AuthClient;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::tui::functions::common::form::SaveOutcome;
|
crate::pages::forms::logic::SaveOutcome;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use crate::tui::functions::common::{
|
use crate::tui::functions::common::{
|
||||||
form::{save as form_save, revert as form_revert},
|
form::{save as form_save, revert as form_revert},
|
||||||
|
|||||||
@@ -6,9 +6,8 @@ use crate::services::grpc_client::GrpcClient;
|
|||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::modes::common::commands::CommandHandler;
|
use crate::modes::common::commands::CommandHandler;
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::tui::functions::common::form::{save, revert};
|
use crate::pages::forms::logic::{save, revert ,SaveOutcome};
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::tui::functions::common::form::SaveOutcome;
|
|
||||||
use crate::pages::routing::{Router, Page};
|
use crate::pages::routing::{Router, Page};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
|||||||
@@ -29,13 +29,14 @@ use crate::state::{
|
|||||||
intro::IntroState,
|
intro::IntroState,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use crate::tui::common::{register, login};
|
||||||
use crate::pages::routing::{Router, Page};
|
use crate::pages::routing::{Router, Page};
|
||||||
use crate::pages::forms::FormState;
|
use crate::pages::forms::FormState;
|
||||||
|
use crate::pages::forms::logic::{save, revert, SaveOutcome};
|
||||||
use crate::search::state::SearchState;
|
use crate::search::state::SearchState;
|
||||||
use crate::tui::functions::common::login::LoginResult;
|
use crate::tui::functions::common::login::LoginResult;
|
||||||
use crate::tui::functions::common::register::RegisterResult;
|
use crate::tui::functions::common::register::RegisterResult;
|
||||||
use crate::tui::{
|
use crate::tui::{
|
||||||
functions::common::{form::SaveOutcome, login, register},
|
|
||||||
terminal::core::TerminalCore,
|
terminal::core::TerminalCore,
|
||||||
{admin, intro},
|
{admin, intro},
|
||||||
};
|
};
|
||||||
@@ -816,7 +817,7 @@ impl EventHandler {
|
|||||||
Ok(EventOutcome::Ok(message))
|
Ok(EventOutcome::Ok(message))
|
||||||
} else {
|
} else {
|
||||||
let save_outcome = if let Page::Form(_) = &router.current {
|
let save_outcome = if let Page::Form(_) = &router.current {
|
||||||
crate::tui::functions::common::form::save(
|
save(
|
||||||
app_state,
|
app_state,
|
||||||
&mut self.grpc_client,
|
&mut self.grpc_client,
|
||||||
)
|
)
|
||||||
@@ -851,7 +852,7 @@ impl EventHandler {
|
|||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
let save_outcome = crate::tui::functions::common::form::save(
|
let save_outcome = save(
|
||||||
app_state,
|
app_state,
|
||||||
&mut self.grpc_client,
|
&mut self.grpc_client,
|
||||||
).await?;
|
).await?;
|
||||||
@@ -882,7 +883,7 @@ impl EventHandler {
|
|||||||
.await
|
.await
|
||||||
} else {
|
} else {
|
||||||
if let Page::Form(_) = &router.current {
|
if let Page::Form(_) = &router.current {
|
||||||
crate::tui::functions::common::form::revert(
|
revert(
|
||||||
app_state,
|
app_state,
|
||||||
&mut self.grpc_client,
|
&mut self.grpc_client,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// src/tui/functions/common/form.rs
|
// src/pages/forms/logic.rs
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
|
use crate::pages::forms::FormState;
|
||||||
use crate::utils::data_converter;
|
use crate::utils::data_converter;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -21,7 +22,6 @@ pub async fn save(
|
|||||||
return Ok(SaveOutcome::NoChange);
|
return Ok(SaveOutcome::NoChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy out what we need before dropping the mutable borrow
|
|
||||||
let profile_name = fs.profile_name.clone();
|
let profile_name = fs.profile_name.clone();
|
||||||
let table_name = fs.table_name.clone();
|
let table_name = fs.table_name.clone();
|
||||||
let fields = fs.fields.clone();
|
let fields = fs.fields.clone();
|
||||||
@@ -75,7 +75,7 @@ pub async fn save(
|
|||||||
} else {
|
} else {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
"Cannot update record: ID is 0, but not classified as new entry."
|
"Cannot update record: ID is 0, but not classified as new entry."
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let response = grpc_client
|
let response = grpc_client
|
||||||
@@ -106,7 +106,7 @@ pub async fn revert(
|
|||||||
if let Some(fs) = app_state.form_state_mut() {
|
if let Some(fs) = app_state.form_state_mut() {
|
||||||
if fs.id == 0
|
if fs.id == 0
|
||||||
|| (fs.total_count > 0 && fs.current_position > fs.total_count)
|
|| (fs.total_count > 0 && fs.current_position > fs.total_count)
|
||||||
|| (fs.total_count == 0 && fs.current_position == 1)
|
|| (fs.total_count == 0 && fs.current_position == 1)
|
||||||
{
|
{
|
||||||
let old_total_count = fs.total_count;
|
let old_total_count = fs.total_count;
|
||||||
fs.reset_to_empty();
|
fs.reset_to_empty();
|
||||||
@@ -136,8 +136,8 @@ pub async fn revert(
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context(format!(
|
.context(format!(
|
||||||
"Failed to get table data by position {} for table {}.{}",
|
"Failed to get table data by position {} for table {}.{}",
|
||||||
fs.current_position, fs.profile_name, fs.table_name
|
fs.current_position, fs.profile_name, fs.table_name
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
fs.update_from_response(&response.data, fs.current_position);
|
fs.update_from_response(&response.data, fs.current_position);
|
||||||
@@ -146,3 +146,37 @@ pub async fn revert(
|
|||||||
Ok("Nothing to revert".to_string())
|
Ok("Nothing to revert".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn handle_action(
|
||||||
|
action: &str,
|
||||||
|
form_state: &mut FormState,
|
||||||
|
_grpc_client: &mut GrpcClient,
|
||||||
|
ideal_cursor_column: &mut usize,
|
||||||
|
) -> Result<String> {
|
||||||
|
if form_state.has_unsaved_changes() {
|
||||||
|
return Ok(
|
||||||
|
"Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating."
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_count = form_state.total_count;
|
||||||
|
|
||||||
|
match action {
|
||||||
|
"previous_entry" => {
|
||||||
|
if form_state.current_position > 1 {
|
||||||
|
form_state.current_position -= 1;
|
||||||
|
*ideal_cursor_column = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"next_entry" => {
|
||||||
|
if form_state.current_position <= total_count {
|
||||||
|
form_state.current_position += 1;
|
||||||
|
*ideal_cursor_column = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => return Err(anyhow!("Unknown form action: {}", action)),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(String::new())
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
pub mod logic;
|
||||||
|
|
||||||
pub use ui::*;
|
pub use ui::*;
|
||||||
pub use state::*;
|
pub use state::*;
|
||||||
|
pub use logic::*;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::pages::add_logic::AddLogicState;
|
use crate::state::pages::add_logic::AddLogicState;
|
||||||
use crate::tui::functions::common::form::SaveOutcome;
|
use crate::pages::forms::logic::SaveOutcome;
|
||||||
use crate::utils::columns::filter_user_columns;
|
use crate::utils::columns::filter_user_columns;
|
||||||
use crate::pages::forms::{FieldDefinition, FormState};
|
use crate::pages::forms::{FieldDefinition, FormState};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
pub mod admin;
|
pub mod admin;
|
||||||
pub mod intro;
|
pub mod intro;
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod form;
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
||||||
pub use admin::*;
|
pub use admin::*;
|
||||||
pub use intro::*;
|
pub use intro::*;
|
||||||
pub use form::*;
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// src/tui/functions/common.rs
|
// src/tui/functions/common.rs
|
||||||
|
|
||||||
pub mod form;
|
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod logout;
|
pub mod logout;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
// src/tui/functions/form.rs
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
|
||||||
use crate::pages::forms::FormState;
|
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
|
|
||||||
pub async fn handle_action(
|
|
||||||
action: &str,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
_grpc_client: &mut GrpcClient,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<String> {
|
|
||||||
if form_state.has_unsaved_changes() {
|
|
||||||
return Ok(
|
|
||||||
"Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating."
|
|
||||||
.to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let total_count = form_state.total_count;
|
|
||||||
|
|
||||||
match action {
|
|
||||||
"previous_entry" => {
|
|
||||||
// Only decrement if the current position is greater than the first record.
|
|
||||||
// This prevents wrapping from 1 to total_count.
|
|
||||||
// It also correctly handles moving from "New Entry" (total_count + 1) to the last record.
|
|
||||||
if form_state.current_position > 1 {
|
|
||||||
form_state.current_position -= 1;
|
|
||||||
*ideal_cursor_column = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"next_entry" => {
|
|
||||||
// Only increment if the current position is not yet at the "New Entry" stage.
|
|
||||||
// The "New Entry" position is total_count + 1.
|
|
||||||
// This allows moving from the last record to "New Entry", but stops there.
|
|
||||||
if form_state.current_position <= total_count {
|
|
||||||
form_state.current_position += 1;
|
|
||||||
*ideal_cursor_column = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => return Err(anyhow!("Unknown form action: {}", action)),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(String::new())
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user