Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
060cff3f0f | ||
|
|
07c48985e4 | ||
|
|
3d266c2ad0 | ||
|
|
e2c326bf1e | ||
|
|
7f5b671084 | ||
|
|
3ccf71ed0f |
@@ -1,16 +1,21 @@
|
|||||||
// src/modes/canvas/common.rs
|
// src/modes/canvas/common.rs
|
||||||
|
|
||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::{form::FormState, auth::AuthState};
|
||||||
use crate::state::state::AppState;
|
use crate::state::state::AppState;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::tui::functions::common::form::{save, revert};
|
use crate::services::auth::AuthClient;
|
||||||
|
use crate::tui::functions::common::{
|
||||||
|
form::{save as form_save, revert},
|
||||||
|
login::{save as login_save, cancel}
|
||||||
|
};
|
||||||
|
|
||||||
/// Main handler for common core actions
|
|
||||||
pub async fn handle_core_action(
|
pub async fn handle_core_action(
|
||||||
action: &str,
|
action: &str,
|
||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
|
auth_state: &mut AuthState,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
|
auth_client: &mut AuthClient,
|
||||||
terminal: &mut TerminalCore,
|
terminal: &mut TerminalCore,
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
@@ -18,38 +23,52 @@ pub async fn handle_core_action(
|
|||||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||||
match action {
|
match action {
|
||||||
"save" => {
|
"save" => {
|
||||||
let message = save(
|
if app_state.ui.show_login {
|
||||||
form_state,
|
let message = login_save(auth_state, auth_client, app_state).await?;
|
||||||
grpc_client,
|
Ok((false, message))
|
||||||
&mut app_state.ui.is_saved,
|
} else {
|
||||||
current_position,
|
let message = form_save(
|
||||||
total_count,
|
form_state,
|
||||||
).await?;
|
grpc_client,
|
||||||
Ok((false, message))
|
&mut app_state.ui.is_saved,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?;
|
||||||
|
Ok((false, message))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"force_quit" => {
|
"force_quit" => {
|
||||||
terminal.cleanup()?;
|
terminal.cleanup()?;
|
||||||
Ok((true, "Force exiting without saving.".to_string()))
|
Ok((true, "Force exiting without saving.".to_string()))
|
||||||
},
|
},
|
||||||
"save_and_quit" => {
|
"save_and_quit" => {
|
||||||
let message = save(
|
let message = if app_state.ui.show_login {
|
||||||
form_state,
|
login_save(auth_state, auth_client, app_state).await?
|
||||||
grpc_client,
|
} else {
|
||||||
&mut app_state.ui.is_saved,
|
form_save(
|
||||||
current_position,
|
form_state,
|
||||||
total_count,
|
grpc_client,
|
||||||
).await?;
|
&mut app_state.ui.is_saved,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?
|
||||||
|
};
|
||||||
terminal.cleanup()?;
|
terminal.cleanup()?;
|
||||||
Ok((true, format!("{}. Exiting application.", message)))
|
Ok((true, format!("{}. Exiting application.", message)))
|
||||||
},
|
},
|
||||||
"revert" => {
|
"revert" => {
|
||||||
let message = revert(
|
if app_state.ui.show_login {
|
||||||
form_state,
|
let message = cancel(auth_state, app_state).await;
|
||||||
grpc_client,
|
Ok((false, message))
|
||||||
current_position,
|
} else {
|
||||||
total_count,
|
let message = revert(
|
||||||
).await?;
|
form_state,
|
||||||
Ok((false, message))
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
).await?;
|
||||||
|
Ok((false, message))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ => Ok((false, format!("Core action not handled: {}", action))),
|
_ => Ok((false, format!("Core action not handled: {}", action))),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use crate::tui::terminal::{
|
|||||||
core::TerminalCore,
|
core::TerminalCore,
|
||||||
};
|
};
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
use crate::tui::functions::common::commands::CommandHandler;
|
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;
|
||||||
@@ -27,11 +28,12 @@ pub struct EventHandler {
|
|||||||
pub ideal_cursor_column: usize,
|
pub ideal_cursor_column: usize,
|
||||||
pub key_sequence_tracker: KeySequenceTracker,
|
pub key_sequence_tracker: KeySequenceTracker,
|
||||||
pub auth_state: AuthState,
|
pub auth_state: AuthState,
|
||||||
|
pub auth_client: AuthClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventHandler {
|
impl EventHandler {
|
||||||
pub fn new() -> Self {
|
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
EventHandler {
|
Ok(EventHandler {
|
||||||
command_mode: false,
|
command_mode: false,
|
||||||
command_input: String::new(),
|
command_input: String::new(),
|
||||||
command_message: String::new(),
|
command_message: String::new(),
|
||||||
@@ -40,7 +42,8 @@ impl EventHandler {
|
|||||||
ideal_cursor_column: 0,
|
ideal_cursor_column: 0,
|
||||||
key_sequence_tracker: KeySequenceTracker::new(800),
|
key_sequence_tracker: KeySequenceTracker::new(800),
|
||||||
auth_state: AuthState::new(),
|
auth_state: AuthState::new(),
|
||||||
}
|
auth_client: AuthClient::new().await?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_event(
|
pub async fn handle_event(
|
||||||
@@ -131,7 +134,9 @@ impl EventHandler {
|
|||||||
return common::handle_core_action(
|
return common::handle_core_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
form_state,
|
||||||
|
&mut self.auth_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
|
&mut self.auth_client,
|
||||||
terminal,
|
terminal,
|
||||||
app_state,
|
app_state,
|
||||||
current_position,
|
current_position,
|
||||||
@@ -191,7 +196,9 @@ impl EventHandler {
|
|||||||
return common::handle_core_action(
|
return common::handle_core_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
form_state,
|
||||||
|
&mut self.auth_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
|
&mut self.auth_client,
|
||||||
terminal,
|
terminal,
|
||||||
app_state,
|
app_state,
|
||||||
current_position,
|
current_position,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// src/tui/functions/common.rs
|
// src/tui/functions/common.rs
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod form;
|
pub mod form;
|
||||||
|
pub mod login;
|
||||||
|
|
||||||
pub use commands::*;
|
pub use commands::*;
|
||||||
pub use form::*;
|
pub use form::{revert, save as form_save};
|
||||||
|
pub use login::{cancel, save as login_save};
|
||||||
|
|||||||
45
client/src/tui/functions/common/login.rs
Normal file
45
client/src/tui/functions/common/login.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// src/tui/functions/common/login.rs
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
|
use crate::state::pages::auth::AuthState;
|
||||||
|
use crate::state::state::AppState;
|
||||||
|
|
||||||
|
pub async fn save(
|
||||||
|
auth_state: &mut AuthState,
|
||||||
|
auth_client: &mut AuthClient,
|
||||||
|
app_state: &mut AppState,
|
||||||
|
) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
let identifier = auth_state.username.clone();
|
||||||
|
let password = auth_state.password.clone();
|
||||||
|
|
||||||
|
match auth_client.login(identifier, password).await {
|
||||||
|
Ok(response) => {
|
||||||
|
auth_state.auth_token = Some(response.access_token);
|
||||||
|
auth_state.user_id = Some(response.user_id);
|
||||||
|
auth_state.role = Some(response.role);
|
||||||
|
auth_state.error_message = None;
|
||||||
|
|
||||||
|
// Update app state to show main interface
|
||||||
|
app_state.ui.show_login = false;
|
||||||
|
app_state.ui.show_form = true;
|
||||||
|
|
||||||
|
Ok("Login successful!".to_string())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let error_message = format!("Login failed: {}", e);
|
||||||
|
auth_state.error_message = Some(error_message.clone());
|
||||||
|
Ok(error_message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cancel(
|
||||||
|
auth_state: &mut AuthState,
|
||||||
|
app_state: &mut AppState,
|
||||||
|
) -> String {
|
||||||
|
auth_state.username.clear();
|
||||||
|
auth_state.password.clear();
|
||||||
|
auth_state.error_message = None;
|
||||||
|
app_state.ui.show_login = false;
|
||||||
|
app_state.ui.show_intro = true;
|
||||||
|
"Login canceled".to_string()
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use crate::tui::terminal::TerminalCore;
|
use crate::tui::terminal::TerminalCore;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::auth::AuthClient;
|
||||||
use crate::services::ui_service::UiService; // Add this import
|
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::tui::functions::common::CommandHandler;
|
||||||
@@ -17,6 +18,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let config = Config::load()?;
|
let config = Config::load()?;
|
||||||
let mut terminal = TerminalCore::new()?;
|
let mut terminal = TerminalCore::new()?;
|
||||||
let mut grpc_client = GrpcClient::new().await?;
|
let mut grpc_client = GrpcClient::new().await?;
|
||||||
|
let mut auth_client = AuthClient::new().await?;
|
||||||
let mut command_handler = CommandHandler::new();
|
let mut command_handler = CommandHandler::new();
|
||||||
let theme = Theme::from_str(&config.colors.theme);
|
let theme = Theme::from_str(&config.colors.theme);
|
||||||
let mut auth_state = AuthState::default();
|
let mut auth_state = AuthState::default();
|
||||||
@@ -31,7 +33,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut form_state = FormState::new(column_names);
|
let mut form_state = FormState::new(column_names);
|
||||||
|
|
||||||
// The rest of your UI initialization remains the same
|
// The rest of your UI initialization remains the same
|
||||||
let mut event_handler = EventHandler::new();
|
let mut event_handler = EventHandler::new().await?;
|
||||||
let event_reader = EventReader::new();
|
let event_reader = EventReader::new();
|
||||||
|
|
||||||
// Fetch the total count of Adresar entries
|
// Fetch the total count of Adresar entries
|
||||||
|
|||||||
Reference in New Issue
Block a user