From 1dd5f685a678564346636775c88f2f39e63f4d64 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 13 Apr 2025 17:06:59 +0200 Subject: [PATCH] authstate splitting into login state and auth state, quite before maddness --- client/src/state/pages/auth.rs | 76 +++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/client/src/state/pages/auth.rs b/client/src/state/pages/auth.rs index fa6714b..f84a759 100644 --- a/client/src/state/pages/auth.rs +++ b/client/src/state/pages/auth.rs @@ -11,22 +11,28 @@ lazy_static! { ]; } +/// Represents the authenticated session state #[derive(Default)] pub struct AuthState { - pub return_selected: bool, - pub username: String, - pub password: String, - pub error_message: Option, - pub current_field: usize, - pub current_cursor_pos: usize, pub auth_token: Option, pub user_id: Option, pub role: Option, pub decoded_username: Option, +} + +/// Represents the state of the Login form UI +#[derive(Default)] +pub struct LoginState { + pub username: String, // Input field for username/email + pub password: String, // Input field for password + pub error_message: Option, // Error message specific to login attempt + pub current_field: usize, // 0 for username, 1 for password + pub current_cursor_pos: usize, // Cursor position within current field pub has_unsaved_changes: bool, } -#[derive(Default, Clone)] // Add Clone derive +/// Represents the state of the Registration form UI +#[derive(Default, Clone)] pub struct RegisterState { pub username: String, pub email: String, @@ -44,24 +50,33 @@ pub struct RegisterState { } impl AuthState { + /// Creates a new empty AuthState (unauthenticated) 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, - has_unsaved_changes: false, decoded_username: None, } } } +impl LoginState { + /// Creates a new empty LoginState + pub fn new() -> Self { + Self { + username: String::new(), + password: String::new(), + error_message: None, + current_field: 0, + current_cursor_pos: 0, + has_unsaved_changes: false, + } + } +} + impl RegisterState { + /// Creates a new empty RegisterState pub fn new() -> Self { Self { username: String::new(), @@ -80,7 +95,7 @@ impl RegisterState { } } - /// Updates role suggestions based on current input. + /// Updates role suggestions based on current input pub fn update_role_suggestions(&mut self) { let current_input = self.role.to_lowercase(); self.role_suggestions = AVAILABLE_ROLES @@ -92,7 +107,7 @@ impl RegisterState { } } -impl CanvasState for AuthState { +impl CanvasState for LoginState { fn current_field(&self) -> usize { self.current_field } @@ -126,7 +141,7 @@ impl CanvasState for AuthState { match self.current_field { 0 => &mut self.username, 1 => &mut self.password, - _ => panic!("Invalid current_field index in AuthState"), + _ => panic!("Invalid current_field index in LoginState"), } } @@ -135,13 +150,12 @@ impl CanvasState for AuthState { } fn set_current_field(&mut self, index: usize) { - if index < 2 { // AuthState only has 2 fields + if index < 2 { self.current_field = index; - // IMPORTANT: Clamp cursor position to the length of the NEW field let len = match self.current_field { - 0 => self.username.len(), - 1 => self.password.len(), - _ => 0, + 0 => self.username.len(), + 1 => self.password.len(), + _ => 0, }; self.current_cursor_pos = self.current_cursor_pos.min(len); } @@ -149,26 +163,23 @@ impl CanvasState for AuthState { fn set_current_cursor_pos(&mut self, pos: usize) { let len = match self.current_field { - 0 => self.username.len(), - 1 => self.password.len(), - _ => 0, + 0 => self.username.len(), + 1 => self.password.len(), + _ => 0, }; - // Ensure stored position is always valid self.current_cursor_pos = pos.min(len); } fn set_has_unsaved_changes(&mut self, changed: bool) { - // Allow the generic handler to signal changes self.has_unsaved_changes = changed; } - // --- Autocomplete Support (Not Used for AuthState) --- fn get_suggestions(&self) -> Option<&[String]> { - None // AuthState doesn't provide suggestions + None } fn get_selected_suggestion_index(&self) -> Option { - None // AuthState doesn't have selected suggestions + None } } @@ -231,12 +242,12 @@ impl CanvasState for RegisterState { "Email (Optional)", "Password (Optional)", "Confirm Password", - "Role (Oprional)" + "Role (Optional)" ] } fn set_current_field(&mut self, index: usize) { - if index < 5 { // RegisterState has 5 fields + if index < 5 { self.current_field = index; let len = match self.current_field { 0 => self.username.len(), @@ -267,7 +278,6 @@ impl CanvasState for RegisterState { } fn get_suggestions(&self) -> Option<&[String]> { - // Only show suggestions for the role field (index 4) when requested if self.current_field == 4 && self.in_suggestion_mode && self.show_role_suggestions { Some(&self.role_suggestions) } else {