From d577ff6715e152c58a29811feaa100e2306bb9f4 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 31 Mar 2025 23:56:58 +0200 Subject: [PATCH] edit mode is like a readonly mode --- client/src/modes/canvas/common.rs | 8 +++--- client/src/tui/functions/common.rs | 4 +-- client/src/tui/functions/common/login.rs | 35 ++++++++++++++++++------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/client/src/modes/canvas/common.rs b/client/src/modes/canvas/common.rs index bd823c6..985bbfd 100644 --- a/client/src/modes/canvas/common.rs +++ b/client/src/modes/canvas/common.rs @@ -6,8 +6,8 @@ use crate::state::state::AppState; use crate::services::grpc_client::GrpcClient; use crate::services::auth::AuthClient; use crate::tui::functions::common::{ - form::{save as form_save, revert}, - login::{save as login_save, cancel} + form::{save as form_save, revert as form_revert}, + login::{save as login_save, revert as login_revert} }; pub async fn handle_core_action( @@ -58,10 +58,10 @@ pub async fn handle_core_action( }, "revert" => { if app_state.ui.show_login { - let message = cancel(auth_state, app_state).await; + let message = login_revert(auth_state, app_state).await; Ok((false, message)) } else { - let message = revert( + let message = form_revert( form_state, grpc_client, current_position, diff --git a/client/src/tui/functions/common.rs b/client/src/tui/functions/common.rs index 8982b5d..b4b457e 100644 --- a/client/src/tui/functions/common.rs +++ b/client/src/tui/functions/common.rs @@ -4,5 +4,5 @@ pub mod form; pub mod login; pub use commands::*; -pub use form::{revert, save as form_save}; -pub use login::{cancel, save as login_save}; +pub use form::{revert as form_revert, save as form_save}; +pub use login::{revert as login_revert, save as login_save}; diff --git a/client/src/tui/functions/common/login.rs b/client/src/tui/functions/common/login.rs index 0fd41dc..cc99d57 100644 --- a/client/src/tui/functions/common/login.rs +++ b/client/src/tui/functions/common/login.rs @@ -2,7 +2,10 @@ use crate::services::auth::AuthClient; use crate::state::pages::auth::AuthState; use crate::state::state::AppState; +use crate::state::canvas_state::CanvasState; +/// Attempts to log the user in using the provided credentials via gRPC. +/// Updates AuthState and AppState on success or failure. pub async fn save( auth_state: &mut AuthState, auth_client: &mut AuthClient, @@ -11,35 +14,49 @@ pub async fn save( let identifier = auth_state.username.clone(); let password = auth_state.password.clone(); + // Call the gRPC login method match auth_client.login(identifier, password).await { Ok(response) => { + // Store authentication details on success 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 + auth_state.set_has_unsaved_changes(false); // Mark as "saved" + + // Update app state to transition from login to the main form view app_state.ui.show_login = false; - app_state.ui.show_form = true; - + app_state.ui.show_form = true; // Assuming form is the next view + Ok("Login successful!".to_string()) } Err(e) => { + // Store error message on failure let error_message = format!("Login failed: {}", e); auth_state.error_message = Some(error_message.clone()); - Ok(error_message) + // Keep unsaved changes true if login fails, allowing retry/revert + auth_state.set_has_unsaved_changes(true); + Ok(error_message) // Return error message to display } } } -pub async fn cancel( +/// Reverts the login form fields to empty and returns to the previous screen (Intro). +/// This function is now named 'revert' to match the 'form' counterpart. +pub async fn revert( auth_state: &mut AuthState, app_state: &mut AppState, ) -> String { + // Clear the input fields auth_state.username.clear(); auth_state.password.clear(); - auth_state.error_message = None; + auth_state.error_message = None; // Clear any previous error + auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes + + // Update app state to hide login and show the previous screen (Intro) app_state.ui.show_login = false; - app_state.ui.show_intro = true; - "Login canceled".to_string() + app_state.ui.show_intro = true; // Assuming Intro is the screen before login + + "Login reverted".to_string() } +