1
//! Opening the account-merge form: the accounts it names are resolved against
2
//! the account options before the form opens.
3

            
4
use cli_core::account::{merge_preview_call, merge_preview_splits};
5
use cli_core::forms::Fetched;
6
use cli_core::render::FieldError;
7
use forms::{ACCOUNT_MERGE, FormValues, Source};
8
use scripting::nomiscript::Value;
9

            
10
use super::App;
11
use super::form_options::{inject_all_select_options, select_options_of, with_none};
12
use super::lookup::Goal;
13
use crate::form::Form;
14
use crate::modal::{ConfirmAction, Modal};
15
use crate::route::RouteCtx;
16
use crate::view::ViewId;
17
use crate::window::Origin;
18

            
19
impl App {
20
    /// Open the merge form with the `from`/`into` of `values` chosen, each an
21
    /// account label or id.
22
29
    pub fn open_account_merge(&mut self, mut values: FormValues) {
23
29
        for field in ACCOUNT_MERGE.fields {
24
58
            if values.text(field.key).is_none() {
25
30
                values.set(field.key, "");
26
28
            }
27
        }
28
29
        let origin = self.window_or_open(ViewId::Accounts);
29
29
        self.start_lookup(Goal::Merge(origin), &ACCOUNT_MERGE, values);
30
29
    }
31

            
32
24
    pub(super) fn open_resolved_merge(
33
24
        &mut self,
34
24
        origin: Origin,
35
24
        resolved: &FormValues,
36
24
        fetched: &Fetched,
37
24
    ) {
38
24
        if !self.dialog_free("merge") {
39
1
            return;
40
23
        }
41
23
        let mut form = Form::account_merge();
42
23
        for key in ["from", "into"] {
43
46
            if let Some(id) = resolved.text(key).filter(|id| !id.is_empty()) {
44
23
                form.preselect_account(key, id);
45
23
            }
46
        }
47
23
        let accounts = fetched
48
23
            .iter()
49
23
            .find(|(source, _)| *source == Source::Accounts)
50
23
            .map(|(_, options)| select_options_of(options.clone()))
51
            .unwrap_or_default();
52
23
        inject_all_select_options(&mut form.fields, &with_none(accounts));
53
23
        self.open_dialog_at(origin, Modal::Form(form));
54
24
    }
55

            
56
    /// Ask the server what merging the picked accounts moves; the form stays
57
    /// locked until the preview settles, and only a confirmed preview merges.
58
11
    pub(crate) fn preview_account_merge(&mut self, form: Form) {
59
11
        let call = merge_preview_call(&form.values());
60
11
        match self.dispatch_app_eval(RouteCtx::MergePreview, call) {
61
11
            Some(request_id) => self.lock_pending_form(request_id, form),
62
            None => {
63
                self.set_status("console not connected");
64
                self.overlays.push(Modal::Form(form));
65
            }
66
        }
67
11
    }
68

            
69
8
    pub(super) fn confirm_merge_preview(&mut self, form: Form, preview: &Value) {
70
8
        let Some(splits) = merge_preview_splits(preview) else {
71
1
            let malformed = FieldError {
72
1
                field: String::new(),
73
1
                message: "malformed merge preview".to_string(),
74
1
            };
75
1
            return self.unlock_pending_form(form, vec![malformed]);
76
        };
77
7
        if self.dialog_free("merge") {
78
6
            self.overlays.set_origin(form.origin);
79
6
            let prompt = merge_prompt(&form, splits);
80
6
            self.overlays.push(Modal::confirm(
81
6
                prompt,
82
6
                ConfirmAction::MergeAccounts(Box::new(form)),
83
6
            ));
84
1
        }
85
8
    }
86
}
87

            
88
/// Names both picked accounts by their labels and what the merge moves.
89
6
fn merge_prompt(form: &Form, splits: u64) -> String {
90
12
    let label = |key| form.select_label(key).unwrap_or_default();
91
6
    format!(
92
6
        "Merge account {} into {}: {splits} splits move, with its lots, budgets, \
93
         sub-accounts and tags, and it is deleted. Confirm?",
94
6
        label("from"),
95
6
        label("into")
96
    )
97
6
}