1
//! Functions that construct and push overlay modals onto the app's overlay stack.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::modal::Modal;
6
use crate::widgets::{Editor, SelectOption};
7

            
8
2
pub(super) fn open_config_set_modal(app: &mut App, args: &[(String, String)]) {
9
2
    let mut name = Editor::new(app.edit_mode);
10
2
    let mut value = Editor::new(app.edit_mode);
11
2
    for (k, v) in args {
12
2
        match k.as_str() {
13
2
            "name" => name = Editor::with_buffer(app.edit_mode, v.clone()),
14
1
            "value" => value = Editor::with_buffer(app.edit_mode, v.clone()),
15
            _ => {}
16
        }
17
    }
18
2
    app.overlays
19
2
        .push(Modal::Form(Form::config_set(name, value)));
20
2
}
21

            
22
pub(super) fn open_commodity_create_modal(app: &mut App) {
23
    app.overlays
24
        .push(Modal::Form(Form::commodity_create(app.edit_mode)));
25
}
26

            
27
pub(super) fn open_commodity_convert_modal(app: &mut App) {
28
    app.overlays
29
        .push(Modal::Form(Form::commodity_convert(app.edit_mode)));
30
    app.fetch_commodity_convert_form_options();
31
}
32

            
33
pub(super) fn open_account_create_modal(app: &mut App) {
34
    let none_opt = SelectOption {
35
        id: String::new(),
36
        label: "(none)".to_string(),
37
    };
38
    let form = Form::account_create(app.edit_mode, vec![none_opt]);
39
    app.overlays.push(Modal::Form(form));
40
    app.fetch_account_form_options();
41
}
42

            
43
1
pub(super) fn open_transaction_create_modal(app: &mut App) {
44
1
    app.overlays
45
1
        .push(Modal::Form(Form::transaction_create(app.edit_mode)));
46
1
    app.fetch_transaction_form_options();
47
1
}
48

            
49
1
pub(super) fn open_report_params_modal(
50
1
    app: &mut App,
51
1
    kind: crate::tabs::reports::ReportKind,
52
1
    chart_default: &str,
53
1
) {
54
1
    let mode = app.edit_mode;
55
1
    let chart = Editor::with_buffer(mode, chart_default.to_string());
56
1
    app.overlays.push(Modal::Form(Form::report_params(
57
1
        Editor::new(mode),
58
1
        Editor::new(mode),
59
1
        chart,
60
1
        kind,
61
1
    )));
62
1
}
63

            
64
/// Open the account-tag form for the currently selected account.
65
///
66
/// Sets a status message when no account is selected.
67
2
pub(super) fn open_account_tag_modal_for_selected(app: &mut App) {
68
2
    let Some(id) = app.accounts.selected_id().map(str::to_string) else {
69
1
        app.set_status("no account selected");
70
1
        return;
71
    };
72
1
    open_account_tag_modal(app, &id);
73
2
}
74

            
75
1
pub(crate) fn open_account_tag_modal(app: &mut App, account_id: &str) {
76
1
    app.overlays.push(Modal::Form(Form::account_tag(
77
1
        app.edit_mode,
78
1
        account_id.to_string(),
79
1
    )));
80
1
}
81

            
82
/// Open the transaction-tag form for the currently selected transaction.
83
///
84
/// Sets a status message when no transaction is selected.
85
pub(super) fn open_transaction_tag_modal_for_selected(app: &mut App) {
86
    let Some(id) = app.transactions.selected_id().map(str::to_string) else {
87
        app.set_status("no transaction selected");
88
        return;
89
    };
90
    open_transaction_tag_modal(app, &id);
91
}
92

            
93
pub(crate) fn open_transaction_tag_modal(app: &mut App, transaction_id: &str) {
94
    app.overlays.push(Modal::Form(Form::transaction_tag(
95
        app.edit_mode,
96
        transaction_id.to_string(),
97
    )));
98
}