1
//! Filtering the Transactions tab from the palette and from the Accounts tab.
2

            
3
use cli_core::forms::Fetched;
4
use forms::{FormValues, Source, TRANSACTION_QUERY};
5

            
6
use super::App;
7
use super::lookup::Goal;
8
use super::pending::rejection;
9
use crate::form::Form;
10
use crate::tabs::list::ListTab;
11
use crate::tabs::paging::filter_summary;
12
use crate::view::Tab;
13
use crate::window::WindowId;
14

            
15
impl App {
16
    /// Filter the Transactions tab by `filter`, whose `account` may be a label.
17
24
    pub fn filter_transactions(&mut self, filter: FormValues) {
18
24
        let window = self.window_or_open(Tab::Transactions).window;
19
24
        self.start_lookup(Goal::Filter(window), &TRANSACTION_QUERY, filter);
20
24
    }
21

            
22
    /// Drop the Transactions filter and show the window.
23
4
    pub fn show_all_transactions(&mut self) {
24
4
        let window = self.window_or_open(Tab::Transactions).window;
25
4
        self.clear_transaction_filter(window);
26
4
        self.activate_window(window);
27
4
    }
28

            
29
    /// Filter the Transactions tab by the account selected on the Accounts tab.
30
10
    pub fn filter_transactions_of_selected_account(&mut self) {
31
10
        let Some(id) = self.selected_id(Tab::Accounts) else {
32
1
            self.set_status("no account selected");
33
1
            return;
34
        };
35
9
        let mut filter = FormValues::default();
36
9
        filter.set("account", &id);
37
9
        self.filter_transactions(filter);
38
10
    }
39

            
40
28
    pub(super) fn next_filter_seq(&mut self, window: WindowId) -> Option<u64> {
41
28
        self.win_mut::<ListTab>(window)
42
28
            .map(|list| list.filter_seq.advance())
43
28
    }
44

            
45
13
    pub(super) fn apply_resolved_filter(
46
13
        &mut self,
47
13
        window: WindowId,
48
13
        filter: &FormValues,
49
13
        fetched: &Fetched,
50
13
    ) {
51
13
        let label = filter
52
13
            .text("account")
53
5
            .and_then(|id| {
54
5
                let (_, options) = fetched.iter().find(|(s, _)| *s == Source::Accounts)?;
55
6
                options.iter().find(|option| option.id == id)
56
5
            })
57
13
            .map_or("", |option| option.label.as_str());
58
13
        let summary = filter_summary(label, filter);
59
13
        self.submit_transaction_filter(window, filter, summary);
60
13
    }
61

            
62
    /// Adopt the filter of an answered query, or report the server's refusal
63
    /// and keep the old one; a reply of a superseded request is dropped.
64
21
    pub(super) fn deliver_filter_reply(
65
21
        &mut self,
66
21
        window: WindowId,
67
21
        seq: u64,
68
21
        filter: &FormValues,
69
21
        summary: String,
70
21
        wire: &str,
71
21
    ) {
72
21
        let awaited = self
73
21
            .win::<ListTab>(window)
74
21
            .is_some_and(|list| list.filter_seq.awaits(seq));
75
21
        if !awaited {
76
2
            return;
77
19
        }
78
19
        if let Err(e) = cli_core::render::parse_wire(wire) {
79
1
            let mut form = Form::transaction_filter(filter);
80
1
            form.show_errors(&rejection(&e));
81
1
            self.set_status(format!("filter failed: {}", form.errors.join("; ")));
82
1
            return;
83
18
        }
84
18
        if let Some(list) = self.win_mut::<ListTab>(window) {
85
18
            list.set_filter(filter, summary);
86
        }
87
18
        self.deliver_transactions_reply(window, wire);
88
18
        self.show_window_if_free(window);
89
21
    }
90
}