Lines
87.5 %
Functions
50 %
Branches
100 %
//! Create-from-template: pick a template, then open the create form filled from its draft.
use cli_core::eval::escape_str;
use cli_core::forms::{parse_form_options, parse_loaded_reply};
use cli_core::render::parse_wire;
use forms::{FormValues, TRANSACTION_CREATE};
use super::form_options::select_options_of;
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::route::{RouteCtx, TemplateRoute};
use crate::view::ViewId;
use crate::window::Origin;
impl App {
/// Asks for the templates to pick from.
pub fn pick_template(&mut self) {
let seq = self.template_pick_seq.advance();
let origin = self.window_or_open(ViewId::Transactions);
let ctx = RouteCtx::Template(TemplateRoute::Pick { seq, origin });
let call = r#"(form-options "templates")"#.to_string();
if self.dispatch_app_eval(ctx, call).is_none() {
self.set_status("console not connected");
}
pub(super) fn deliver_template_pick(&mut self, seq: u64, origin: Origin, wire: &str) {
if !self.template_pick_seq.awaits(seq) {
return;
if let Err(e) = parse_wire(wire) {
return self.set_status(format!("can't list templates: {e}"));
let options = select_options_of(parse_form_options(wire));
if options.is_empty() {
return self.set_status("no templates");
if self.dialog_free("template picker") {
let form = Form::template_pick(&options);
self.open_dialog_at(origin, Modal::Form(form));
/// Loads the draft of the picked template; the form opens when it arrives.
pub fn load_from_template(&mut self, values: &FormValues) {
let Some(id) = values.text("template") else {
return self.set_status("no template chosen");
};
let call = format!(
"(load-form {} {})",
escape_str(TRANSACTION_CREATE.name),
escape_str(id)
);
let Some(origin) = self.dialog_origin() else {
let ctx = RouteCtx::Template(TemplateRoute::Load { origin });
pub(super) fn deliver_template_load(&mut self, origin: Origin, wire: &str) {
match parse_loaded_reply(&TRANSACTION_CREATE, wire) {
Ok(loaded) if self.dialog_free("create from template") => {
let form = Form::transaction_from_template(&loaded);
if self.open_dialog_at(origin, Modal::Form(form)) {
self.fetch_transaction_form_options();
Ok(_) => {}
Err(reason) => self.status = format!("can't create from this template: {reason}"),