registration now has working form

This commit is contained in:
filipriec
2025-08-29 12:22:25 +02:00
parent cf79bc7bd5
commit 72c2691a17
7 changed files with 241 additions and 65 deletions

View File

@@ -1,6 +1,7 @@
// src/pages/register/state.rs
use canvas::{DataProvider, AppMode};
use canvas::{DataProvider, AppMode, FormEditor};
use std::fmt;
use lazy_static::lazy_static;
lazy_static! {
@@ -182,3 +183,128 @@ impl DataProvider for RegisterState {
field_index == 4 // only Role field supports suggestions
}
}
/// Wrapper that owns both the raw register state and its editor
pub struct RegisterFormState {
pub state: RegisterState,
pub editor: FormEditor<RegisterState>,
}
impl Default for RegisterFormState {
fn default() -> Self {
Self::new()
}
}
// manual Debug because FormEditor doesnt implement Debug
impl fmt::Debug for RegisterFormState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RegisterFormState")
.field("state", &self.state)
.finish()
}
}
impl RegisterFormState {
pub fn new() -> Self {
let state = RegisterState::default();
let editor = FormEditor::new(state.clone());
Self { state, editor }
}
// === Delegates to RegisterState ===
pub fn username(&self) -> &str {
&self.state.username
}
pub fn username_mut(&mut self) -> &mut String {
&mut self.state.username
}
pub fn email(&self) -> &str {
&self.state.email
}
pub fn email_mut(&mut self) -> &mut String {
&mut self.state.email
}
pub fn password(&self) -> &str {
&self.state.password
}
pub fn password_mut(&mut self) -> &mut String {
&mut self.state.password
}
pub fn password_confirmation(&self) -> &str {
&self.state.password_confirmation
}
pub fn password_confirmation_mut(&mut self) -> &mut String {
&mut self.state.password_confirmation
}
pub fn role(&self) -> &str {
&self.state.role
}
pub fn role_mut(&mut self) -> &mut String {
&mut self.state.role
}
pub fn error_message(&self) -> Option<&String> {
self.state.error_message.as_ref()
}
pub fn set_error_message(&mut self, msg: Option<String>) {
self.state.error_message = msg;
}
pub fn has_unsaved_changes(&self) -> bool {
self.state.has_unsaved_changes
}
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
self.state.has_unsaved_changes = changed;
}
pub fn clear(&mut self) {
self.state.username.clear();
self.state.email.clear();
self.state.password.clear();
self.state.password_confirmation.clear();
self.state.role.clear();
self.state.error_message = None;
self.state.has_unsaved_changes = false;
self.state.current_field = 0;
self.state.current_cursor_pos = 0;
}
// === Delegates to cursor/input ===
pub fn current_field(&self) -> usize {
self.state.current_field()
}
pub fn set_current_field(&mut self, index: usize) {
self.state.set_current_field(index);
}
pub fn current_cursor_pos(&self) -> usize {
self.state.current_cursor_pos()
}
pub fn set_current_cursor_pos(&mut self, pos: usize) {
self.state.set_current_cursor_pos(pos);
}
pub fn get_current_input(&self) -> &str {
self.state.get_current_input()
}
pub fn get_current_input_mut(&mut self) -> &mut String {
self.state.get_current_input_mut(self.state.current_field)
}
// === Delegates to FormEditor ===
pub fn mode(&self) -> canvas::AppMode {
self.editor.mode()
}
pub fn cursor_position(&self) -> usize {
self.editor.cursor_position()
}
pub fn handle_key_event(
&mut self,
key_event: crossterm::event::KeyEvent,
) -> canvas::keymap::KeyEventOutcome {
self.editor.handle_key_event(key_event)
}
}