From cf1aa4fd2a0d73cf91432f0f0ddfe00831fe710b Mon Sep 17 00:00:00 2001 From: filipriec Date: Fri, 11 Apr 2025 13:51:05 +0200 Subject: [PATCH] register form now has optional field role --- client/src/components/auth/register.rs | 4 +++- client/src/services/auth.rs | 10 ++++++---- client/src/state/pages/auth.rs | 11 ++++++++++- client/src/tui/functions/common/register.rs | 8 +++++++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/client/src/components/auth/register.rs b/client/src/components/auth/register.rs index 6cd19f5..f0ac7f6 100644 --- a/client/src/components/auth/register.rs +++ b/client/src/components/auth/register.rs @@ -39,7 +39,7 @@ pub fn render_register( let chunks = Layout::default() .direction(Direction::Vertical) .constraints([ - Constraint::Length(6), // Form (4 fields + padding) + Constraint::Length(7), // Form (5 fields + padding) Constraint::Length(1), // Error message Constraint::Length(3), // Buttons ]) @@ -55,6 +55,7 @@ pub fn render_register( "Email (Optional)", "Password (Optional)", "Confirm Password", + "Role (Optional)", ], &state.current_field, &[ // Update values from RegisterState @@ -62,6 +63,7 @@ pub fn render_register( &state.email, &state.password, &state.password_confirmation, + &state.role, ], theme, is_edit_mode, diff --git a/client/src/services/auth.rs b/client/src/services/auth.rs index fdcb7b3..a37f531 100644 --- a/client/src/services/auth.rs +++ b/client/src/services/auth.rs @@ -28,14 +28,16 @@ impl AuthClient { &mut self, username: String, email: String, - password: Option, // Use Option for optional fields - password_confirmation: Option, // Use Option for optional fields + password: Option, + password_confirmation: Option, + role: Option, ) -> Result> { let request = tonic::Request::new(RegisterRequest { username, email, - password: password.unwrap_or_default(), // Send empty string if None - password_confirmation: password_confirmation.unwrap_or_default(), // Send empty string if None + password: password.unwrap_or_default(), + password_confirmation: password_confirmation.unwrap_or_default(), + role: role.unwrap_or_default(), }); let response = self.client.register(request).await?.into_inner(); Ok(response) diff --git a/client/src/state/pages/auth.rs b/client/src/state/pages/auth.rs index 8ea976c..3c32cd5 100644 --- a/client/src/state/pages/auth.rs +++ b/client/src/state/pages/auth.rs @@ -21,6 +21,7 @@ pub struct RegisterState { pub email: String, pub password: String, pub password_confirmation: String, + pub role: String, pub error_message: Option, pub current_field: usize, pub current_cursor_pos: usize, @@ -51,6 +52,7 @@ impl RegisterState { email: String::new(), password: String::new(), password_confirmation: String::new(), + role: String::new(), error_message: None, current_field: 0, current_cursor_pos: 0, @@ -141,6 +143,7 @@ impl CanvasState for RegisterState { 1 => self.email.len(), 2 => self.password.len(), 3 => self.password_confirmation.len(), + 4 => self.role.len(), _ => 0, }; self.current_cursor_pos.min(len) @@ -156,6 +159,7 @@ impl CanvasState for RegisterState { &self.email, &self.password, &self.password_confirmation, + &self.role, ] } @@ -165,6 +169,7 @@ impl CanvasState for RegisterState { 1 => &self.email, 2 => &self.password, 3 => &self.password_confirmation, + 4 => &self.role, _ => "", } } @@ -175,6 +180,7 @@ impl CanvasState for RegisterState { 1 => &mut self.email, 2 => &mut self.password, 3 => &mut self.password_confirmation, + 4 => &mut self.role, _ => panic!("Invalid current_field index in RegisterState"), } } @@ -185,17 +191,19 @@ impl CanvasState for RegisterState { "Email (Optional)", "Password (Optional)", "Confirm Password", + "Role (Oprional)" ] } fn set_current_field(&mut self, index: usize) { - if index < 4 { // RegisterState has 4 fields + if index < 5 { // RegisterState has 5 fields self.current_field = index; let len = match self.current_field { 0 => self.username.len(), 1 => self.email.len(), 2 => self.password.len(), 3 => self.password_confirmation.len(), + 4 => self.role.len(), _ => 0, }; self.current_cursor_pos = self.current_cursor_pos.min(len); @@ -208,6 +216,7 @@ impl CanvasState for RegisterState { 1 => self.email.len(), 2 => self.password.len(), 3 => self.password_confirmation.len(), + 4 => self.role.len(), _ => 0, }; self.current_cursor_pos = pos.min(len); diff --git a/client/src/tui/functions/common/register.rs b/client/src/tui/functions/common/register.rs index 0700516..07f2f45 100644 --- a/client/src/tui/functions/common/register.rs +++ b/client/src/tui/functions/common/register.rs @@ -30,6 +30,11 @@ pub async fn save( } else { Some(register_state.password_confirmation.clone()) }; + let role = if register_state.role.is_empty() { + None + } else { + Some(register_state.role.clone()) + }; // Basic client-side validation (example) if username.is_empty() { @@ -59,7 +64,7 @@ pub async fn save( app_state.hide_dialog(); // Call the gRPC register method - match auth_client.register(username, email, password, password_confirmation).await { + match auth_client.register(username, email, password, password_confirmation, role).await { Ok(response) => { // Clear fields on success? Optional, maybe wait for dialog confirmation. // register_state.username.clear(); @@ -117,6 +122,7 @@ pub async fn revert( register_state.email.clear(); register_state.password.clear(); register_state.password_confirmation.clear(); + register_state.role.clear(); register_state.error_message = None; register_state.set_has_unsaved_changes(false); register_state.current_field = 0; // Reset focus to first field