Skip to main content

tui/app/
form_options.rs

1//! Helpers that inject fetched Select options into an open form's fields,
2//! and methods that fetch option lists from the server.
3
4use crate::app::App;
5use crate::form::Field;
6use crate::route::{FormOptionsSource, RouteCtx};
7use crate::view::ViewId;
8use crate::widgets::{SelectOption, Widget};
9
10/// Inject options into the first unpopulated Select field (account-create pattern).
11pub(super) fn inject_select_options(
12    fields: &mut [Field],
13    options: &[SelectOption],
14    allow_repopulate: bool,
15) {
16    for field in fields {
17        if let Widget::Select(ref mut sw) = field.widget {
18            if allow_repopulate || sw.option_count() <= 1 {
19                sw.set_options(options.to_vec());
20            }
21            return;
22        }
23    }
24}
25
26/// Inject options into ALL Select fields (used for forms with multiple commodity Selects).
27pub(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
35pub(super) fn inject_splits_account_options(fields: &mut [Field], options: Vec<SelectOption>) {
36    for field in fields {
37        if let Widget::Splits(ref mut sw) = field.widget {
38            sw.set_account_options(options);
39            return;
40        }
41    }
42}
43
44pub(super) fn inject_splits_commodity_options(fields: &mut [Field], options: Vec<SelectOption>) {
45    for field in fields {
46        if let Widget::Splits(ref mut sw) = field.widget {
47            sw.set_commodity_options(options);
48            return;
49        }
50    }
51}
52
53impl 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    pub fn fetch_transaction_form_options(&mut self) {
75        if self.console_eval.is_none() {
76            return;
77        }
78        self.form_options_seq += 1;
79        let seq = self.form_options_seq;
80        let _ = self.dispatch_eval(
81            ViewId::Accounts,
82            RouteCtx::FormOptions {
83                seq,
84                source: FormOptionsSource::Accounts,
85            },
86            "(list-accounts)".to_string(),
87        );
88        let _ = self.dispatch_eval(
89            ViewId::Commodities,
90            RouteCtx::FormOptions {
91                seq,
92                source: FormOptionsSource::Commodities,
93            },
94            "(list-commodities)".to_string(),
95        );
96    }
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}