event.rs and ui.rs refactor for the forms page(moved logic to the forms page dir and just calling it now)
This commit is contained in:
62
client/src/pages/forms/event.rs
Normal file
62
client/src/pages/forms/event.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
// src/pages/forms/event.rs
|
||||
|
||||
use anyhow::Result;
|
||||
use crossterm::event::Event;
|
||||
use canvas::keymap::KeyEventOutcome;
|
||||
use crate::{
|
||||
state::app::state::AppState,
|
||||
pages::forms::{FormState, logic},
|
||||
modes::handlers::event::EventOutcome,
|
||||
};
|
||||
|
||||
pub fn handle_form_event(
|
||||
event: Event,
|
||||
app_state: &mut AppState,
|
||||
path: &str,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<EventOutcome> {
|
||||
if let Event::Key(key_event) = event {
|
||||
if let Some(editor) = app_state.editor_for_path(path) {
|
||||
match editor.handle_key_event(key_event) {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
return Ok(EventOutcome::Ok(msg));
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
return Ok(EventOutcome::Ok("Form input updated".into()));
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
return Ok(EventOutcome::Ok("Waiting for next key...".into()));
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
// fall through to navigation / save / revert
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(EventOutcome::Ok(String::new()))
|
||||
}
|
||||
|
||||
// Save wrapper
|
||||
pub async fn save_form(
|
||||
app_state: &mut AppState,
|
||||
path: &str,
|
||||
grpc_client: &mut crate::services::grpc_client::GrpcClient,
|
||||
) -> Result<EventOutcome> {
|
||||
let outcome = logic::save(app_state, path, grpc_client).await?;
|
||||
let message = match outcome {
|
||||
logic::SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
logic::SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
logic::SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
};
|
||||
Ok(EventOutcome::DataSaved(outcome, message))
|
||||
}
|
||||
|
||||
pub async fn revert_form(
|
||||
app_state: &mut AppState,
|
||||
path: &str,
|
||||
grpc_client: &mut crate::services::grpc_client::GrpcClient,
|
||||
) -> Result<EventOutcome> {
|
||||
let message = logic::revert(app_state, path, grpc_client).await?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
Reference in New Issue
Block a user