reverted core actions in canvas
This commit is contained in:
0
client/src/modes/canvas.rs
Normal file
0
client/src/modes/canvas.rs
Normal file
86
client/src/modes/canvas/common_mode.rs
Normal file
86
client/src/modes/canvas/common_mode.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
// src/modes/canvas/common_mode.rs
|
||||
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState, auth::AuthState};
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::tui::functions::common::form::SaveOutcome;
|
||||
use anyhow::{Context, Result};
|
||||
use crate::tui::functions::common::{
|
||||
form::{save as form_save, revert as form_revert},
|
||||
login::{save as login_save, revert as login_revert},
|
||||
register::{revert as register_revert},
|
||||
};
|
||||
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
auth_client: &mut AuthClient,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<EventOutcome> {
|
||||
match action {
|
||||
"save" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_save(auth_state, login_state, auth_client, app_state).await.context("Login save action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let save_outcome = form_save(
|
||||
app_state,
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await.context("Register save action failed")?;
|
||||
let message = match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
};
|
||||
Ok(EventOutcome::DataSaved(save_outcome, message))
|
||||
}
|
||||
},
|
||||
"force_quit" => {
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit("Force exiting without saving.".to_string()))
|
||||
},
|
||||
"save_and_quit" => {
|
||||
let message = if app_state.ui.show_login {
|
||||
login_save(auth_state, login_state, auth_client, app_state).await.context("Login save n quit action failed")?
|
||||
} else {
|
||||
let save_outcome = form_save(
|
||||
app_state,
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await?;
|
||||
match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
}
|
||||
};
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit(format!("{}. Exiting application.", message)))
|
||||
},
|
||||
"revert" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_revert(login_state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else if app_state.ui.show_register {
|
||||
let message = register_revert(register_state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let message = form_revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await.context("Form revert x action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
},
|
||||
_ => Ok(EventOutcome::Ok(format!("Core action not handled: {}", action))),
|
||||
}
|
||||
}
|
||||
@@ -754,8 +754,17 @@ impl EventHandler {
|
||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
// TODO: Replace with proper implementation
|
||||
return Ok(EventOutcome::Ok("Action not implemented".to_string()));
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state,
|
||||
login_state,
|
||||
register_state,
|
||||
terminal,
|
||||
app_state,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -800,8 +809,17 @@ impl EventHandler {
|
||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
// TODO: Replace with proper implementation
|
||||
return Ok(EventOutcome::Ok("Action not implemented".to_string()));
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state,
|
||||
login_state,
|
||||
register_state,
|
||||
terminal,
|
||||
app_state,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -1024,6 +1042,102 @@ impl EventHandler {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn handle_core_action(
|
||||
&mut self,
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<EventOutcome> {
|
||||
match action {
|
||||
"save" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = crate::tui::functions::common::login::save(
|
||||
auth_state,
|
||||
login_state,
|
||||
&mut self.auth_client,
|
||||
app_state,
|
||||
)
|
||||
.await?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let save_outcome = crate::tui::functions::common::form::save(
|
||||
app_state,
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?;
|
||||
let message = match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
};
|
||||
Ok(EventOutcome::DataSaved(save_outcome, message))
|
||||
}
|
||||
}
|
||||
"force_quit" => {
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit(
|
||||
"Force exiting without saving.".to_string(),
|
||||
))
|
||||
}
|
||||
"save_and_quit" => {
|
||||
let message = if app_state.ui.show_login {
|
||||
crate::tui::functions::common::login::save(
|
||||
auth_state,
|
||||
login_state,
|
||||
&mut self.auth_client,
|
||||
app_state,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
let save_outcome = crate::tui::functions::common::form::save(
|
||||
app_state,
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?;
|
||||
match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
}
|
||||
};
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit(format!(
|
||||
"{}. Exiting application.",
|
||||
message
|
||||
)))
|
||||
}
|
||||
"revert" => {
|
||||
let message = if app_state.ui.show_login {
|
||||
crate::tui::functions::common::login::revert(login_state, app_state)
|
||||
.await
|
||||
} else if app_state.ui.show_register {
|
||||
crate::tui::functions::common::register::revert(
|
||||
register_state,
|
||||
app_state,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
crate::tui::functions::common::form::revert(
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?
|
||||
};
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
_ => Ok(EventOutcome::Ok(format!(
|
||||
"Core action not handled: {}",
|
||||
action
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mode_transition_action(action: &str) -> bool {
|
||||
matches!(action,
|
||||
"exit" |
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
pub mod handlers;
|
||||
pub mod general;
|
||||
pub mod common;
|
||||
pub mod canvas;
|
||||
|
||||
pub use handlers::*;
|
||||
pub use general::*;
|
||||
pub use common::*;
|
||||
pub use canvas::*;
|
||||
|
||||
Reference in New Issue
Block a user