Lines
96.61 %
Functions
50 %
Branches
100 %
//! Opening the account-merge form: the accounts it names are resolved against
//! the account options before the form opens.
use cli_core::account::{merge_preview_call, merge_preview_splits};
use cli_core::forms::Fetched;
use cli_core::render::FieldError;
use forms::{ACCOUNT_MERGE, FormValues, Source};
use scripting::nomiscript::Value;
use super::App;
use super::form_options::{inject_all_select_options, select_options_of, with_none};
use super::lookup::Goal;
use crate::form::Form;
use crate::modal::{ConfirmAction, Modal};
use crate::route::RouteCtx;
use crate::view::ViewId;
use crate::window::Origin;
impl App {
/// Open the merge form with the `from`/`into` of `values` chosen, each an
/// account label or id.
pub fn open_account_merge(&mut self, mut values: FormValues) {
for field in ACCOUNT_MERGE.fields {
if values.text(field.key).is_none() {
values.set(field.key, "");
}
let origin = self.window_or_open(ViewId::Accounts);
self.start_lookup(Goal::Merge(origin), &ACCOUNT_MERGE, values);
pub(super) fn open_resolved_merge(
&mut self,
origin: Origin,
resolved: &FormValues,
fetched: &Fetched,
) {
if !self.dialog_free("merge") {
return;
let mut form = Form::account_merge();
for key in ["from", "into"] {
if let Some(id) = resolved.text(key).filter(|id| !id.is_empty()) {
form.preselect_account(key, id);
let accounts = fetched
.iter()
.find(|(source, _)| *source == Source::Accounts)
.map(|(_, options)| select_options_of(options.clone()))
.unwrap_or_default();
inject_all_select_options(&mut form.fields, &with_none(accounts));
self.open_dialog_at(origin, Modal::Form(form));
/// Ask the server what merging the picked accounts moves; the form stays
/// locked until the preview settles, and only a confirmed preview merges.
pub(crate) fn preview_account_merge(&mut self, form: Form) {
let call = merge_preview_call(&form.values());
match self.dispatch_app_eval(RouteCtx::MergePreview, call) {
Some(request_id) => self.lock_pending_form(request_id, form),
None => {
self.set_status("console not connected");
self.overlays.push(Modal::Form(form));
pub(super) fn confirm_merge_preview(&mut self, form: Form, preview: &Value) {
let Some(splits) = merge_preview_splits(preview) else {
let malformed = FieldError {
field: String::new(),
message: "malformed merge preview".to_string(),
};
return self.unlock_pending_form(form, vec![malformed]);
if self.dialog_free("merge") {
self.overlays.set_origin(form.origin);
let prompt = merge_prompt(&form, splits);
self.overlays.push(Modal::confirm(
prompt,
ConfirmAction::MergeAccounts(Box::new(form)),
));
/// Names both picked accounts by their labels and what the merge moves.
fn merge_prompt(form: &Form, splits: u64) -> String {
let label = |key| form.select_label(key).unwrap_or_default();
format!(
"Merge account {} into {}: {splits} splits move, with its lots, budgets, \
sub-accounts and tags, and it is deleted. Confirm?",
label("from"),
label("into")
)