Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efb59c5571 | ||
|
|
26cf06df14 | ||
|
|
c82b62f72b | ||
|
|
87cdb8048d | ||
|
|
f71498703a | ||
|
|
94eea47b76 | ||
|
|
ac833294c4 | ||
|
|
81f0527085 | ||
|
|
dd4d9e88c6 | ||
|
|
f66d67c238 | ||
|
|
9cf25afa52 | ||
|
|
2ed2419f9e |
@@ -10,8 +10,7 @@ use ratatui::{
|
|||||||
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
||||||
style::{Style, Modifier, Color}, // Removed unused Color import
|
style::{Style, Modifier, Color}, // Removed unused Color import
|
||||||
widgets::{Block, BorderType, Borders, Paragraph},
|
widgets::{Block, BorderType, Borders, Paragraph},
|
||||||
Frame,
|
Frame, // Removed unused Span import
|
||||||
text::Line, // Removed unused Span import
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn render_login(
|
pub fn render_login(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// src/components/common/dialog.rs
|
// src/components/common/dialog.rs
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Constraint, Direction, Layout, Rect, Margin},
|
layout::{Constraint, Direction, Layout, Rect, Margin},
|
||||||
style::{Color, Modifier, Style},
|
style::{Modifier, Style},
|
||||||
widgets::{Block, BorderType, Borders, Paragraph},
|
widgets::{Block, BorderType, Borders, Paragraph},
|
||||||
Frame,
|
Frame,
|
||||||
text::{Text, Line, Span}
|
text::{Text, Line, Span}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use crate::config::colors::themes::Theme;
|
|||||||
use crate::state::canvas_state::CanvasState;
|
use crate::state::canvas_state::CanvasState;
|
||||||
use crate::components::handlers::canvas::render_canvas;
|
use crate::components::handlers::canvas::render_canvas;
|
||||||
|
|
||||||
// Original form renderer (keep for backward compatibility)
|
|
||||||
pub fn render_form(
|
pub fn render_form(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ pub mod config;
|
|||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod components;
|
pub mod components;
|
||||||
pub mod modes;
|
pub mod modes;
|
||||||
|
pub mod services;
|
||||||
|
|
||||||
pub use ui::run_ui;
|
pub use ui::run_ui;
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
// src/modes/canvas/common.rs
|
// src/modes/canvas/common.rs
|
||||||
|
|
||||||
use crate::config::binds::config::Config;
|
|
||||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::state::state::AppState;
|
use crate::state::state::AppState;
|
||||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::tui::functions::common::form::{save, revert};
|
||||||
|
|
||||||
/// Main handler for common core actions
|
/// Main handler for common core actions
|
||||||
pub async fn handle_core_action(
|
pub async fn handle_core_action(
|
||||||
@@ -52,127 +51,6 @@ pub async fn handle_core_action(
|
|||||||
).await?;
|
).await?;
|
||||||
Ok((false, message))
|
Ok((false, message))
|
||||||
},
|
},
|
||||||
// We should never hit this case with proper filtering
|
|
||||||
_ => Ok((false, format!("Core action not handled: {}", action))),
|
_ => Ok((false, format!("Core action not handled: {}", action))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to check if a key event should trigger a core action
|
|
||||||
pub fn is_core_action(config: &Config, key_code: crossterm::event::KeyCode, modifiers: crossterm::event::KeyModifiers) -> Option<String> {
|
|
||||||
// Check for core application actions (save, quit, etc.)
|
|
||||||
if let Some(action) = config.get_action_for_key_in_mode(
|
|
||||||
&config.keybindings.common,
|
|
||||||
key_code,
|
|
||||||
modifiers
|
|
||||||
) {
|
|
||||||
match action {
|
|
||||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
|
||||||
return Some(action.to_string())
|
|
||||||
},
|
|
||||||
_ => {} // Other actions are handled by their respective mode handlers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
||||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||||
use crate::tui::terminal::{
|
|
||||||
grpc_client::GrpcClient,
|
|
||||||
};
|
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::modes::canvas::common;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::tui::functions::common::form::{save, revert};
|
||||||
|
|
||||||
pub async fn handle_edit_event_internal(
|
pub async fn handle_edit_event_internal(
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
@@ -69,7 +67,7 @@ async fn execute_common_action(
|
|||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
common::save(
|
save(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
is_saved,
|
is_saved,
|
||||||
@@ -78,7 +76,7 @@ async fn execute_common_action(
|
|||||||
).await
|
).await
|
||||||
},
|
},
|
||||||
"revert" => {
|
"revert" => {
|
||||||
common::revert(
|
revert(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
@@ -179,7 +177,7 @@ async fn execute_edit_action(
|
|||||||
) -> Result<String, Box<dyn std::error::Error>> {
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
common::save(
|
save(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client, // Changed from AppTerminal
|
grpc_client, // Changed from AppTerminal
|
||||||
is_saved,
|
is_saved,
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
|
|
||||||
// src/modes/canvas/read_only.rs
|
// src/modes/canvas/read_only.rs
|
||||||
|
|
||||||
use crossterm::event::{KeyEvent};
|
use crossterm::event::{KeyEvent};
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::state::pages::auth::AuthState;
|
use crate::state::pages::auth::AuthState;
|
||||||
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum CharType {
|
enum CharType {
|
||||||
@@ -187,11 +186,6 @@ async fn execute_action(
|
|||||||
command_message.clear();
|
command_message.clear();
|
||||||
Ok("".to_string())
|
Ok("".to_string())
|
||||||
}
|
}
|
||||||
"exit_edit_mode" => {
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
command_message.clear();
|
|
||||||
Ok("".to_string())
|
|
||||||
}
|
|
||||||
"move_left" => {
|
"move_left" => {
|
||||||
let current_pos = form_state.current_cursor_pos;
|
let current_pos = form_state.current_cursor_pos;
|
||||||
form_state.current_cursor_pos = current_pos.saturating_sub(1);
|
form_state.current_cursor_pos = current_pos.saturating_sub(1);
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
// src/modes/handlers/command_mode.rs
|
// src/modes/handlers/command_mode.rs
|
||||||
|
|
||||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::tui::controls::commands::CommandHandler;
|
use crate::tui::functions::common::commands::CommandHandler;
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::modes::{
|
use crate::tui::functions::common::form::{save, revert};
|
||||||
canvas::{common},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub async fn handle_command_event(
|
pub async fn handle_command_event(
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
@@ -97,7 +95,7 @@ async fn process_command(
|
|||||||
Ok((should_exit, message, true))
|
Ok((should_exit, message, true))
|
||||||
},
|
},
|
||||||
"save" => {
|
"save" => {
|
||||||
let message = common::save(
|
let message = save(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
&mut command_handler.is_saved,
|
&mut command_handler.is_saved,
|
||||||
@@ -108,7 +106,7 @@ async fn process_command(
|
|||||||
return Ok((false, message, true));
|
return Ok((false, message, true));
|
||||||
},
|
},
|
||||||
"revert" => {
|
"revert" => {
|
||||||
let message = common::revert(
|
let message = revert(
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ use crossterm::event::Event;
|
|||||||
use crossterm::cursor::SetCursorStyle;
|
use crossterm::cursor::SetCursorStyle;
|
||||||
use crate::tui::terminal::{
|
use crate::tui::terminal::{
|
||||||
core::TerminalCore,
|
core::TerminalCore,
|
||||||
grpc_client::GrpcClient,
|
|
||||||
};
|
};
|
||||||
use crate::tui::controls::commands::CommandHandler;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::tui::functions::common::commands::CommandHandler;
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::state::pages::auth::AuthState;
|
use crate::state::pages::auth::AuthState;
|
||||||
|
|||||||
23
client/src/services/auth.rs
Normal file
23
client/src/services/auth.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// src/services/auth.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// src/tui/terminal/grpc_client.rs
|
// src/services/grpc_client.rs
|
||||||
|
|
||||||
use tonic::transport::Channel;
|
use tonic::transport::Channel;
|
||||||
use common::proto::multieko2::adresar::adresar_client::AdresarClient;
|
use common::proto::multieko2::adresar::adresar_client::AdresarClient;
|
||||||
@@ -11,6 +11,7 @@ use common::proto::multieko2::table_definition::{
|
|||||||
ProfileTreeResponse
|
ProfileTreeResponse
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct GrpcClient {
|
pub struct GrpcClient {
|
||||||
adresar_client: AdresarClient<Channel>,
|
adresar_client: AdresarClient<Channel>,
|
||||||
table_structure_client: TableStructureServiceClient<Channel>,
|
table_structure_client: TableStructureServiceClient<Channel>,
|
||||||
9
client/src/services/mod.rs
Normal file
9
client/src/services/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// services/mod.rs
|
||||||
|
|
||||||
|
pub mod grpc_client;
|
||||||
|
pub mod auth;
|
||||||
|
pub mod ui_service;
|
||||||
|
|
||||||
|
pub use grpc_client::*;
|
||||||
|
pub use ui_service::*;
|
||||||
|
pub use auth::*;
|
||||||
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -9,6 +9,9 @@ pub struct AuthState {
|
|||||||
pub error_message: Option<String>,
|
pub error_message: Option<String>,
|
||||||
pub current_field: usize,
|
pub current_field: usize,
|
||||||
pub current_cursor_pos: usize,
|
pub current_cursor_pos: usize,
|
||||||
|
pub auth_token: Option<String>,
|
||||||
|
pub user_id: Option<String>,
|
||||||
|
pub role: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AuthState {
|
impl AuthState {
|
||||||
@@ -20,6 +23,9 @@ impl AuthState {
|
|||||||
error_message: None,
|
error_message: None,
|
||||||
current_field: 0,
|
current_field: 0,
|
||||||
current_cursor_pos: 0,
|
current_cursor_pos: 0,
|
||||||
|
auth_token: None,
|
||||||
|
user_id: None,
|
||||||
|
role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
// src/tui/controls.rs
|
|
||||||
|
|
||||||
pub mod commands;
|
|
||||||
|
|
||||||
pub use commands::*;
|
|
||||||
@@ -4,7 +4,9 @@ pub mod admin;
|
|||||||
pub mod intro;
|
pub mod intro;
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod form;
|
pub mod form;
|
||||||
|
pub mod common;
|
||||||
|
|
||||||
pub use admin::*;
|
pub use admin::*;
|
||||||
pub use intro::*;
|
pub use intro::*;
|
||||||
pub use form::*;
|
pub use form::*;
|
||||||
|
pub use common::*;
|
||||||
|
|||||||
6
client/src/tui/functions/common.rs
Normal file
6
client/src/tui/functions/common.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
// src/tui/functions/common.rs
|
||||||
|
pub mod commands;
|
||||||
|
pub mod form;
|
||||||
|
|
||||||
|
pub use commands::*;
|
||||||
|
pub use form::*;
|
||||||
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())
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
// src/tui/functions/form.rs
|
// src/tui/functions/form.rs
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::tui::terminal::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use common::proto::multieko2::adresar::AdresarResponse;
|
|
||||||
|
|
||||||
pub async fn handle_action(
|
pub async fn handle_action(
|
||||||
action: &str,
|
action: &str,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// src/tui/mod.rs
|
// src/tui/mod.rs
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
pub mod controls;
|
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
|
|
||||||
|
pub use functions::*;
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
// src/tui/terminal.rs
|
// src/tui/terminal.rs
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod grpc_client;
|
|
||||||
pub mod event_reader;
|
pub mod event_reader;
|
||||||
|
|
||||||
pub use core::TerminalCore;
|
pub use core::TerminalCore;
|
||||||
pub use grpc_client::GrpcClient;
|
|
||||||
pub use event_reader::EventReader;
|
pub use event_reader::EventReader;
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
// src/ui/handlers/ui.rs
|
// src/ui/handlers/ui.rs
|
||||||
|
|
||||||
use crate::tui::terminal::TerminalCore;
|
use crate::tui::terminal::TerminalCore;
|
||||||
use crate::tui::terminal::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::tui::controls::CommandHandler;
|
use crate::services::ui_service::UiService; // Add this import
|
||||||
use crate::tui::terminal::EventReader;
|
use crate::tui::terminal::EventReader;
|
||||||
|
use crate::tui::functions::common::CommandHandler;
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::ui::handlers::render::render_ui;
|
use crate::ui::handlers::render::render_ui;
|
||||||
@@ -23,19 +24,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Initialize app_state first
|
// Initialize app_state first
|
||||||
let mut app_state = AppState::new()?;
|
let mut app_state = AppState::new()?;
|
||||||
|
|
||||||
// Fetch profile tree and table structure
|
// Initialize app state with profile tree and table structure
|
||||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).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 FormState with dynamic fields
|
// Initialize FormState with dynamic fields
|
||||||
let mut form_state = FormState::new(column_names);
|
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();
|
let event_reader = EventReader::new();
|
||||||
|
|
||||||
// Fetch the total count of Adresar entries
|
// Fetch the total count of Adresar entries
|
||||||
let total_count = grpc_client.get_adresar_count().await?;
|
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||||
app_state.update_total_count(total_count);
|
|
||||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
|
||||||
form_state.reset_to_empty();
|
form_state.reset_to_empty();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let total_count = grpc_client.get_adresar_count().await?;
|
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||||
app_state.update_total_count(total_count);
|
|
||||||
|
|
||||||
terminal.draw(|f| {
|
terminal.draw(|f| {
|
||||||
render_ui(
|
render_ui(
|
||||||
@@ -109,44 +96,22 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
form_state.current_field = 0;
|
form_state.current_field = 0;
|
||||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||||
// Existing entry - load data
|
// Existing entry - load data
|
||||||
match grpc_client.get_adresar_by_position(app_state.current_position).await {
|
let current_position = app_state.current_position;
|
||||||
Ok(response) => {
|
let message = UiService::load_adresar_by_position(
|
||||||
// Set the ID properly
|
&mut grpc_client,
|
||||||
form_state.id = response.id;
|
&mut app_state,
|
||||||
|
&mut form_state,
|
||||||
|
current_position
|
||||||
|
).await?;
|
||||||
|
|
||||||
// Update form values dynamically
|
let current_input = form_state.get_current_input();
|
||||||
form_state.values = vec![
|
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||||
response.firma,
|
current_input.len() - 1 // In readonly mode, limit to last character
|
||||||
response.kz,
|
} else {
|
||||||
response.drc,
|
current_input.len()
|
||||||
response.ulica,
|
};
|
||||||
response.psc,
|
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||||
response.mesto,
|
event_handler.command_message = message;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Invalid position - reset to first entry
|
// Invalid position - reset to first entry
|
||||||
app_state.current_position = 1;
|
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