diff --git a/core/src/action.rs b/core/src/action.rs index 0df6928..5cd8c9b 100644 --- a/core/src/action.rs +++ b/core/src/action.rs @@ -64,6 +64,8 @@ pub enum Action { PostResponse { color: ResponseColor, text: String }, ResetTicket, SelectTicket(u16), + UserNo, + UserYes, // Screens DismissPopup, DisplayPopup(PopupType, String), diff --git a/core/src/components/popup.rs b/core/src/components/popup.rs index 32c0292..498f322 100644 --- a/core/src/components/popup.rs +++ b/core/src/components/popup.rs @@ -14,6 +14,7 @@ // along with Deja-Vu. If not, see . // use color_eyre::Result; +use crossterm::event::KeyCode; use rand::random; use ratatui::{ prelude::*, @@ -25,13 +26,14 @@ use tokio::sync::mpsc::UnboundedSender; use tracing::info; use super::Component; -use crate::{action::Action, config::Config}; +use crate::{action::Action, config::Config, tui::Event}; #[derive(Default, Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] pub enum Type { #[default] Info, Error, + Question, Success, } @@ -39,6 +41,7 @@ pub enum Type { pub struct Popup { command_tx: Option>, config: Config, + get_input: bool, popup_type: Type, popup_text: String, } @@ -46,7 +49,10 @@ pub struct Popup { impl Popup { #[must_use] pub fn new() -> Self { - Self::default() + Self { + get_input: false, + ..Default::default() + } } } @@ -61,11 +67,34 @@ impl Component for Popup { Ok(()) } + fn handle_events(&mut self, event: Option) -> Result> { + if !self.get_input { + return Ok(None); + } + let action = match event { + Some(Event::Key(key_event)) => match key_event.code { + KeyCode::Char(c) => { + let response = c.to_ascii_lowercase(); + match response { + 'n' => Some(Action::UserNo), + 'y' => Some(Action::UserYes), + _ => None, + } + } + _ => None, + }, + Some(Event::Mouse(_)) => None, + _ => None, + }; + Ok(action) + } + fn update(&mut self, action: Action) -> Result> { match action { Action::DismissPopup => self.popup_text.clear(), Action::DisplayPopup(new_type, new_text) => { info!("Show Popup ({new_type}): {new_text}"); + self.get_input = new_type == Type::Question; self.popup_type = new_type; self.popup_text = format!("\n{new_text}"); } @@ -85,6 +114,7 @@ impl Component for Popup { match self.popup_type { Type::Info => style = style.cyan(), Type::Error => style = style.red(), + Type::Question => style = style.yellow(), Type::Success => style = style.green(), } diff --git a/core/src/components/select_ticket.rs b/core/src/components/select_ticket.rs index f15544f..900d294 100644 --- a/core/src/components/select_ticket.rs +++ b/core/src/components/select_ticket.rs @@ -23,7 +23,10 @@ use tokio::sync::mpsc::UnboundedSender; use tui_input::{Input, InputRequest}; use super::Component; -use crate::{action::Action, config::Config, osticket::OsTicket, state::Mode, tui::Event}; +use crate::{ + action::Action, components::popup::Type as PopupType, config::Config, osticket::OsTicket, + state::Mode, tui::Event, +}; #[derive(Default)] pub struct TicketSelection { @@ -82,12 +85,10 @@ impl Component for TicketSelection { result } KeyCode::Enter => Some(Action::SetMode(Mode::ScanDisks)), - KeyCode::Esc => { - if let Ok(mut osticket) = self.osticket_arc.lock() { - osticket.disable(); - } - Some(Action::NextScreen) - } + KeyCode::Esc => Some(Action::DisplayPopup( + PopupType::Question, + String::from("Disable osTicket integration?\n\nY / N"), + )), _ => None, }, Some(Event::Mouse(_)) => None, diff --git a/deja_vu/src/app.rs b/deja_vu/src/app.rs index 0eeae9e..cfc3c91 100644 --- a/deja_vu/src/app.rs +++ b/deja_vu/src/app.rs @@ -595,6 +595,13 @@ impl App { }; } Action::TasksComplete => self.action_tx.send(Action::NextScreen)?, + Action::UserNo => self.action_tx.send(Action::DismissPopup)?, + Action::UserYes => { + if let Ok(mut osticket) = self.osticket_arc.lock() { + osticket.disable(); + } + self.action_tx.send(Action::NextScreen)?; + } _ => {} } for component in &mut self.components {