Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efb59c5571 | ||
|
|
26cf06df14 | ||
|
|
c82b62f72b | ||
|
|
87cdb8048d | ||
|
|
f71498703a | ||
|
|
94eea47b76 | ||
|
|
ac833294c4 | ||
|
|
81f0527085 | ||
|
|
dd4d9e88c6 | ||
|
|
f66d67c238 | ||
|
|
9cf25afa52 | ||
|
|
2ed2419f9e | ||
|
|
e19b30f4f4 | ||
|
|
f6e21b6a61 | ||
|
|
2180d8decf | ||
|
|
12aec72141 | ||
|
|
0568441e46 | ||
|
|
301189bd85 | ||
|
|
81767f376f | ||
|
|
e36b1817bc | ||
|
|
13d4db6bdc | ||
|
|
b5a5ebd7c0 | ||
|
|
6e04c1f267 | ||
|
|
a0d96cb87a | ||
|
|
4052a5b81b | ||
|
|
ed99ebf541 | ||
|
|
0a4f59cf8e | ||
|
|
fd6a9b73be | ||
|
|
dc994f6ee1 | ||
|
|
e234dd1785 | ||
|
|
6e0943f0cc | ||
|
|
9622e0bd3c | ||
|
|
ee666e91ed | ||
|
|
4f8f2f4a40 | ||
|
|
f21953147b | ||
|
|
48b2658b55 | ||
|
|
37b08fdd10 | ||
|
|
d4efdf4833 | ||
|
|
b408abe8c6 | ||
|
|
722c63af54 | ||
|
|
4601ba4094 | ||
|
|
3e2b8a36df | ||
|
|
11214734ae | ||
|
|
92045a4e67 | ||
|
|
9564bd8524 | ||
|
|
ad64df2ec3 | ||
|
|
aab11c1cba | ||
|
|
1fe139e0c5 |
@@ -4,3 +4,5 @@ RUST_DB_USER=multi_psql_dev
|
||||
RUST_DB_PASSWORD=3
|
||||
RUST_DB_HOST=localhost
|
||||
RUST_DB_PORT=5432
|
||||
|
||||
JWT_SECRET=<YOUR-JWT-HERE>
|
||||
|
||||
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -170,9 +170,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.87"
|
||||
version = "0.1.88"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d556ec1359574147ec0c4fc5eb525f3f23263a592b1a9c07e0a75b427de55c97"
|
||||
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -421,8 +421,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "client"
|
||||
version = "0.2.0"
|
||||
version = "0.2.5"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"common",
|
||||
"crossterm",
|
||||
"dirs 6.0.0",
|
||||
@@ -457,7 +458,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "common"
|
||||
version = "0.2.0"
|
||||
version = "0.2.5"
|
||||
dependencies = [
|
||||
"prost",
|
||||
"serde",
|
||||
@@ -2588,7 +2589,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "server"
|
||||
version = "0.2.0"
|
||||
version = "0.2.5"
|
||||
dependencies = [
|
||||
"bcrypt",
|
||||
"chrono",
|
||||
|
||||
@@ -5,7 +5,7 @@ resolver = "2"
|
||||
[workspace.package]
|
||||
# TODO: idk how to do the name, fix later
|
||||
# name = "Multieko2"
|
||||
version = "0.2.0"
|
||||
version = "0.2.5"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
authors = ["Filip Priečinský <filippriec@gmail.com>"]
|
||||
|
||||
@@ -5,6 +5,7 @@ edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
async-trait = "0.1.88"
|
||||
common = { path = "../common" }
|
||||
|
||||
crossterm = "0.28.1"
|
||||
|
||||
@@ -3,4 +3,3 @@ pub mod login;
|
||||
pub mod register;
|
||||
|
||||
pub use login::*;
|
||||
pub use register::*;
|
||||
|
||||
@@ -1,111 +1,146 @@
|
||||
// src/components/login/login.rs
|
||||
use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
|
||||
style::Style,
|
||||
text::{Line, Span, Text},
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
Frame,
|
||||
// src/components/auth/login.rs
|
||||
|
||||
use crate::{
|
||||
config::colors::themes::Theme,
|
||||
state::pages::auth::AuthState,
|
||||
components::common::dialog,
|
||||
state::state::AppState, // Add this import
|
||||
};
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Style, Modifier, Color}, // Removed unused Color import
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame, // Removed unused Span import
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
|
||||
pub struct LoginState {
|
||||
pub fields: Vec<String>,
|
||||
pub values: Vec<String>,
|
||||
pub selected_field: usize,
|
||||
}
|
||||
pub fn render_login(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
state: &AuthState,
|
||||
app_state: &AppState, // Add AppState parameter
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
// Main container
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Login ")
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
impl LoginState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: vec!["Username".to_string(), "Password".to_string()],
|
||||
values: vec![String::new(), String::new()],
|
||||
selected_field: 0,
|
||||
}
|
||||
}
|
||||
f.render_widget(block, area);
|
||||
|
||||
pub fn next_field(&mut self) {
|
||||
self.selected_field = (self.selected_field + 1) % self.fields.len();
|
||||
}
|
||||
let inner_area = area.inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
pub fn previous_field(&mut self) {
|
||||
self.selected_field = if self.selected_field == 0 {
|
||||
self.fields.len() - 1
|
||||
// Layout chunks
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(4), // Form (2 fields + padding)
|
||||
Constraint::Length(1), // Error message
|
||||
Constraint::Length(3), // Buttons
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// --- FORM RENDERING ---
|
||||
let input_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if is_edit_mode {
|
||||
Style::default().fg(theme.accent)
|
||||
} else {
|
||||
self.selected_field - 1
|
||||
};
|
||||
Style::default().fg(theme.border)
|
||||
})
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Calculate inner area BEFORE rendering
|
||||
let input_area = input_block.inner(chunks[0]);
|
||||
|
||||
f.render_widget(input_block, chunks[0]);
|
||||
|
||||
// Use the canvas renderer for fields
|
||||
crate::components::handlers::canvas::render_canvas(
|
||||
f,
|
||||
input_area, // Use the pre-calculated area
|
||||
state,
|
||||
&["Username/Email", "Password"],
|
||||
&state.current_field,
|
||||
&[&state.username, &state.password],
|
||||
theme,
|
||||
is_edit_mode,
|
||||
);
|
||||
|
||||
// --- BUTTONS --- (Keep this unchanged)
|
||||
let button_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
.split(chunks[2]);
|
||||
|
||||
// Login Button
|
||||
let login_active = !state.return_selected;
|
||||
let mut login_style = Style::default().fg(theme.fg);
|
||||
let mut login_border = Style::default().fg(theme.border);
|
||||
if login_active {
|
||||
login_style = login_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
|
||||
login_border = login_border.fg(theme.accent);
|
||||
}
|
||||
|
||||
pub fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
f.render_widget(
|
||||
Paragraph::new("Login")
|
||||
.style(login_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(login_border),
|
||||
),
|
||||
button_chunks[0],
|
||||
);
|
||||
|
||||
let inner_area = block.inner(area);
|
||||
f.render_widget(block, area);
|
||||
// Return Button
|
||||
let return_active = state.return_selected;
|
||||
let mut return_style = Style::default().fg(theme.fg);
|
||||
let mut return_border = Style::default().fg(theme.border);
|
||||
if return_active {
|
||||
return_style = return_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
|
||||
return_border = return_border.fg(theme.accent);
|
||||
}
|
||||
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(5),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.split(inner_area);
|
||||
f.render_widget(
|
||||
Paragraph::new("Return")
|
||||
.style(return_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(return_border),
|
||||
),
|
||||
button_chunks[1],
|
||||
);
|
||||
|
||||
// Title
|
||||
let title = Line::from(Span::styled("Login", Style::default().fg(theme.highlight)));
|
||||
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
|
||||
f.render_widget(title_widget, chunks[0]);
|
||||
// Error message
|
||||
if let Some(err) = &state.error_message {
|
||||
f.render_widget(
|
||||
Paragraph::new(err.as_str())
|
||||
.style(Style::default().fg(Color::Red))
|
||||
.alignment(Alignment::Center),
|
||||
chunks[1],
|
||||
);
|
||||
}
|
||||
|
||||
// Login form
|
||||
let form_chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(3),
|
||||
])
|
||||
.split(chunks[1]);
|
||||
|
||||
// Username field
|
||||
let username_block = Block::default()
|
||||
.title("Username")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 0 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let username = Paragraph::new(self.values[0].as_str())
|
||||
.block(username_block);
|
||||
f.render_widget(username, form_chunks[0]);
|
||||
|
||||
// Password field
|
||||
let password_block = Block::default()
|
||||
.title("Password")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 1 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let password = Paragraph::new("*".repeat(self.values[1].len()))
|
||||
.block(password_block);
|
||||
f.render_widget(password, form_chunks[1]);
|
||||
|
||||
// Submit button
|
||||
let submit_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 2 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let submit = Paragraph::new("Submit")
|
||||
.block(submit_block)
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(submit, form_chunks[2]);
|
||||
if app_state.ui.dialog.show_dialog {
|
||||
dialog::render_dialog(
|
||||
f,
|
||||
f.area(), // Use area() instead of deprecated size()
|
||||
theme,
|
||||
&app_state.ui.dialog.dialog_title,
|
||||
&app_state.ui.dialog.dialog_message,
|
||||
app_state.ui.dialog.dialog_button_active,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
pub mod command_line;
|
||||
pub mod status_line;
|
||||
pub mod background;
|
||||
pub mod dialog;
|
||||
|
||||
pub use command_line::*;
|
||||
pub use status_line::*;
|
||||
pub use background::*;
|
||||
pub use dialog::*;
|
||||
|
||||
101
client/src/components/common/dialog.rs
Normal file
101
client/src/components/common/dialog.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
// src/components/common/dialog.rs
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Rect, Margin},
|
||||
style::{Modifier, Style},
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
text::{Text, Line, Span}
|
||||
};
|
||||
use ratatui::prelude::Alignment;
|
||||
use crate::config::colors::themes::Theme;
|
||||
|
||||
pub fn render_dialog(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
title: &str,
|
||||
message: &str,
|
||||
is_active: bool,
|
||||
) {
|
||||
// Create a centered rect for the dialog
|
||||
let dialog_area = centered_rect(60, 25, area);
|
||||
|
||||
// Main dialog container
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.title(title)
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(&block, dialog_area);
|
||||
|
||||
// Inner content area
|
||||
let inner_area = block.inner(dialog_area).inner(Margin {
|
||||
horizontal: 2,
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
// Split into message and button areas
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(3), // Message content
|
||||
Constraint::Length(3), // Button
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Message text
|
||||
let message_text = Text::from(message.lines().map(|l| Line::from(Span::styled(
|
||||
l,
|
||||
Style::default().fg(theme.fg)
|
||||
))).collect::<Vec<_>>());
|
||||
|
||||
let message_paragraph = Paragraph::new(message_text)
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(message_paragraph, chunks[0]);
|
||||
|
||||
// OK Button
|
||||
let button_style = if is_active {
|
||||
Style::default()
|
||||
.fg(theme.highlight)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
};
|
||||
|
||||
let button_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new("OK")
|
||||
.block(button_block)
|
||||
.style(button_style)
|
||||
.alignment(Alignment::Center),
|
||||
chunks[1],
|
||||
);
|
||||
}
|
||||
|
||||
/// Helper function to center a rect with given percentage values
|
||||
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||
let popup_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
Constraint::Percentage(percent_y),
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
])
|
||||
.split(r);
|
||||
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
Constraint::Percentage(percent_x),
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
])
|
||||
.split(popup_layout[1])[1]
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/components/handlers/form.rs
|
||||
// src/components/form/form.rs
|
||||
use ratatui::{
|
||||
widgets::{Paragraph, Block, Borders},
|
||||
layout::{Layout, Constraint, Direction, Rect, Margin, Alignment},
|
||||
@@ -6,13 +6,13 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
|
||||
pub fn render_form(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &FormState,
|
||||
form_state: &impl CanvasState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
inputs: &[&String],
|
||||
|
||||
@@ -8,12 +8,12 @@ use ratatui::{
|
||||
prelude::Alignment,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
pub fn render_canvas(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &FormState,
|
||||
form_state: &impl CanvasState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
inputs: &[&String],
|
||||
@@ -30,7 +30,7 @@ pub fn render_canvas(
|
||||
let input_container = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if is_edit_mode {
|
||||
form_state.has_unsaved_changes.then(|| theme.warning).unwrap_or(theme.accent)
|
||||
form_state.has_unsaved_changes().then(|| theme.warning).unwrap_or(theme.accent)
|
||||
} else {
|
||||
theme.secondary
|
||||
})
|
||||
@@ -81,7 +81,7 @@ pub fn render_canvas(
|
||||
f.render_widget(input_display, input_rows[i]);
|
||||
|
||||
if is_active {
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos() as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y));
|
||||
}
|
||||
|
||||
@@ -32,9 +32,9 @@ impl IntroState {
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage(35),
|
||||
Constraint::Length(7), // Increased to accommodate 3 buttons
|
||||
Constraint::Percentage(35),
|
||||
Constraint::Percentage(40),
|
||||
Constraint::Length(5), // Increased to accommodate 3 buttons
|
||||
Constraint::Percentage(40),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
@@ -118,16 +118,4 @@ impl IntroState {
|
||||
pub fn previous_option(&mut self) {
|
||||
self.selected_option = if self.selected_option == 0 { 2 } else { self.selected_option - 1 };
|
||||
}
|
||||
|
||||
pub fn handle_selection(&self, app_state: &mut crate::state::state::AppState) {
|
||||
match self.selected_option {
|
||||
0 => { /* Continue logic */ }
|
||||
1 => { /* Admin logic */ }
|
||||
2 => {
|
||||
app_state.ui.show_intro = false;
|
||||
app_state.ui.show_login = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ pub mod config;
|
||||
pub mod state;
|
||||
pub mod components;
|
||||
pub mod modes;
|
||||
pub mod services;
|
||||
|
||||
pub use ui::run_ui;
|
||||
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
// src/modes/canvas/common.rs
|
||||
|
||||
use crossterm::event::{KeyEvent};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::tui::controls::commands::CommandHandler;
|
||||
use crate::state::pages::form::FormState;
|
||||
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
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
command_handler: &mut CommandHandler,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
current_position: &mut u64,
|
||||
@@ -55,127 +51,6 @@ pub async fn handle_core_action(
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
},
|
||||
// We should never hit this case with proper filtering
|
||||
_ => 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
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::tui::terminal::{
|
||||
grpc_client::GrpcClient,
|
||||
};
|
||||
use crate::config::binds::config::Config;
|
||||
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(
|
||||
key: KeyEvent,
|
||||
@@ -69,7 +67,7 @@ async fn execute_common_action(
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
common::save(
|
||||
save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
is_saved,
|
||||
@@ -78,7 +76,7 @@ async fn execute_common_action(
|
||||
).await
|
||||
},
|
||||
"revert" => {
|
||||
common::revert(
|
||||
revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
@@ -179,7 +177,7 @@ async fn execute_edit_action(
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
common::save(
|
||||
save(
|
||||
form_state,
|
||||
grpc_client, // Changed from AppTerminal
|
||||
is_saved,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// src/modes/handlers/read_only.rs
|
||||
// src/modes/canvas/read_only.rs
|
||||
|
||||
use crossterm::event::{KeyEvent};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
@@ -14,9 +15,11 @@ enum CharType {
|
||||
}
|
||||
|
||||
pub async fn handle_read_only_event(
|
||||
app_state: &crate::state::state::AppState,
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
key_sequence_tracker: &mut KeySequenceTracker,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
@@ -50,16 +53,34 @@ pub async fn handle_read_only_event(
|
||||
|
||||
// Try to match the current sequence against Read-Only mode bindings
|
||||
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
|
||||
let result = execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?;
|
||||
let result = if (action == "previous_entry" || action == "next_entry" ||
|
||||
action == "move_up" || action == "move_down") && app_state.ui.show_form {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
} else if (action == "move_up" || action == "move_down") && app_state.ui.show_login {
|
||||
crate::tui::functions::login::handle_action(
|
||||
action,
|
||||
auth_state,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
} else {
|
||||
execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
};
|
||||
key_sequence_tracker.reset();
|
||||
return Ok((false, result));
|
||||
}
|
||||
@@ -72,16 +93,27 @@ pub async fn handle_read_only_event(
|
||||
// Since it's not part of a multi-key sequence, check for a direct action
|
||||
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
||||
let result = execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?;
|
||||
let result = if action == "previous_entry" && app_state.ui.show_form {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
} else {
|
||||
execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
};
|
||||
key_sequence_tracker.reset();
|
||||
return Ok((false, result));
|
||||
}
|
||||
@@ -91,16 +123,27 @@ pub async fn handle_read_only_event(
|
||||
key_sequence_tracker.reset();
|
||||
|
||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
||||
let result = execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?;
|
||||
let result = if action == "previous_entry" && app_state.ui.show_form {
|
||||
crate::tui::functions::form::handle_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
ideal_cursor_column,
|
||||
).await?
|
||||
} else {
|
||||
execute_action(
|
||||
action,
|
||||
form_state,
|
||||
ideal_cursor_column,
|
||||
key_sequence_tracker,
|
||||
command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
).await?
|
||||
};
|
||||
return Ok((false, result));
|
||||
}
|
||||
}
|
||||
@@ -128,72 +171,15 @@ async fn execute_action(
|
||||
grpc_client: &mut GrpcClient,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
let new_position = current_position.saturating_sub(1);
|
||||
if new_position >= 1 {
|
||||
*current_position = new_position;
|
||||
match grpc_client.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
form_state.id = response.id;
|
||||
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 !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else { 0 };
|
||||
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
*command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
*command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
key_sequence_tracker.reset();
|
||||
}
|
||||
Ok(command_message.clone())
|
||||
"previous_entry" | "next_entry" => {
|
||||
// This will only be called when no component is active
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Navigation prev/next only available in form mode"))
|
||||
}
|
||||
"next_entry" => {
|
||||
if *current_position <= total_count {
|
||||
*current_position += 1;
|
||||
if *current_position <= total_count {
|
||||
match grpc_client.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
form_state.id = response.id;
|
||||
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 !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else { 0 };
|
||||
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
*command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
*command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
form_state.current_cursor_pos = 0;
|
||||
*ideal_cursor_column = 0;
|
||||
*command_message = "New entry mode".to_string();
|
||||
}
|
||||
key_sequence_tracker.reset();
|
||||
}
|
||||
Ok(command_message.clone())
|
||||
"move_up" | "move_down" => {
|
||||
// This will only be called when no component is active
|
||||
key_sequence_tracker.reset();
|
||||
Ok(format!("Navigation up/down only available in form mode"))
|
||||
}
|
||||
"exit_edit_mode" => {
|
||||
key_sequence_tracker.reset();
|
||||
@@ -215,38 +201,6 @@ async fn execute_action(
|
||||
}
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_up" => {
|
||||
// Change field first
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
|
||||
// Get current input AFTER changing field
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_down" => {
|
||||
// Change field first
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
|
||||
// Get current input AFTER changing field
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_word_next" => {
|
||||
let current_input = form_state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
// src/modes/handlers/command_mode.rs
|
||||
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
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::modes::{
|
||||
canvas::{common},
|
||||
};
|
||||
use crate::tui::functions::common::form::{save, revert};
|
||||
|
||||
pub async fn handle_command_event(
|
||||
key: KeyEvent,
|
||||
@@ -97,7 +95,7 @@ async fn process_command(
|
||||
Ok((should_exit, message, true))
|
||||
},
|
||||
"save" => {
|
||||
let message = common::save(
|
||||
let message = save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut command_handler.is_saved,
|
||||
@@ -108,7 +106,7 @@ async fn process_command(
|
||||
return Ok((false, message, true));
|
||||
},
|
||||
"revert" => {
|
||||
let message = common::revert(
|
||||
let message = revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
|
||||
@@ -4,6 +4,7 @@ use crossterm::event::KeyEvent;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::state::AppState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::tui::functions::{intro, admin};
|
||||
|
||||
pub async fn handle_navigation_event(
|
||||
key: KeyEvent,
|
||||
@@ -21,16 +22,11 @@ pub async fn handle_navigation_event(
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"move_down" => {
|
||||
let item_count = if app_state.ui.show_intro {
|
||||
2 // Intro options count
|
||||
} else {
|
||||
app_state.profile_tree.profiles.len() // Admin panel items
|
||||
};
|
||||
move_down(app_state, item_count);
|
||||
move_down(app_state);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"next_option" => {
|
||||
next_option(app_state, 2); // Intro has 2 options
|
||||
next_option(app_state); // Intro has 2 options
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"previous_option" => {
|
||||
@@ -84,7 +80,7 @@ pub fn move_up(app_state: &mut AppState) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_down(app_state: &mut AppState, item_count: usize) {
|
||||
pub fn move_down(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
@@ -98,11 +94,12 @@ pub fn move_down(app_state: &mut AppState, item_count: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_option(app_state: &mut AppState, option_count: usize) {
|
||||
pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
} else {
|
||||
// For other screens that might have options
|
||||
// Get option count from state instead of parameter
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
app_state.general.current_option = (app_state.general.current_option + 1) % option_count;
|
||||
}
|
||||
}
|
||||
@@ -111,36 +108,20 @@ pub fn previous_option(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.previous_option();
|
||||
} else {
|
||||
// For other screens that might have options
|
||||
if app_state.general.current_option == 0 {
|
||||
// We'd need the option count here, but since it's not passed we can't wrap around correctly
|
||||
// For now, just stay at 0
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
app_state.general.current_option = if app_state.general.current_option == 0 {
|
||||
option_count.saturating_sub(1) // Wrap to last option
|
||||
} else {
|
||||
app_state.general.current_option -= 1;
|
||||
}
|
||||
app_state.general.current_option - 1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
// Handle selection in intro screen
|
||||
if app_state.ui.intro_state.selected_option == 0 {
|
||||
// First option selected - show form
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
} else {
|
||||
// Second option selected - show admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
}
|
||||
app_state.ui.show_intro = false;
|
||||
intro::handle_intro_selection(app_state);
|
||||
} else if app_state.ui.show_admin {
|
||||
// Handle selection in admin panel
|
||||
let profiles = &app_state.profile_tree.profiles;
|
||||
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
|
||||
// Set the selected profile
|
||||
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
|
||||
}
|
||||
admin::handle_admin_selection(app_state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
// src/modes/handlers/event.rs
|
||||
use crossterm::event::{Event, KeyEvent};
|
||||
use crossterm::event::Event;
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
use crate::tui::terminal::{
|
||||
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::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::ui::handlers::rat_state::UiStateHandler;
|
||||
use crate::modes::{
|
||||
common::{command_mode},
|
||||
@@ -25,6 +26,7 @@ pub struct EventHandler {
|
||||
pub edit_mode_cooldown: bool,
|
||||
pub ideal_cursor_column: usize,
|
||||
pub key_sequence_tracker: KeySequenceTracker,
|
||||
pub auth_state: AuthState,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
@@ -37,6 +39,7 @@ impl EventHandler {
|
||||
edit_mode_cooldown: false,
|
||||
ideal_cursor_column: 0,
|
||||
key_sequence_tracker: KeySequenceTracker::new(800),
|
||||
auth_state: AuthState::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +132,6 @@ impl EventHandler {
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
command_handler,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
@@ -142,9 +144,11 @@ impl EventHandler {
|
||||
|
||||
// Let read_only mode handle its own actions (including navigation from common bindings)
|
||||
return read_only::handle_read_only_event(
|
||||
&app_state,
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
&mut self.auth_state,
|
||||
&mut self.key_sequence_tracker,
|
||||
current_position,
|
||||
total_count,
|
||||
@@ -188,7 +192,6 @@ impl EventHandler {
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
command_handler,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
|
||||
@@ -15,14 +15,16 @@ pub struct ModeManager;
|
||||
impl ModeManager {
|
||||
// Determine current mode based on app state
|
||||
pub fn derive_mode(app_state: &AppState, event_handler: &EventHandler) -> AppMode {
|
||||
// Command mode takes precedence if active
|
||||
if event_handler.command_mode {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
// Check UI state flags
|
||||
if app_state.ui.show_intro || app_state.ui.show_admin {
|
||||
AppMode::General
|
||||
if app_state.ui.show_login { // NEW: Check auth visibility
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
} else if app_state.ui.show_form {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
@@ -30,11 +32,10 @@ impl ModeManager {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
} else {
|
||||
// Fallback
|
||||
AppMode::General
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Mode transition rules
|
||||
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
||||
!matches!(current_mode, AppMode::Edit) // Can't enter from Edit mode
|
||||
|
||||
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 common::proto::multieko2::adresar::adresar_client::AdresarClient;
|
||||
@@ -11,6 +11,7 @@ use common::proto::multieko2::table_definition::{
|
||||
ProfileTreeResponse
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GrpcClient {
|
||||
adresar_client: AdresarClient<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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
49
client/src/state/canvas_state.rs
Normal file
49
client/src/state/canvas_state.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
// src/state/canvas_state.rs
|
||||
|
||||
use crate::state::pages::form::FormState;
|
||||
|
||||
pub trait CanvasState {
|
||||
fn current_field(&self) -> usize;
|
||||
fn current_cursor_pos(&self) -> usize;
|
||||
fn has_unsaved_changes(&self) -> bool;
|
||||
fn inputs(&self) -> Vec<&String>;
|
||||
fn get_current_input(&self) -> &str;
|
||||
fn get_current_input_mut(&mut self) -> &mut String;
|
||||
fn fields(&self) -> Vec<&str>;
|
||||
}
|
||||
|
||||
// Implement for FormState (keep existing form.rs code and add this)
|
||||
impl CanvasState for FormState {
|
||||
fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
self.values.iter().collect()
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
self.values
|
||||
.get(self.current_field)
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.values
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
self.fields.iter().map(|s| s.as_str()).collect()
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/state/mod.rs
|
||||
pub mod state;
|
||||
pub mod pages;
|
||||
pub mod canvas_state;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/state/pages.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod auth;
|
||||
|
||||
70
client/src/state/pages/auth.rs
Normal file
70
client/src/state/pages/auth.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
// src/state/pages/auth.rs
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AuthState {
|
||||
pub return_selected: bool,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub error_message: Option<String>,
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub auth_token: Option<String>,
|
||||
pub user_id: Option<String>,
|
||||
pub role: Option<String>,
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
return_selected: false,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
auth_token: None,
|
||||
user_id: None,
|
||||
role: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasState for AuthState {
|
||||
fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
// Auth form doesn't need unsaved changes tracking
|
||||
false
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![&self.username, &self.password]
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "", // Return empty string for invalid index instead of panicking
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_field {
|
||||
0 => &mut self.username,
|
||||
1 => &mut self.password,
|
||||
_ => panic!("Invalid current_field index in AuthState"),
|
||||
}
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec!["Username/Email", "Password"]
|
||||
}
|
||||
}
|
||||
@@ -70,4 +70,15 @@ impl FormState {
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
|
||||
pub fn update_from_response(&mut self, response: common::proto::multieko2::adresar::AdresarResponse) {
|
||||
self.id = response.id;
|
||||
self.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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,14 @@ use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||
use crate::components::IntroState;
|
||||
use crate::modes::handlers::mode_manager::AppMode;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DialogState {
|
||||
pub show_dialog: bool,
|
||||
pub dialog_title: String,
|
||||
pub dialog_message: String,
|
||||
pub dialog_button_active: bool,
|
||||
}
|
||||
|
||||
pub struct UiState {
|
||||
pub show_sidebar: bool,
|
||||
pub is_saved: bool,
|
||||
@@ -13,6 +21,7 @@ pub struct UiState {
|
||||
pub show_form: bool,
|
||||
pub show_login: bool,
|
||||
pub intro_state: IntroState,
|
||||
pub dialog: DialogState, // Add dialog state here
|
||||
}
|
||||
|
||||
pub struct GeneralState {
|
||||
@@ -66,6 +75,24 @@ impl AppState {
|
||||
pub fn update_mode(&mut self, mode: AppMode) {
|
||||
self.current_mode = mode;
|
||||
}
|
||||
|
||||
// Add dialog helper methods
|
||||
pub fn show_dialog(&mut self, title: &str, message: &str) {
|
||||
self.ui.dialog.show_dialog = true;
|
||||
self.ui.dialog.dialog_title = title.to_string();
|
||||
self.ui.dialog.dialog_message = message.to_string();
|
||||
self.ui.dialog.dialog_button_active = true;
|
||||
}
|
||||
|
||||
pub fn hide_dialog(&mut self) {
|
||||
self.ui.dialog.show_dialog = false;
|
||||
self.ui.dialog.dialog_title.clear();
|
||||
self.ui.dialog.dialog_message.clear();
|
||||
}
|
||||
|
||||
pub fn set_dialog_button_active(&mut self, active: bool) {
|
||||
self.ui.dialog.dialog_button_active = active;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UiState {
|
||||
@@ -78,6 +105,7 @@ impl Default for UiState {
|
||||
show_form: false,
|
||||
show_login: false,
|
||||
intro_state: IntroState::new(),
|
||||
dialog: DialogState::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
// src/tui/controls.rs
|
||||
|
||||
pub mod commands;
|
||||
|
||||
pub use commands::*;
|
||||
12
client/src/tui/functions.rs
Normal file
12
client/src/tui/functions.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
// src/tui/functions.rs
|
||||
|
||||
pub mod admin;
|
||||
pub mod intro;
|
||||
pub mod login;
|
||||
pub mod form;
|
||||
pub mod common;
|
||||
|
||||
pub use admin::*;
|
||||
pub use intro::*;
|
||||
pub use form::*;
|
||||
pub use common::*;
|
||||
8
client/src/tui/functions/admin.rs
Normal file
8
client/src/tui/functions/admin.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub fn handle_admin_selection(app_state: &mut AppState) {
|
||||
let profiles = &app_state.profile_tree.profiles;
|
||||
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
|
||||
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
|
||||
}
|
||||
}
|
||||
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())
|
||||
}
|
||||
111
client/src/tui/functions/form.rs
Normal file
111
client/src/tui/functions/form.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
// src/tui/functions/form.rs
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
let new_position = current_position.saturating_sub(1);
|
||||
if new_position >= 1 {
|
||||
*current_position = new_position;
|
||||
let response = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||
|
||||
// Direct field assignments
|
||||
form_state.id = response.id;
|
||||
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 !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else { 0 };
|
||||
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
|
||||
Ok(format!("Loaded form entry {}", *current_position))
|
||||
} else {
|
||||
Ok("Already at first form entry".into())
|
||||
}
|
||||
}
|
||||
"next_entry" => {
|
||||
if *current_position <= total_count {
|
||||
*current_position += 1;
|
||||
if *current_position <= total_count {
|
||||
let response = grpc_client.get_adresar_by_position(*current_position).await?;
|
||||
|
||||
// Direct field assignments
|
||||
form_state.id = response.id;
|
||||
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 !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else { 0 };
|
||||
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
|
||||
form_state.has_unsaved_changes = false;
|
||||
|
||||
Ok(format!("Loaded form entry {}", *current_position))
|
||||
} else {
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
form_state.current_cursor_pos = 0;
|
||||
*ideal_cursor_column = 0;
|
||||
Ok("New form entry mode".into())
|
||||
}
|
||||
} else {
|
||||
Ok("Already at last entry".into())
|
||||
}
|
||||
}
|
||||
"move_up" => {
|
||||
// Change field first
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
|
||||
// Get current input AFTER changing field
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
"move_down" => {
|
||||
// Change field first
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
|
||||
// Get current input AFTER changing field
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
Ok("".to_string())
|
||||
}
|
||||
_ => Err("Unknown form action".into())
|
||||
}
|
||||
}
|
||||
23
client/src/tui/functions/intro.rs
Normal file
23
client/src/tui/functions/intro.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub fn handle_intro_selection(app_state: &mut AppState) {
|
||||
match app_state.ui.intro_state.selected_option {
|
||||
0 => { // Continue
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = false;
|
||||
}
|
||||
1 => { // Admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
app_state.ui.show_login = false;
|
||||
}
|
||||
2 => { // Login
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
app_state.ui.show_intro = false;
|
||||
}
|
||||
64
client/src/tui/functions/login.rs
Normal file
64
client/src/tui/functions/login.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
// src/tui/functions/login.rs
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
auth_state: &mut AuthState,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"move_up" => {
|
||||
if auth_state.return_selected {
|
||||
// From Return button to last field (password)
|
||||
auth_state.return_selected = false;
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 1 {
|
||||
// Password -> Username
|
||||
auth_state.current_field = 0;
|
||||
} else if auth_state.current_field == 0 {
|
||||
// Username -> Password (wrap around fields only)
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 2 {
|
||||
// From Login button to Password field
|
||||
auth_state.current_field = 1;
|
||||
}
|
||||
|
||||
// Update cursor position only when in a field
|
||||
if auth_state.current_field < 2 {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = current_input.len();
|
||||
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
}
|
||||
|
||||
Ok(format!("Navigation 'up' from functions/login"))
|
||||
}
|
||||
"move_down" => {
|
||||
if auth_state.return_selected {
|
||||
// From Return button to first field (username)
|
||||
auth_state.return_selected = false;
|
||||
auth_state.current_field = 0;
|
||||
} else if auth_state.current_field == 0 {
|
||||
// Username -> Password
|
||||
auth_state.current_field = 1;
|
||||
} else if auth_state.current_field == 1 {
|
||||
// Password -> Login button
|
||||
auth_state.current_field = 2;
|
||||
auth_state.return_selected = false;
|
||||
} else if auth_state.current_field == 2 {
|
||||
// Login button -> Return button
|
||||
auth_state.return_selected = true;
|
||||
}
|
||||
|
||||
// Update cursor position only when in a field
|
||||
if auth_state.current_field < 2 {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = current_input.len();
|
||||
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||
}
|
||||
|
||||
Ok(format!("Navigation 'down' from functions/login"))
|
||||
}
|
||||
_ => Err("Unknown login action".into())
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
// src/tui/mod.rs
|
||||
pub mod terminal;
|
||||
pub mod controls;
|
||||
pub mod functions;
|
||||
|
||||
pub use functions::*;
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
// src/tui/terminal.rs
|
||||
|
||||
pub mod core;
|
||||
pub mod grpc_client;
|
||||
pub mod event_reader;
|
||||
|
||||
pub use core::TerminalCore;
|
||||
pub use grpc_client::GrpcClient;
|
||||
pub use event_reader::EventReader;
|
||||
|
||||
@@ -6,18 +6,20 @@ use crate::components::{
|
||||
render_status_line,
|
||||
handlers::sidebar::{self, calculate_sidebar_layout},
|
||||
form::form::render_form,
|
||||
intro::{intro},
|
||||
admin::{admin_panel::AdminPanelState},
|
||||
auth::login::render_login,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::Frame;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
@@ -44,6 +46,15 @@ pub fn render_ui(
|
||||
if app_state.ui.show_intro {
|
||||
// Use app_state's intro_state directly
|
||||
app_state.ui.intro_state.render(f, main_content_area, theme);
|
||||
}else if app_state.ui.show_login {
|
||||
render_login(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
auth_state,
|
||||
app_state, // Add AppState reference here
|
||||
auth_state.current_field < 2
|
||||
);
|
||||
} else if app_state.ui.show_admin {
|
||||
// Create temporary AdminPanelState for rendering
|
||||
let mut admin_state = AdminPanelState::new(
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
// src/ui/handlers/ui.rs
|
||||
|
||||
use crate::tui::terminal::TerminalCore;
|
||||
use crate::tui::terminal::GrpcClient;
|
||||
use crate::tui::controls::CommandHandler;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::ui_service::UiService; // Add this import
|
||||
use crate::tui::terminal::EventReader;
|
||||
use crate::tui::functions::common::CommandHandler;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::state::AppState;
|
||||
use crate::components::admin::{admin_panel::AdminPanelState};
|
||||
use crate::components::intro::{intro::IntroState};
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
@@ -19,31 +19,13 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut grpc_client = GrpcClient::new().await?;
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut intro_state = IntroState::new();
|
||||
let mut auth_state = AuthState::default();
|
||||
|
||||
// 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;
|
||||
|
||||
// Now create admin panel with profiles from app_state
|
||||
if intro_state.selected_option == 1 {
|
||||
app_state.ui.show_admin = true;
|
||||
app_state.general.selected_item = 0;
|
||||
app_state.general.current_option = 0;
|
||||
}
|
||||
|
||||
// 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);
|
||||
@@ -53,19 +35,17 @@ 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(
|
||||
f,
|
||||
&mut form_state,
|
||||
&mut auth_state,
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
app_state.total_count,
|
||||
@@ -116,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;
|
||||
@@ -166,3 +124,4 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
22
server/.sqlx/query-468bb5bb4fdaefcb4b280761d7880a556d40c172568ad3a1ed13156fbef72776.json
generated
Normal file
22
server/.sqlx/query-468bb5bb4fdaefcb4b280761d7880a556d40c172568ad3a1ed13156fbef72776.json
generated
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT ltd.table_name\n FROM table_definition_links tdl\n JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id\n WHERE tdl.source_table_id = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "table_name",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "468bb5bb4fdaefcb4b280761d7880a556d40c172568ad3a1ed13156fbef72776"
|
||||
}
|
||||
42
server/.sqlx/query-48d0a6d393dac121bfa4230830a105aede2179b07395f97750ab2fa1970afacd.json
generated
Normal file
42
server/.sqlx/query-48d0a6d393dac121bfa4230830a105aede2179b07395f97750ab2fa1970afacd.json
generated
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO users (username, email, password_hash, role)\n VALUES ($1, $2, $3, 'accountant')\n RETURNING id, username, email, role\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "username",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "email",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "role",
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Varchar",
|
||||
"Varchar",
|
||||
"Varchar"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "48d0a6d393dac121bfa4230830a105aede2179b07395f97750ab2fa1970afacd"
|
||||
}
|
||||
34
server/.sqlx/query-6be0bb23ad1ba85add309f6e4f55495a5d248901fb0d23fea908815747a0bd50.json
generated
Normal file
34
server/.sqlx/query-6be0bb23ad1ba85add309f6e4f55495a5d248901fb0d23fea908815747a0bd50.json
generated
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT id, password_hash, role\n FROM users\n WHERE username = $1 OR email = $1\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "password_hash",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "role",
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "6be0bb23ad1ba85add309f6e4f55495a5d248901fb0d23fea908815747a0bd50"
|
||||
}
|
||||
23
server/.sqlx/query-80f0f7f9ab12a8fe07ea71b548d27bcd6253b598ac4486b72b3d960f03df47f3.json
generated
Normal file
23
server/.sqlx/query-80f0f7f9ab12a8fe07ea71b548d27bcd6253b598ac4486b72b3d960f03df47f3.json
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT table_name FROM table_definitions\n WHERE profile_id = $1 AND table_name LIKE $2",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "table_name",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "80f0f7f9ab12a8fe07ea71b548d27bcd6253b598ac4486b72b3d960f03df47f3"
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT ltd.table_name \n FROM table_definition_links tdl\n JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id\n WHERE tdl.source_table_id = $1 AND tdl.is_required = true",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "table_name",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "b097f30f98490b979939759d85327a20ca7ade4866052a5cfdb0451fb76fbf15"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "INSERT INTO table_scripts\n (table_definitions_id, target_column, target_column_type, script, description)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING id",
|
||||
"query": "INSERT INTO table_scripts\n (table_definitions_id, target_table, target_column,\n target_column_type, script, description, profile_id)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n RETURNING id",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -15,12 +15,14 @@
|
||||
"Text",
|
||||
"Text",
|
||||
"Text",
|
||||
"Text"
|
||||
"Text",
|
||||
"Text",
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "4cd5de4d3332ca35a9975ffb32728041978435e14f97c559e2ffec4a82d567ae"
|
||||
"hash": "c07a8511e5f32bf230240b28cb292d40f862b6ec58883f21ee8e1937860585d6"
|
||||
}
|
||||
Reference in New Issue
Block a user