register form now has optional field role

This commit is contained in:
filipriec
2025-04-11 13:51:05 +02:00
parent 0fd2a589eb
commit cf1aa4fd2a
4 changed files with 26 additions and 7 deletions

View File

@@ -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,

View File

@@ -28,14 +28,16 @@ impl AuthClient {
&mut self,
username: String,
email: String,
password: Option<String>, // Use Option for optional fields
password_confirmation: Option<String>, // Use Option for optional fields
password: Option<String>,
password_confirmation: Option<String>,
role: Option<String>,
) -> Result<AuthResponse, Box<dyn std::error::Error>> {
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)

View File

@@ -21,6 +21,7 @@ pub struct RegisterState {
pub email: String,
pub password: String,
pub password_confirmation: String,
pub role: String,
pub error_message: Option<String>,
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);

View File

@@ -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