From 597bdde7e1b592b3681b8c2ead3e2594c75be59f Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 23 Aug 2025 20:58:12 +0200 Subject: [PATCH] login moved to the pages --- client/src/modes/handlers/event.rs | 3 +- client/src/pages/login/logic.rs | 2 +- client/src/pages/login/mod.rs | 2 +- client/src/pages/login/state.rs | 93 +++++++++++++++++++- client/src/pages/login/ui.rs | 2 +- client/src/pages/routing/router.rs | 3 +- client/src/state/pages/auth.rs | 133 +---------------------------- client/src/ui/handlers/ui.rs | 2 +- 8 files changed, 103 insertions(+), 137 deletions(-) diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 49450b1..80c16b5 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -25,10 +25,11 @@ use crate::state::{ }, pages::{ admin::AdminState, - auth::{AuthState, LoginState, RegisterState}, + auth::{AuthState, RegisterState}, intro::IntroState, }, }; +use crate::pages::login::LoginState; use crate::tui::common::register; use crate::pages::login; use crate::pages::login::logic; diff --git a/client/src/pages/login/logic.rs b/client/src/pages/login/logic.rs index d956903..6fa354a 100644 --- a/client/src/pages/login/logic.rs +++ b/client/src/pages/login/logic.rs @@ -2,12 +2,12 @@ use crate::services::auth::AuthClient; use crate::state::pages::auth::AuthState; -use crate::state::pages::auth::LoginState; use crate::state::app::state::AppState; use crate::buffer::state::{AppView, BufferState}; use crate::config::storage::storage::{StoredAuthData, save_auth_data}; use crate::ui::handlers::context::DialogPurpose; use common::proto::komp_ac::auth::LoginResponse; +use crate::pages::login::LoginState; use anyhow::{Context, Result}; use tokio::spawn; use tokio::sync::mpsc; diff --git a/client/src/pages/login/mod.rs b/client/src/pages/login/mod.rs index c5a03ac..162e08b 100644 --- a/client/src/pages/login/mod.rs +++ b/client/src/pages/login/mod.rs @@ -4,6 +4,6 @@ pub mod state; pub mod ui; pub mod logic; -pub use state::LoginState; +pub use state::*; pub use ui::render_login; pub use logic::*; diff --git a/client/src/pages/login/state.rs b/client/src/pages/login/state.rs index 83395a9..ce69fd2 100644 --- a/client/src/pages/login/state.rs +++ b/client/src/pages/login/state.rs @@ -1,6 +1,6 @@ // src/pages/login/state.rs -use canvas::AppMode; +use canvas::{AppMode, DataProvider}; #[derive(Debug, Clone)] pub struct LoginState { @@ -28,3 +28,94 @@ impl Default for LoginState { } } } + +impl LoginState { + pub fn new() -> Self { + Self { + app_mode: AppMode::Edit, + ..Default::default() + } + } + + pub fn current_field(&self) -> usize { + self.current_field + } + + pub fn current_cursor_pos(&self) -> usize { + self.current_cursor_pos + } + + pub fn set_current_field(&mut self, index: usize) { + if index < 2 { + self.current_field = index; + } + } + + pub fn set_current_cursor_pos(&mut self, pos: usize) { + self.current_cursor_pos = pos; + } + + pub fn get_current_input(&self) -> &str { + match self.current_field { + 0 => &self.username, + 1 => &self.password, + _ => "", + } + } + + pub 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 LoginState"), + } + } + + pub fn current_mode(&self) -> AppMode { + self.app_mode + } + + pub fn has_unsaved_changes(&self) -> bool { + self.has_unsaved_changes + } + + pub fn set_has_unsaved_changes(&mut self, changed: bool) { + self.has_unsaved_changes = changed; + } +} + +// Implement DataProvider for LoginState +impl DataProvider for LoginState { + fn field_count(&self) -> usize { + 2 + } + + fn field_name(&self, index: usize) -> &str { + match index { + 0 => "Username/Email", + 1 => "Password", + _ => "", + } + } + + fn field_value(&self, index: usize) -> &str { + match index { + 0 => &self.username, + 1 => &self.password, + _ => "", + } + } + + fn set_field_value(&mut self, index: usize, value: String) { + match index { + 0 => self.username = value, + 1 => self.password = value, + _ => {} + } + self.has_unsaved_changes = true; + } + + fn supports_suggestions(&self, _field_index: usize) -> bool { + false // Login form doesn't support suggestions + } +} diff --git a/client/src/pages/login/ui.rs b/client/src/pages/login/ui.rs index 9d278fb..2eab56c 100644 --- a/client/src/pages/login/ui.rs +++ b/client/src/pages/login/ui.rs @@ -2,7 +2,6 @@ use crate::{ config::colors::themes::Theme, - state::pages::auth::LoginState, state::app::state::AppState, }; use ratatui::{ @@ -17,6 +16,7 @@ use canvas::{ render_suggestions_dropdown, DefaultCanvasTheme, }; +use crate::pages::login::LoginState; use crate::dialog; pub fn render_login( diff --git a/client/src/pages/routing/router.rs b/client/src/pages/routing/router.rs index 9ae710c..2ee959a 100644 --- a/client/src/pages/routing/router.rs +++ b/client/src/pages/routing/router.rs @@ -1,12 +1,13 @@ // src/pages/routing/router.rs use crate::state::pages::{ admin::AdminState, - auth::{AuthState, LoginState, RegisterState}, + auth::{AuthState, RegisterState}, intro::IntroState, add_logic::AddLogicState, add_table::AddTableState, }; use crate::pages::forms::FormState; +use crate::pages::login::LoginState; #[derive(Debug)] pub enum Page { diff --git a/client/src/state/pages/auth.rs b/client/src/state/pages/auth.rs index ab0c1f9..d1871b9 100644 --- a/client/src/state/pages/auth.rs +++ b/client/src/state/pages/auth.rs @@ -1,4 +1,5 @@ // src/state/pages/auth.rs + use canvas::{DataProvider, AppMode}; use lazy_static::lazy_static; @@ -20,34 +21,6 @@ pub struct AuthState { pub decoded_username: Option, } -/// Represents the state of the Login form UI -#[derive(Debug, Clone)] -pub struct LoginState { - pub username: String, - pub password: String, - pub error_message: Option, - pub current_field: usize, - pub current_cursor_pos: usize, - pub has_unsaved_changes: bool, - pub login_request_pending: bool, - pub app_mode: AppMode, -} - -impl Default for LoginState { - fn default() -> Self { - Self { - username: String::new(), - password: String::new(), - error_message: None, - current_field: 0, - current_cursor_pos: 0, - has_unsaved_changes: false, - login_request_pending: false, - app_mode: AppMode::Edit, - } - } -} - /// Represents the state of the Registration form UI #[derive(Debug, Clone)] pub struct RegisterState { @@ -61,7 +34,6 @@ pub struct RegisterState { pub current_cursor_pos: usize, pub has_unsaved_changes: bool, pub app_mode: AppMode, - // Keep role suggestions for later integration pub role_suggestions: Vec, pub role_suggestions_active: bool, } @@ -91,63 +63,6 @@ impl AuthState { } } -impl LoginState { - pub fn new() -> Self { - Self { - app_mode: AppMode::Edit, - ..Default::default() - } - } - - // Legacy method compatibility - pub fn current_field(&self) -> usize { - self.current_field - } - - pub fn current_cursor_pos(&self) -> usize { - self.current_cursor_pos - } - - pub fn set_current_field(&mut self, index: usize) { - if index < 2 { - self.current_field = index; - } - } - - pub fn set_current_cursor_pos(&mut self, pos: usize) { - self.current_cursor_pos = pos; - } - - pub fn get_current_input(&self) -> &str { - match self.current_field { - 0 => &self.username, - 1 => &self.password, - _ => "", - } - } - - pub 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 LoginState"), - } - } - - pub fn current_mode(&self) -> AppMode { - self.app_mode - } - - // Add missing methods that used to come from CanvasState trait - pub fn has_unsaved_changes(&self) -> bool { - self.has_unsaved_changes - } - - pub fn set_has_unsaved_changes(&mut self, changed: bool) { - self.has_unsaved_changes = changed; - } -} - impl RegisterState { pub fn new() -> Self { Self { @@ -158,7 +73,6 @@ impl RegisterState { } } - // Legacy method compatibility pub fn current_field(&self) -> usize { self.current_field } @@ -171,7 +85,6 @@ impl RegisterState { if index < 5 { self.current_field = index; - // Auto-activate role suggestions when moving to role field (index 4) if index == 4 { self.activate_role_suggestions(); } else { @@ -195,8 +108,8 @@ impl RegisterState { } } - pub fn get_current_input_mut(&mut self) -> &mut String { - match self.current_field { + pub fn get_current_input_mut(&mut self, index: usize) -> &mut String { + match index { 0 => &mut self.username, 1 => &mut self.email, 2 => &mut self.password, @@ -210,10 +123,8 @@ impl RegisterState { self.app_mode } - // Role suggestions management pub fn activate_role_suggestions(&mut self) { self.role_suggestions_active = true; - // Filter suggestions based on current input let current_input = self.role.to_lowercase(); self.role_suggestions = AVAILABLE_ROLES .iter() @@ -234,7 +145,6 @@ impl RegisterState { &self.role_suggestions } - // Add missing methods that used to come from CanvasState trait pub fn has_unsaved_changes(&self) -> bool { self.has_unsaved_changes } @@ -244,43 +154,6 @@ impl RegisterState { } } -// Step 2: Implement DataProvider for LoginState -impl DataProvider for LoginState { - fn field_count(&self) -> usize { - 2 - } - - fn field_name(&self, index: usize) -> &str { - match index { - 0 => "Username/Email", - 1 => "Password", - _ => "", - } - } - - fn field_value(&self, index: usize) -> &str { - match index { - 0 => &self.username, - 1 => &self.password, - _ => "", - } - } - - fn set_field_value(&mut self, index: usize, value: String) { - match index { - 0 => self.username = value, - 1 => self.password = value, - _ => {} - } - self.has_unsaved_changes = true; - } - - fn supports_suggestions(&self, _field_index: usize) -> bool { - false // Login form doesn't support suggestions - } -} - -// Step 3: Implement DataProvider for RegisterState impl DataProvider for RegisterState { fn field_count(&self) -> usize { 5 diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index 1f76c6e..f0c98c5 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -9,7 +9,6 @@ use crate::modes::common::commands::CommandHandler; use crate::modes::handlers::event::{EventHandler, EventOutcome}; use crate::modes::handlers::mode_manager::{AppMode, ModeManager}; use crate::state::pages::auth::AuthState; -use crate::state::pages::auth::LoginState; use crate::state::pages::auth::RegisterState; use crate::state::pages::admin::AdminState; use crate::state::pages::admin::AdminFocus; @@ -23,6 +22,7 @@ use crate::tui::terminal::{EventReader, TerminalCore}; use crate::ui::handlers::render::render_ui; use crate::pages::login; use crate::pages::login::LoginResult; +use crate::pages::login::LoginState; use crate::tui::functions::common::register::RegisterResult; use crate::ui::handlers::context::DialogPurpose; use crate::tui::functions::common::register;