1
//! Helpers that inject fetched Select options into an open form's fields,
2
//! and methods that fetch option lists from the server.
3

            
4
use crate::app::App;
5
use crate::form::Field;
6
use crate::route::{FormOptionsSource, RouteCtx};
7
use crate::view::ViewId;
8
use crate::widgets::{SelectOption, Widget};
9

            
10
/// Inject options into the first unpopulated Select field (account-create pattern).
11
2
pub(super) fn inject_select_options(
12
2
    fields: &mut [Field],
13
2
    options: &[SelectOption],
14
2
    allow_repopulate: bool,
15
2
) {
16
4
    for field in fields {
17
4
        if let Widget::Select(ref mut sw) = field.widget {
18
2
            if allow_repopulate || sw.option_count() <= 1 {
19
1
                sw.set_options(options.to_vec());
20
1
            }
21
2
            return;
22
2
        }
23
    }
24
2
}
25

            
26
/// Inject options into ALL Select fields (used for forms with multiple commodity Selects).
27
pub(super) fn inject_all_select_options(fields: &mut [Field], options: &[SelectOption]) {
28
    for field in fields {
29
        if let Widget::Select(ref mut sw) = field.widget {
30
            sw.set_options(options.to_vec());
31
        }
32
    }
33
}
34

            
35
1
pub(super) fn inject_splits_account_options(fields: &mut [Field], options: Vec<SelectOption>) {
36
3
    for field in fields {
37
3
        if let Widget::Splits(ref mut sw) = field.widget {
38
1
            sw.set_account_options(options);
39
1
            return;
40
2
        }
41
    }
42
1
}
43

            
44
1
pub(super) fn inject_splits_commodity_options(fields: &mut [Field], options: Vec<SelectOption>) {
45
3
    for field in fields {
46
3
        if let Widget::Splits(ref mut sw) = field.widget {
47
1
            sw.set_commodity_options(options);
48
1
            return;
49
2
        }
50
    }
51
1
}
52

            
53
impl App {
54
    /// Issue a `list-accounts` fetch to populate the parent Select in the open
55
    /// account-create form.  Bumps `form_options_seq` so a stale reply is dropped.
56
    pub fn fetch_account_form_options(&mut self) {
57
        if self.console_eval.is_none() {
58
            return;
59
        }
60
        self.form_options_seq += 1;
61
        let seq = self.form_options_seq;
62
        let _ = self.dispatch_eval(
63
            ViewId::Accounts,
64
            RouteCtx::FormOptions {
65
                seq,
66
                source: FormOptionsSource::Accounts,
67
            },
68
            "(list-accounts)".to_string(),
69
        );
70
    }
71

            
72
    /// Issue `list-accounts` + `list-commodities` to populate the Selects in
73
    /// the open transaction-create/edit form.  Both requests share the same seq.
74
1
    pub fn fetch_transaction_form_options(&mut self) {
75
1
        if self.console_eval.is_none() {
76
            return;
77
1
        }
78
1
        self.form_options_seq += 1;
79
1
        let seq = self.form_options_seq;
80
1
        let _ = self.dispatch_eval(
81
1
            ViewId::Accounts,
82
1
            RouteCtx::FormOptions {
83
1
                seq,
84
1
                source: FormOptionsSource::Accounts,
85
1
            },
86
1
            "(list-accounts)".to_string(),
87
1
        );
88
1
        let _ = self.dispatch_eval(
89
1
            ViewId::Commodities,
90
1
            RouteCtx::FormOptions {
91
1
                seq,
92
1
                source: FormOptionsSource::Commodities,
93
1
            },
94
1
            "(list-commodities)".to_string(),
95
1
        );
96
1
    }
97

            
98
    /// Issue a `list-commodities` fetch to populate the two Selects in the open
99
    /// commodity-convert form.  Bumps `form_options_seq` so a stale reply is dropped.
100
    pub fn fetch_commodity_convert_form_options(&mut self) {
101
        if self.console_eval.is_none() {
102
            return;
103
        }
104
        self.form_options_seq += 1;
105
        let seq = self.form_options_seq;
106
        let _ = self.dispatch_eval(
107
            ViewId::Commodities,
108
            RouteCtx::FormOptions {
109
                seq,
110
                source: FormOptionsSource::Commodities,
111
            },
112
            "(list-commodities)".to_string(),
113
        );
114
    }
115
}