Skip to main content

tui/event/
template.rs

1//! The template keys of the Templates tab.
2
3use crate::app::App;
4use crate::form::Form;
5use crate::modal::Modal;
6use crate::tabs::templates::TemplatesTab;
7use crate::view::Tab;
8
9/// What a template key asks for.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum TemplateKey {
12    /// Render the source (stored, or being edited) into a draft.
13    Test,
14    /// Start a new transaction from a template picked from a list.
15    Pick,
16}
17
18pub(super) fn apply_tab(app: &mut App, key: TemplateKey) {
19    match (app.front_kind(), key) {
20        (Some(Tab::Templates), TemplateKey::Test) => app.test_selected_template(),
21        (Some(Tab::Transactions), TemplateKey::Pick) => app.pick_template(),
22        _ => {}
23    }
24}
25
26pub(super) fn open_create(app: &mut App) {
27    app.open_dialog_for(Tab::Templates, Modal::Form(Form::template_create()));
28}
29
30/// Loads the selected template into the edit form; says why when none is selected.
31pub(super) fn open_edit_for_selected(app: &mut App) {
32    match app
33        .front::<TemplatesTab>()
34        .and_then(|tab| tab.selected_key().map(str::to_string))
35    {
36        Some(id) => app.open_template_edit_async(&id),
37        None => app.set_status("no template selected"),
38    }
39}
40
41/// Confirms deleting the selected template; says why when none is selected.
42pub(super) fn confirm_delete_selected(app: &mut App) {
43    match app
44        .front::<TemplatesTab>()
45        .and_then(|tab| tab.selected_row().cloned())
46    {
47        Some(row) => app.confirm_delete_template(row),
48        None => app.set_status("no template selected"),
49    }
50}