gRPC implemented back
This commit is contained in:
@@ -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<CrosstermBackend<io::Stdout>>,
|
||||
grpc_client: AdresarClient<Channel>, // gRPC client
|
||||
}
|
||||
|
||||
impl AppTerminal {
|
||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
enable_raw_mode()?; // Moved before execute!
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
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<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
|
||||
@@ -39,37 +47,39 @@ impl AppTerminal {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
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))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
let mut app_terminal = AppTerminal::new()?;
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
}
|
||||
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<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
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),
|
||||
|
||||
@@ -14,7 +14,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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]"),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user