Skip to main content

tui/app/
mutation.rs

1//! Mutation-submit methods for `App` (create, delete, set-tag, transaction forms).
2
3use crate::tabs::list::ListTab;
4use crate::tabs::reports::ReportsTab;
5use crate::tabs::seq::Seq;
6use cli_core::eval::{
7    build_delete_script_form, build_delete_tag_form, build_delete_template_form,
8    build_delete_transaction_form, build_remove_ssh_key_form,
9};
10use cli_core::transaction::submit_call;
11use forms::{
12    ACCOUNT_CREATE, ACCOUNT_EDIT, ACCOUNT_MERGE, COMMODITY_CREATE, COMMODITY_EDIT, CONFIG_SET,
13    FormValues, SCRIPT_CREATE, SCRIPT_EDIT, SSH_KEY_ADD, TAG_CREATE, TAG_EDIT, TEMPLATE_CREATE,
14    TEMPLATE_EDIT,
15};
16
17use crate::app::App;
18use crate::route::{RouteCtx, ScriptRoute, TemplateRoute};
19use crate::tabs::reports::ReportKind;
20use crate::view::ViewId;
21use crate::window::WindowId;
22
23impl App {
24    /// Dispatches a mutation of the open dialog whose success refreshes `view`. Returns `None`
25    /// if there is no eval worker or the dialog's window is gone.
26    fn dispatch_mutation(&mut self, view: ViewId, form: String) -> Option<i64> {
27        self.dispatch_mutation_as(RouteCtx::Mutation { refresh: view }, form)
28    }
29
30    fn dispatch_mutation_as(&mut self, ctx: RouteCtx, form: String) -> Option<i64> {
31        let window = self.dialog_window()?;
32        self.dispatch_mutation_in(window, ctx, form)
33    }
34
35    /// Dispatches a mutation whose reply goes to `window`.
36    pub(super) fn dispatch_mutation_in(
37        &mut self,
38        window: WindowId,
39        ctx: RouteCtx,
40        form: String,
41    ) -> Option<i64> {
42        if self.console_eval.is_none() {
43            self.status = "console not connected".to_string();
44            return None;
45        }
46        let id = self.dispatch_eval(window, ctx, form);
47        if id.is_none() {
48            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
49        }
50        id
51    }
52
53    pub fn submit_delete_transaction(&mut self, id: &str) -> Option<i64> {
54        let ctx = RouteCtx::DeleteTransaction { id: id.to_string() };
55        self.dispatch_mutation_as(ctx, build_delete_transaction_form(id))
56    }
57
58    pub fn submit_add_ssh_key(&mut self, values: &FormValues) -> Option<i64> {
59        let form = submit_call(&SSH_KEY_ADD, values);
60        self.dispatch_mutation_as(RouteCtx::SshKeyAdd, form)
61    }
62
63    pub fn submit_remove_ssh_key(&mut self, fingerprint: String) {
64        let form = build_remove_ssh_key_form(&fingerprint);
65        let ctx = RouteCtx::SshKeyRemove { fingerprint };
66        self.dispatch_mutation_as(ctx, form);
67    }
68
69    pub fn submit_tag_create(&mut self, values: &FormValues) -> Option<i64> {
70        self.dispatch_mutation_as(RouteCtx::TagCreate, submit_call(&TAG_CREATE, values))
71    }
72
73    pub fn submit_tag_edit(&mut self, values: &FormValues) -> Option<i64> {
74        self.dispatch_mutation(ViewId::Tags, submit_call(&TAG_EDIT, values))
75    }
76
77    pub fn submit_delete_tag(&mut self, id: &str, label: String) {
78        let ctx = RouteCtx::TagDelete { label };
79        self.dispatch_mutation_as(ctx, build_delete_tag_form(id));
80    }
81
82    pub fn submit_template_create(&mut self, values: &FormValues) -> Option<i64> {
83        self.dispatch_mutation_as(
84            RouteCtx::Template(TemplateRoute::Created),
85            submit_call(&TEMPLATE_CREATE, values),
86        )
87    }
88
89    pub fn submit_template_edit(&mut self, values: &FormValues) -> Option<i64> {
90        self.dispatch_mutation(ViewId::Templates, submit_call(&TEMPLATE_EDIT, values))
91    }
92
93    pub fn submit_script_create(&mut self, values: &FormValues) -> Option<i64> {
94        self.dispatch_mutation_as(
95            RouteCtx::Script(ScriptRoute::Created),
96            submit_call(&SCRIPT_CREATE, values),
97        )
98    }
99
100    pub fn submit_script_edit(&mut self, values: &FormValues) -> Option<i64> {
101        self.dispatch_mutation(ViewId::Scripts, submit_call(&SCRIPT_EDIT, values))
102    }
103
104    /// Submits the loaded script with the other state; the reply settles as `label`.
105    pub(super) fn submit_script_toggle(
106        &mut self,
107        window: WindowId,
108        values: &FormValues,
109        label: String,
110        enabled: bool,
111    ) {
112        let ctx = RouteCtx::Script(ScriptRoute::Toggled { label, enabled });
113        self.dispatch_mutation_in(window, ctx, submit_call(&SCRIPT_EDIT, values));
114    }
115
116    pub fn submit_delete_script(&mut self, id: &str, label: String) {
117        let ctx = RouteCtx::Script(ScriptRoute::Delete { label });
118        self.dispatch_mutation_as(ctx, build_delete_script_form(id));
119    }
120
121    pub fn submit_delete_template(&mut self, id: &str, label: String) {
122        let ctx = RouteCtx::Template(TemplateRoute::Delete { label });
123        self.dispatch_mutation_as(ctx, build_delete_template_form(id));
124    }
125
126    pub fn submit_commodity_create(&mut self, values: &FormValues) -> Option<i64> {
127        self.dispatch_mutation(ViewId::Commodities, submit_call(&COMMODITY_CREATE, values))
128    }
129
130    pub fn submit_commodity_edit(&mut self, values: &FormValues) -> Option<i64> {
131        self.dispatch_mutation(ViewId::Commodities, submit_call(&COMMODITY_EDIT, values))
132    }
133
134    pub fn submit_config_set(&mut self, values: &FormValues) -> Option<i64> {
135        let ctx = RouteCtx::ConfigSet {
136            key: values.text("key").unwrap_or_default().trim().to_string(),
137        };
138        self.dispatch_mutation_as(ctx, submit_call(&CONFIG_SET, values))
139    }
140
141    pub fn submit_account_create(&mut self, values: &FormValues) -> Option<i64> {
142        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_CREATE, values))
143    }
144
145    pub fn submit_account_edit(&mut self, values: &FormValues) -> Option<i64> {
146        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_EDIT, values))
147    }
148
149    pub fn submit_account_merge(&mut self, values: &FormValues) -> Option<i64> {
150        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_MERGE, values))
151    }
152
153    /// Runs a report form in Reports window `window`, where its reply renders. Returns `None`
154    /// if there is no eval worker, `window` is not a Reports window or the worker has stopped.
155    pub fn submit_report(
156        &mut self,
157        window: WindowId,
158        kind: ReportKind,
159        values: &FormValues,
160    ) -> Option<i64> {
161        if self.console_eval.is_none() {
162            self.status = "console not connected".to_string();
163            return None;
164        }
165        self.report_seq_of(window).map(Seq::advance);
166        let Some(reports) = self.win_mut::<ReportsTab>(window) else {
167            self.say_no_window(Some(ViewId::Reports));
168            return None;
169        };
170        reports.abandon_loading();
171        let seq = reports.run_seq.advance();
172        let ctx = RouteCtx::Reports {
173            kind,
174            seq,
175            values: values.clone(),
176        };
177        let id = self.dispatch_eval(window, ctx, submit_call(kind.descriptor(), values));
178        if id.is_none() {
179            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
180        }
181        id
182    }
183
184    /// Fetch the first page of a filter in Transactions window `window`; the window adopts the
185    /// filter only when the server accepts it.
186    pub fn submit_transaction_filter(
187        &mut self,
188        window: WindowId,
189        values: &FormValues,
190        summary: String,
191    ) -> Option<i64> {
192        if self.console_eval.is_none() {
193            self.set_status("console not connected");
194            return None;
195        }
196        let Some(call) = self
197            .win::<ListTab>(window)
198            .and_then(|list| list.filter_request(values))
199        else {
200            self.say_no_window(Some(ViewId::Transactions));
201            return None;
202        };
203        let ctx = RouteCtx::TransactionFilter {
204            seq: self.next_filter_seq(window)?,
205            filter: values.clone(),
206            summary,
207        };
208        let id = self.dispatch_eval(window, ctx, call);
209        if id.is_none() {
210            self.set_status(crate::tabs::nms_eval::UNAVAILABLE);
211        }
212        id
213    }
214
215    /// Submit a `transaction-create` or `transaction-edit` form.
216    pub fn submit_transaction(
217        &mut self,
218        descriptor: &forms::Form,
219        values: &FormValues,
220    ) -> Option<i64> {
221        self.dispatch_mutation(ViewId::Transactions, submit_call(descriptor, values))
222    }
223}