diff --git a/src/client/terminal.rs b/src/client/terminal.rs index 523bc9b..391c654 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -6,19 +6,27 @@ use crossterm::{ }; use ratatui::{backend::CrosstermBackend, Terminal}; use std::io::{self, stdout}; +use tonic::transport::Channel; +use crate::proto::multieko2::adresar_client::AdresarClient; +use crate::proto::multieko2::AdresarRequest; pub struct AppTerminal { terminal: Terminal>, + grpc_client: AdresarClient, // gRPC client } impl AppTerminal { - pub fn new() -> Result> { - enable_raw_mode()?; // Moved before execute! + pub async fn new() -> Result> { + enable_raw_mode()?; let mut stdout = stdout(); execute!(stdout, EnterAlternateScreen)?; let backend = CrosstermBackend::new(stdout); let terminal = Terminal::new(backend)?; - Ok(Self { terminal }) + + // Initialize gRPC client + let grpc_client = AdresarClient::connect("http://[::1]:50051").await?; + + Ok(Self { terminal, grpc_client }) } pub fn draw(&mut self, f: F) -> Result<(), Box> @@ -39,37 +47,39 @@ impl AppTerminal { Ok(()) } - pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box> { + pub async fn handle_command( + &mut self, + command_input: &str, + is_saved: &mut bool, + form_data: &AdresarRequest, // Pass form data here + ) -> Result<(bool, String), Box> { match command_input { "w" => { - // Save the state + // Send data to the server + let request = tonic::Request::new(form_data.clone()); + let response = self.grpc_client.create_adresar(request).await?; + *is_saved = true; - Ok((false, "State saved.".to_string())) // Do not exit + Ok((false, format!("State saved. Response: {:?}", response))) } "q" => { - // Quit if saved if *is_saved { self.cleanup()?; - Ok((true, "Exiting.".to_string())) // Exit + Ok((true, "Exiting.".to_string())) } else { - Ok((false, "No changes saved. Use :q! to force quit.".to_string())) // Do not exit + Ok((false, "No changes saved. Use :q! to force quit.".to_string())) } } "q!" => { - // Force quit without saving self.cleanup()?; - Ok((true, "Force exiting without saving.".to_string())) // Exit + Ok((true, "Force exiting without saving.".to_string())) } "wq" => { - // Save and quit *is_saved = true; self.cleanup()?; - Ok((true, "State saved. Exiting.".to_string())) // Exit - } - _ => { - // Handle other commands here - Ok((false, format!("Command not recognized: {}", command_input))) // Do not exit + Ok((true, "State saved. Exiting.".to_string())) } + _ => Ok((false, format!("Command not recognized: {}", command_input))), } } } diff --git a/src/client/ui.rs b/src/client/ui.rs index fa6117f..5cb6a2b 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -5,9 +5,10 @@ use crate::client::components::{render_command_line, render_form, render_preview use crate::client::colors::Theme; use ratatui::layout::{Constraint, Direction, Layout, Rect}; use std::env; +use crate::proto::multieko2::AdresarRequest; -pub fn run_ui() -> Result<(), Box> { - let mut app_terminal = AppTerminal::new()?; +pub async fn run_ui() -> Result<(), Box> { + let mut app_terminal = AppTerminal::new().await?; let mut command_mode = false; let mut command_input = String::new(); let theme = Theme::dark(); @@ -98,9 +99,28 @@ pub fn run_ui() -> Result<(), Box> { if command_mode { match key.code { KeyCode::Enter => { - // Handle the command - let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved)?; - command_message = message; // Set the message to display + // Create AdresarRequest from form data + let form_data = AdresarRequest { + firma: firma.clone(), + kz: kz.clone(), + drc: drc.clone(), + ulica: ulica.clone(), + psc: psc.clone(), + mesto: mesto.clone(), + stat: stat.clone(), + banka: banka.clone(), + ucet: ucet.clone(), + skladm: skladm.clone(), + ico: ico.clone(), + kontakt: kontakt.clone(), + telefon: telefon.clone(), + skladu: skladu.clone(), + fax: fax.clone(), + }; + + // Pass form data to handle_command + let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved, &form_data).await?; + command_message = message; if should_exit { return Ok(()); } @@ -109,12 +129,12 @@ pub fn run_ui() -> Result<(), Box> { } KeyCode::Char(c) => command_input.push(c), KeyCode::Backspace => { - command_input.pop(); + command_input.pop(); // Ignore the return value } KeyCode::Esc => { command_mode = false; command_input.clear(); - command_message.clear(); // Clear the message when exiting command mode + command_message.clear(); } _ => {} } @@ -123,7 +143,7 @@ pub fn run_ui() -> Result<(), Box> { KeyCode::Char(':') => { command_mode = true; command_input.clear(); - command_message.clear(); // Clear the message when entering command mode + command_message.clear(); } KeyCode::Tab => { if key.modifiers.contains(KeyModifiers::SHIFT) { @@ -135,10 +155,7 @@ pub fn run_ui() -> Result<(), Box> { KeyCode::BackTab => current_field = current_field.saturating_sub(1), KeyCode::Down => current_field = (current_field + 1) % fields.len(), KeyCode::Up => current_field = current_field.saturating_sub(1), - KeyCode::Enter => { - // Move to the next field, wrapping around to the first field if necessary - current_field = (current_field + 1) % fields.len(); - } + KeyCode::Enter => current_field = (current_field + 1) % fields.len(), KeyCode::Char(c) => { match current_field { 0 => firma.push(c), diff --git a/src/main.rs b/src/main.rs index 6e7474a..141355a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ async fn main() -> Result<(), Box> { match env::args().nth(1).as_deref() { Some("server") => server::run_server(db_pool).await?, // Some("client") => client::run_client().await?, - Some("client") => client::run_ui()?, + Some("client") => client::run_ui().await?, _ => println!("Usage: cargo run -- [server|client]"), }