1
//! The template keys of the Templates tab.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::modal::Modal;
6
use crate::tabs::templates::TemplatesTab;
7
use crate::view::Tab;
8

            
9
/// What a template key asks for.
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub 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

            
18
12
pub(super) fn apply_tab(app: &mut App, key: TemplateKey) {
19
12
    match (app.front_kind(), key) {
20
4
        (Some(Tab::Templates), TemplateKey::Test) => app.test_selected_template(),
21
8
        (Some(Tab::Transactions), TemplateKey::Pick) => app.pick_template(),
22
        _ => {}
23
    }
24
12
}
25

            
26
3
pub(super) fn open_create(app: &mut App) {
27
3
    app.open_dialog_for(Tab::Templates, Modal::Form(Form::template_create()));
28
3
}
29

            
30
/// Loads the selected template into the edit form; says why when none is selected.
31
7
pub(super) fn open_edit_for_selected(app: &mut App) {
32
7
    match app
33
7
        .front::<TemplatesTab>()
34
7
        .and_then(|tab| tab.selected_key().map(str::to_string))
35
    {
36
3
        Some(id) => app.open_template_edit_async(&id),
37
4
        None => app.set_status("no template selected"),
38
    }
39
7
}
40

            
41
/// Confirms deleting the selected template; says why when none is selected.
42
4
pub(super) fn confirm_delete_selected(app: &mut App) {
43
4
    match app
44
4
        .front::<TemplatesTab>()
45
4
        .and_then(|tab| tab.selected_row().cloned())
46
    {
47
3
        Some(row) => app.confirm_delete_template(row),
48
1
        None => app.set_status("no template selected"),
49
    }
50
4
}