Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f71498703a | ||
|
|
94eea47b76 | ||
|
|
ac833294c4 | ||
|
|
81f0527085 |
@@ -10,8 +10,7 @@ use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Style, Modifier, Color}, // Removed unused Color import
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
text::Line, // Removed unused Span import
|
||||
Frame, // Removed unused Span import
|
||||
};
|
||||
|
||||
pub fn render_login(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/components/common/dialog.rs
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Color, Modifier, Style},
|
||||
style::{Modifier, Style},
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
text::{Text, Line, Span}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/adresar.rs
|
||||
@@ -0,0 +1,24 @@
|
||||
// src/services/login.rs
|
||||
|
||||
use tonic::transport::Channel;
|
||||
use common::proto::multieko2::auth::{
|
||||
auth_service_client::AuthServiceClient,
|
||||
LoginRequest, LoginResponse
|
||||
};
|
||||
|
||||
pub struct AuthClient {
|
||||
client: AuthServiceClient<Channel>,
|
||||
}
|
||||
|
||||
impl AuthClient {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let client = AuthServiceClient::connect("http://[::1]:50051").await?;
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(LoginRequest { identifier, password });
|
||||
let response = self.client.login(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,12 @@ use common::proto::multieko2::table_definition::{
|
||||
table_definition_client::TableDefinitionClient,
|
||||
ProfileTreeResponse
|
||||
};
|
||||
use common::proto::multieko2::auth::{
|
||||
auth_service_client::AuthServiceClient,
|
||||
LoginRequest, LoginResponse
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GrpcClient {
|
||||
adresar_client: AdresarClient<Channel>,
|
||||
table_structure_client: TableStructureServiceClient<Channel>,
|
||||
table_definition_client: TableDefinitionClient<Channel>,
|
||||
auth_client: AuthServiceClient<Channel>,
|
||||
}
|
||||
|
||||
impl GrpcClient {
|
||||
@@ -28,13 +23,11 @@ impl GrpcClient {
|
||||
let adresar_client = AdresarClient::connect("http://[::1]:50051").await?;
|
||||
let table_structure_client = TableStructureServiceClient::connect("http://[::1]:50051").await?;
|
||||
let table_definition_client = TableDefinitionClient::connect("http://[::1]:50051").await?;
|
||||
let auth_client = AuthServiceClient::connect("http://[::1]:50051").await?;
|
||||
|
||||
Ok(Self {
|
||||
adresar_client,
|
||||
table_structure_client,
|
||||
table_definition_client,
|
||||
auth_client,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -73,10 +66,4 @@ impl GrpcClient {
|
||||
let response = self.table_definition_client.get_profile_tree(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(LoginRequest { identifier, password });
|
||||
let response = self.auth_client.login(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
// services/mod.rs
|
||||
|
||||
pub mod grpc_client;
|
||||
pub mod adresar;
|
||||
pub mod table;
|
||||
pub mod profile;
|
||||
pub mod auth;
|
||||
pub mod ui_service;
|
||||
|
||||
pub use grpc_client::*;
|
||||
pub use adresar::*;
|
||||
pub use table::*;
|
||||
pub use profile::*;
|
||||
pub use ui_service::*;
|
||||
pub use auth::*;
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/profile.rs
|
||||
@@ -1 +0,0 @@
|
||||
// src/services/table.rs
|
||||
89
client/src/services/ui_service.rs
Normal file
89
client/src/services/ui_service.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
// src/services/ui_service.rs
|
||||
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub struct UiService;
|
||||
|
||||
impl UiService {
|
||||
pub async fn initialize_app_state(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||
// Fetch profile tree
|
||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// Fetch table structure
|
||||
let table_structure = grpc_client.get_table_structure().await?;
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
pub async fn initialize_adresar_count(
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
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<(), Box<dyn std::error::Error>> {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
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<String, Box<dyn std::error::Error>> {
|
||||
match grpc_client.get_adresar_by_position(position).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))
|
||||
}
|
||||
Err(e) => {
|
||||
Ok(format!("Error loading entry: {}", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// src/tui/functions/form.rs
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use common::proto::multieko2::adresar::AdresarResponse;
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::tui::terminal::TerminalCore;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::ui_service::UiService; // Add this import
|
||||
use crate::tui::controls::CommandHandler;
|
||||
use crate::tui::terminal::EventReader;
|
||||
use crate::config::colors::themes::Theme;
|
||||
@@ -23,19 +24,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize app_state first
|
||||
let mut app_state = AppState::new()?;
|
||||
|
||||
// Fetch profile tree and table structure
|
||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// Fetch table structure at startup (one-time)
|
||||
let table_structure = grpc_client.get_table_structure().await?;
|
||||
|
||||
// Extract the column names from the response
|
||||
let column_names: Vec<String> = table_structure
|
||||
.columns
|
||||
.iter()
|
||||
.map(|col| col.name.clone())
|
||||
.collect();
|
||||
// Initialize app state with profile tree and table structure
|
||||
let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
@@ -45,14 +35,11 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
// Fetch the total count of Adresar entries
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
||||
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
form_state.reset_to_empty();
|
||||
|
||||
loop {
|
||||
let total_count = grpc_client.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
terminal.draw(|f| {
|
||||
render_ui(
|
||||
@@ -109,44 +96,22 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
match grpc_client.get_adresar_by_position(app_state.current_position).await {
|
||||
Ok(response) => {
|
||||
// Set the ID properly
|
||||
form_state.id = response.id;
|
||||
let current_position = app_state.current_position;
|
||||
let message = UiService::load_adresar_by_position(
|
||||
&mut grpc_client,
|
||||
&mut app_state,
|
||||
&mut form_state,
|
||||
current_position
|
||||
).await?;
|
||||
|
||||
// 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,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||
current_input.len() - 1 // In readonly mode, limit to last character
|
||||
} else {
|
||||
current_input.len()
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||
current_input.len() - 1 // In readonly mode, limit to last character
|
||||
} else {
|
||||
current_input.len()
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
event_handler.command_message = message;
|
||||
} else {
|
||||
// Invalid position - reset to first entry
|
||||
app_state.current_position = 1;
|
||||
@@ -159,3 +124,4 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user