From 9505c47b2deb0df6335a2781093d6c24faa6830d Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Mon, 25 Sep 2023 20:20:22 -0700 Subject: [PATCH] added multiple period input test --- src/uis/quick_transaction/paycheck.rs | 56 ++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/uis/quick_transaction/paycheck.rs b/src/uis/quick_transaction/paycheck.rs index 20c70e2..9240317 100644 --- a/src/uis/quick_transaction/paycheck.rs +++ b/src/uis/quick_transaction/paycheck.rs @@ -21,6 +21,7 @@ pub struct QuickPaycheckState { pub gross_amount: String, pub net_amount: String, selected: SelectedEntry, + has_error: Option, } impl QuickPaycheckState { @@ -29,6 +30,7 @@ impl QuickPaycheckState { net_amount: "".to_string(), gross_amount: "".to_string(), selected: SelectedEntry::Net, + has_error: None, } } @@ -54,6 +56,7 @@ impl QuickPaycheckState { "Take home:", &state.net_amount, state.selected == SelectedEntry::Net, + state.has_error == Some(SelectedEntry::Net) ); render_labled_entry_row( @@ -62,9 +65,20 @@ impl QuickPaycheckState { "Gross:", &state.gross_amount, state.selected == SelectedEntry::Gross, + state.has_error == Some(SelectedEntry::Gross) ); } + pub fn validate_input(&mut self) { + // The field only accepts digits and periods, so the only fail state + // should be multiple periods + if self.gross_amount.chars().filter(|c| *c == '.').count() > 1 { + self.has_error = Some(SelectedEntry::Gross); + } else if self.net_amount.chars().filter(|c| *c == '.').count() > 1 { + self.has_error = Some(SelectedEntry::Net); + } + } + pub fn handle_event(event: KeyEvent, app: &mut App) { if event.kind == KeyEventKind::Press { match event.code { @@ -80,6 +94,7 @@ impl QuickPaycheckState { app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross; } } + app.states.quick_transactions.paycheck_state.validate_input(); } KeyCode::Down | KeyCode::Tab => { match app.states.quick_transactions.paycheck_state.selected { @@ -93,14 +108,36 @@ impl QuickPaycheckState { app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Net; } } + app.states.quick_transactions.paycheck_state.validate_input(); + } + KeyCode::Enter => { + app.states.quick_transactions.paycheck_state.validate_input(); + match app.states.quick_transactions.paycheck_state.selected { + SelectedEntry::Net => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross; + } + SelectedEntry::Gross => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Execute; + } + SelectedEntry::Execute => { + if app.states.quick_transactions.paycheck_state.has_error.is_none() { + // TODO: actually do something here. E.g. Input validation, error + // messages, and actual submissions + } + } + } } KeyCode::Char(value) => { match app.states.quick_transactions.paycheck_state.selected { SelectedEntry::Net => { - app.states.quick_transactions.paycheck_state.net_amount.push(value); + if value.is_ascii_digit() || value == '.' { + app.states.quick_transactions.paycheck_state.net_amount.push(value); + } } SelectedEntry::Gross => { - app.states.quick_transactions.paycheck_state.gross_amount.push(value); + if value.is_ascii_digit() || value == '.' { + app.states.quick_transactions.paycheck_state.gross_amount.push(value); + } } _ => {} } @@ -134,6 +171,7 @@ fn render_labled_entry_row( left_text: &str, right_text: &String, do_highlight: bool, + is_error: bool, ) { let horizontal_pieces = Layout::default() .direction(Direction::Horizontal) @@ -150,10 +188,16 @@ fn render_labled_entry_row( horizontal_pieces[0], ); - let right_bg_color = if do_highlight { - Color::Yellow - } else { - Color::White + let right_bg_color = { + if do_highlight { + Color::Yellow + } else { + if is_error { + Color::LightRed + } else { + Color::White + } + } }; f.render_widget(