moving back to tui nonmode functions for now
This commit is contained in:
@@ -1 +1,7 @@
|
||||
// src/tui/functions/common.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod login;
|
||||
|
||||
pub use form::*;
|
||||
pub use login::*;
|
||||
|
||||
107
client/src/tui/functions/common/form.rs
Normal file
107
client/src/tui/functions/common/form.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
// src/tui/functions/common/form.rs
|
||||
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::form::FormState;
|
||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||
|
||||
/// Shared logic for saving the current form state
|
||||
pub async fn save(
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
is_saved: &mut bool,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let is_new = *current_position == total_count + 1;
|
||||
|
||||
let message = if is_new {
|
||||
let post_request = PostAdresarRequest {
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let response = grpc_client.post_adresar(post_request).await?;
|
||||
let new_total = grpc_client.get_adresar_count().await?;
|
||||
*current_position = new_total;
|
||||
form_state.id = response.into_inner().id;
|
||||
"New entry created".to_string()
|
||||
} else {
|
||||
let put_request = PutAdresarRequest {
|
||||
id: form_state.id,
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let _ = grpc_client.put_adresar(put_request).await?;
|
||||
"Entry updated".to_string()
|
||||
};
|
||||
|
||||
*is_saved = true;
|
||||
form_state.has_unsaved_changes = false;
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
/// Discard changes since last save
|
||||
pub async fn revert(
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let is_new = *current_position == total_count + 1;
|
||||
|
||||
if is_new {
|
||||
// Clear all fields for new entries
|
||||
form_state.values.iter_mut().for_each(|v| *v = String::new());
|
||||
form_state.has_unsaved_changes = false;
|
||||
return Ok("New entry cleared".to_string());
|
||||
}
|
||||
|
||||
let data = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||
|
||||
// Update form fields with saved values
|
||||
form_state.values = vec![
|
||||
data.firma,
|
||||
data.kz,
|
||||
data.drc,
|
||||
data.ulica,
|
||||
data.psc,
|
||||
data.mesto,
|
||||
data.stat,
|
||||
data.banka,
|
||||
data.ucet,
|
||||
data.skladm,
|
||||
data.ico,
|
||||
data.kontakt,
|
||||
data.telefon,
|
||||
data.skladu,
|
||||
data.fax,
|
||||
];
|
||||
|
||||
form_state.has_unsaved_changes = false;
|
||||
Ok("Changes discarded, reloaded last saved version".to_string())
|
||||
}
|
||||
62
client/src/tui/functions/common/login.rs
Normal file
62
client/src/tui/functions/common/login.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
// src/tui/functions/common/login.rs
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::state::AppState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
/// Attempts to log the user in using the provided credentials via gRPC.
|
||||
/// Updates AuthState and AppState on success or failure.
|
||||
pub async fn save(
|
||||
auth_state: &mut AuthState,
|
||||
auth_client: &mut AuthClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let identifier = auth_state.username.clone();
|
||||
let password = auth_state.password.clone();
|
||||
|
||||
// Call the gRPC login method
|
||||
match auth_client.login(identifier, password).await {
|
||||
Ok(response) => {
|
||||
// Store authentication details on success
|
||||
auth_state.auth_token = Some(response.access_token);
|
||||
auth_state.user_id = Some(response.user_id);
|
||||
auth_state.role = Some(response.role);
|
||||
auth_state.error_message = None;
|
||||
auth_state.set_has_unsaved_changes(false); // Mark as "saved"
|
||||
|
||||
// Update app state to transition from login to the main form view
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_form = true; // Assuming form is the next view
|
||||
|
||||
Ok("Login successful!".to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
// Store error message on failure
|
||||
let error_message = format!("Login failed: {}", e);
|
||||
auth_state.error_message = Some(error_message.clone());
|
||||
// Keep unsaved changes true if login fails, allowing retry/revert
|
||||
auth_state.set_has_unsaved_changes(true);
|
||||
Ok(error_message) // Return error message to display
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
||||
/// This function is now named 'revert' to match the 'form' counterpart.
|
||||
pub async fn revert(
|
||||
auth_state: &mut AuthState,
|
||||
app_state: &mut AppState,
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
auth_state.username.clear();
|
||||
auth_state.password.clear();
|
||||
auth_state.error_message = None; // Clear any previous error
|
||||
auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes
|
||||
|
||||
// Update app state to hide login and show the previous screen (Intro)
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_intro = true; // Assuming Intro is the screen before login
|
||||
|
||||
"Login reverted".to_string()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user