register is now separated also
This commit is contained in:
184
client/src/pages/register/state.rs
Normal file
184
client/src/pages/register/state.rs
Normal file
@@ -0,0 +1,184 @@
|
||||
// src/pages/register/state.rs
|
||||
|
||||
use canvas::{DataProvider, AppMode};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref AVAILABLE_ROLES: Vec<String> = vec![
|
||||
"admin".to_string(),
|
||||
"moderator".to_string(),
|
||||
"accountant".to_string(),
|
||||
"viewer".to_string(),
|
||||
];
|
||||
}
|
||||
|
||||
/// Represents the state of the Registration form UI
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RegisterState {
|
||||
pub username: String,
|
||||
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,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub app_mode: AppMode,
|
||||
pub role_suggestions: Vec<String>,
|
||||
pub role_suggestions_active: bool,
|
||||
}
|
||||
|
||||
impl Default for RegisterState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
email: String::new(),
|
||||
password: String::new(),
|
||||
password_confirmation: String::new(),
|
||||
role: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
app_mode: AppMode::Edit,
|
||||
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||
role_suggestions_active: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RegisterState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
app_mode: AppMode::Edit,
|
||||
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||
role_suggestions_active: false,
|
||||
..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 < 5 {
|
||||
self.current_field = index;
|
||||
|
||||
if index == 4 {
|
||||
self.activate_role_suggestions();
|
||||
} else {
|
||||
self.deactivate_role_suggestions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
3 => &mut self.password_confirmation,
|
||||
4 => &mut self.role,
|
||||
_ => panic!("Invalid current_field index in RegisterState"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_mode(&self) -> AppMode {
|
||||
self.app_mode
|
||||
}
|
||||
|
||||
pub fn activate_role_suggestions(&mut self) {
|
||||
self.role_suggestions_active = true;
|
||||
let current_input = self.role.to_lowercase();
|
||||
self.role_suggestions = AVAILABLE_ROLES
|
||||
.iter()
|
||||
.filter(|role| role.to_lowercase().contains(¤t_input))
|
||||
.cloned()
|
||||
.collect();
|
||||
}
|
||||
|
||||
pub fn deactivate_role_suggestions(&mut self) {
|
||||
self.role_suggestions_active = false;
|
||||
}
|
||||
|
||||
pub fn is_role_suggestions_active(&self) -> bool {
|
||||
self.role_suggestions_active
|
||||
}
|
||||
|
||||
pub fn get_role_suggestions(&self) -> &[String] {
|
||||
&self.role_suggestions
|
||||
}
|
||||
|
||||
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 DataProvider for RegisterState {
|
||||
fn field_count(&self) -> usize {
|
||||
5
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Username",
|
||||
1 => "Email (Optional)",
|
||||
2 => "Password (Optional)",
|
||||
3 => "Confirm Password",
|
||||
4 => "Role (Optional)",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.username,
|
||||
1 => &self.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.username = value,
|
||||
1 => self.email = value,
|
||||
2 => self.password = value,
|
||||
3 => self.password_confirmation = value,
|
||||
4 => self.role = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||
field_index == 4 // only Role field supports suggestions
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user