Skip to main content

tui/app/
edit.rs

1//! Opening the transaction-, account- and commodity-edit forms from a `load-form` reply.
2
3use cli_core::eval::escape_str;
4use cli_core::forms::parse_loaded_reply;
5use forms::{
6    ACCOUNT_EDIT, COMMODITY_EDIT, FormValues, SCRIPT_EDIT, TAG_EDIT, TEMPLATE_EDIT,
7    TRANSACTION_EDIT,
8};
9
10use crate::app::App;
11use crate::form::Form;
12use crate::modal::Modal;
13use crate::route::{RouteCtx, ScriptRoute, TemplateRoute};
14use crate::view::ViewId;
15use crate::window::WindowId;
16
17impl App {
18    /// Dispatch `(load-form "transaction-edit" "<id>")` with `RouteCtx::TransactionEdit`.
19    ///
20    /// The async reply opens the edit form, or shows why the transaction
21    /// cannot be edited.
22    pub fn open_transaction_edit_async(&mut self, id: &str) {
23        self.load_for_edit(
24            ViewId::Transactions,
25            RouteCtx::TransactionEdit,
26            TRANSACTION_EDIT.name,
27            (id, "transaction"),
28        );
29    }
30
31    /// Dispatch `(load-form "account-edit" "<id>")` with `RouteCtx::AccountEdit`.
32    pub fn open_account_edit_async(&mut self, id: &str) {
33        self.load_for_edit(
34            ViewId::Accounts,
35            RouteCtx::AccountEdit,
36            ACCOUNT_EDIT.name,
37            (id, "account"),
38        );
39    }
40
41    /// Dispatch `(load-form "commodity-edit" "<id>")` with `RouteCtx::CommodityEdit`.
42    pub fn open_commodity_edit_async(&mut self, id: &str) {
43        self.load_for_edit(
44            ViewId::Commodities,
45            RouteCtx::CommodityEdit,
46            COMMODITY_EDIT.name,
47            (id, "commodity"),
48        );
49    }
50
51    /// Dispatch `(load-form "tag-edit" "<id>")` with `RouteCtx::TagEdit`.
52    pub fn open_tag_edit_async(&mut self, id: &str) {
53        self.load_for_edit(ViewId::Tags, RouteCtx::TagEdit, TAG_EDIT.name, (id, "tag"));
54    }
55
56    /// Dispatch `(load-form "template-edit" "<id>")` with `TemplateRoute::Edit`.
57    pub fn open_template_edit_async(&mut self, id: &str) {
58        let ctx = RouteCtx::Template(TemplateRoute::Edit);
59        self.load_for_edit(ViewId::Templates, ctx, TEMPLATE_EDIT.name, (id, "template"));
60    }
61
62    /// Dispatch `(load-form "script-edit" "<id>")` with `ScriptRoute::Edit`.
63    pub fn open_script_edit_async(&mut self, id: &str) {
64        let ctx = RouteCtx::Script(ScriptRoute::Edit);
65        self.load_for_edit(ViewId::Scripts, ctx, SCRIPT_EDIT.name, (id, "script"));
66    }
67
68    /// As `open_transaction_edit_async`, from `window` whatever it shows: a detail's edit.
69    pub fn open_transaction_edit_from(&mut self, window: WindowId, id: &str) {
70        self.load_edit_in(
71            window,
72            RouteCtx::TransactionEdit,
73            TRANSACTION_EDIT.name,
74            (id, "transaction"),
75        );
76    }
77
78    pub(super) fn load_for_edit(
79        &mut self,
80        view: ViewId,
81        ctx: RouteCtx,
82        name: &str,
83        what: (&str, &str),
84    ) {
85        if let Some(window) = self.front_or_say(view) {
86            self.load_edit_in(window, ctx, name, what);
87        }
88    }
89
90    /// Asks the form of `id` for the window `window` names; the reply opens it as its dialog.
91    fn load_edit_in(
92        &mut self,
93        window: WindowId,
94        ctx: RouteCtx,
95        name: &str,
96        (id, what): (&str, &str),
97    ) {
98        if self.console_eval.is_none() {
99            self.status = "console not connected".to_string();
100            return;
101        }
102        if self
103            .pending_routes
104            .values()
105            .any(|route| route.ctx.is_same_request(&ctx))
106        {
107            self.status = format!("the {what} is already loading");
108            return;
109        }
110        let form = format!("(load-form {} {})", escape_str(name), escape_str(id));
111        if self.dispatch_eval(window, ctx, form).is_none() {
112            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
113        }
114    }
115
116    /// Called from `deliver_reply` when a `TransactionEdit` route reply arrives.
117    pub(super) fn deliver_transaction_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
118        self.open_loaded(
119            target,
120            &TRANSACTION_EDIT,
121            wire,
122            "transaction",
123            |app, loaded| {
124                let form = Form::transaction_edit(loaded);
125                app.overlays.push(Modal::Form(form));
126                app.fetch_transaction_form_options();
127            },
128        );
129    }
130
131    /// Called from `deliver_reply` when an `AccountEdit` route reply arrives.
132    pub(super) fn deliver_account_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
133        self.open_loaded(target, &ACCOUNT_EDIT, wire, "account", |app, loaded| {
134            let form = Form::account_edit(loaded);
135            app.overlays.push(Modal::Form(form));
136            app.fetch_account_form_options();
137        });
138    }
139
140    /// Called from `deliver_reply` when a `CommodityEdit` route reply arrives.
141    pub(super) fn deliver_commodity_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
142        self.open_loaded(target, &COMMODITY_EDIT, wire, "commodity", |app, loaded| {
143            let form = Form::commodity_edit(loaded);
144            app.overlays.push(Modal::Form(form));
145        });
146    }
147
148    /// Called from `deliver_reply` when a `TagEdit` route reply arrives.
149    pub(super) fn deliver_tag_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
150        self.open_loaded(target, &TAG_EDIT, wire, "tag", |app, loaded| {
151            let form = Form::tag_edit(loaded);
152            app.overlays.push(Modal::Form(form));
153        });
154    }
155
156    /// Called when a `TemplateRoute::Edit` reply arrives.
157    pub(super) fn deliver_template_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
158        self.open_loaded(target, &TEMPLATE_EDIT, wire, "template", |app, loaded| {
159            let form = Form::template_edit(loaded);
160            app.overlays.push(Modal::Form(form));
161        });
162    }
163
164    /// Called when a `ScriptRoute::Edit` reply arrives; the server's notice rides on the form.
165    pub(super) fn deliver_script_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
166        let notices = rpc::parse_notices(wire);
167        self.open_loaded(target, &SCRIPT_EDIT, wire, "script", |app, loaded| {
168            let mut form = Form::script_edit(loaded);
169            form.notices = notices;
170            app.overlays.push(Modal::Form(form));
171        });
172    }
173
174    fn open_loaded(
175        &mut self,
176        target: Option<WindowId>,
177        descriptor: &forms::Form,
178        wire: &str,
179        what: &str,
180        open: impl FnOnce(&mut Self, &FormValues),
181    ) {
182        match parse_loaded_reply(descriptor, wire) {
183            Ok(loaded) if self.dialog_free("edit") => {
184                if self.begin_dialog_of(target) {
185                    open(self, &loaded);
186                }
187            }
188            Ok(_) => {}
189            Err(reason) => self.status = format!("can't edit this {what}: {reason}"),
190        }
191    }
192}