1
//! Mutation-submit methods for `App` (create, delete, set-tag, transaction forms).
2

            
3
use crate::tabs::list::ListTab;
4
use crate::tabs::reports::ReportsTab;
5
use crate::tabs::seq::Seq;
6
use 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
};
10
use cli_core::transaction::submit_call;
11
use 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

            
17
use crate::app::App;
18
use crate::route::{RouteCtx, ScriptRoute, TemplateRoute};
19
use crate::tabs::reports::ReportKind;
20
use crate::view::ViewId;
21
use crate::window::WindowId;
22

            
23
impl 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
44
    fn dispatch_mutation(&mut self, view: ViewId, form: String) -> Option<i64> {
27
44
        self.dispatch_mutation_as(RouteCtx::Mutation { refresh: view }, form)
28
44
    }
29

            
30
103
    fn dispatch_mutation_as(&mut self, ctx: RouteCtx, form: String) -> Option<i64> {
31
103
        let window = self.dialog_window()?;
32
95
        self.dispatch_mutation_in(window, ctx, form)
33
103
    }
34

            
35
    /// Dispatches a mutation whose reply goes to `window`.
36
99
    pub(super) fn dispatch_mutation_in(
37
99
        &mut self,
38
99
        window: WindowId,
39
99
        ctx: RouteCtx,
40
99
        form: String,
41
99
    ) -> Option<i64> {
42
99
        if self.console_eval.is_none() {
43
21
            self.status = "console not connected".to_string();
44
21
            return None;
45
78
        }
46
78
        let id = self.dispatch_eval(window, ctx, form);
47
78
        if id.is_none() {
48
            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
49
78
        }
50
78
        id
51
99
    }
52

            
53
11
    pub fn submit_delete_transaction(&mut self, id: &str) -> Option<i64> {
54
11
        let ctx = RouteCtx::DeleteTransaction { id: id.to_string() };
55
11
        self.dispatch_mutation_as(ctx, build_delete_transaction_form(id))
56
11
    }
57

            
58
2
    pub fn submit_add_ssh_key(&mut self, values: &FormValues) -> Option<i64> {
59
2
        let form = submit_call(&SSH_KEY_ADD, values);
60
2
        self.dispatch_mutation_as(RouteCtx::SshKeyAdd, form)
61
2
    }
62

            
63
2
    pub fn submit_remove_ssh_key(&mut self, fingerprint: String) {
64
2
        let form = build_remove_ssh_key_form(&fingerprint);
65
2
        let ctx = RouteCtx::SshKeyRemove { fingerprint };
66
2
        self.dispatch_mutation_as(ctx, form);
67
2
    }
68

            
69
11
    pub fn submit_tag_create(&mut self, values: &FormValues) -> Option<i64> {
70
11
        self.dispatch_mutation_as(RouteCtx::TagCreate, submit_call(&TAG_CREATE, values))
71
11
    }
72

            
73
5
    pub fn submit_tag_edit(&mut self, values: &FormValues) -> Option<i64> {
74
5
        self.dispatch_mutation(ViewId::Tags, submit_call(&TAG_EDIT, values))
75
5
    }
76

            
77
2
    pub fn submit_delete_tag(&mut self, id: &str, label: String) {
78
2
        let ctx = RouteCtx::TagDelete { label };
79
2
        self.dispatch_mutation_as(ctx, build_delete_tag_form(id));
80
2
    }
81

            
82
2
    pub fn submit_template_create(&mut self, values: &FormValues) -> Option<i64> {
83
2
        self.dispatch_mutation_as(
84
2
            RouteCtx::Template(TemplateRoute::Created),
85
2
            submit_call(&TEMPLATE_CREATE, values),
86
2
        )
87
2
    }
88

            
89
3
    pub fn submit_template_edit(&mut self, values: &FormValues) -> Option<i64> {
90
3
        self.dispatch_mutation(ViewId::Templates, submit_call(&TEMPLATE_EDIT, values))
91
3
    }
92

            
93
3
    pub fn submit_script_create(&mut self, values: &FormValues) -> Option<i64> {
94
3
        self.dispatch_mutation_as(
95
3
            RouteCtx::Script(ScriptRoute::Created),
96
3
            submit_call(&SCRIPT_CREATE, values),
97
3
        )
98
3
    }
99

            
100
3
    pub fn submit_script_edit(&mut self, values: &FormValues) -> Option<i64> {
101
3
        self.dispatch_mutation(ViewId::Scripts, submit_call(&SCRIPT_EDIT, values))
102
3
    }
103

            
104
    /// Submits the loaded script with the other state; the reply settles as `label`.
105
4
    pub(super) fn submit_script_toggle(
106
4
        &mut self,
107
4
        window: WindowId,
108
4
        values: &FormValues,
109
4
        label: String,
110
4
        enabled: bool,
111
4
    ) {
112
4
        let ctx = RouteCtx::Script(ScriptRoute::Toggled { label, enabled });
113
4
        self.dispatch_mutation_in(window, ctx, submit_call(&SCRIPT_EDIT, values));
114
4
    }
115

            
116
2
    pub fn submit_delete_script(&mut self, id: &str, label: String) {
117
2
        let ctx = RouteCtx::Script(ScriptRoute::Delete { label });
118
2
        self.dispatch_mutation_as(ctx, build_delete_script_form(id));
119
2
    }
120

            
121
2
    pub fn submit_delete_template(&mut self, id: &str, label: String) {
122
2
        let ctx = RouteCtx::Template(TemplateRoute::Delete { label });
123
2
        self.dispatch_mutation_as(ctx, build_delete_template_form(id));
124
2
    }
125

            
126
15
    pub fn submit_commodity_create(&mut self, values: &FormValues) -> Option<i64> {
127
15
        self.dispatch_mutation(ViewId::Commodities, submit_call(&COMMODITY_CREATE, values))
128
15
    }
129

            
130
1
    pub fn submit_commodity_edit(&mut self, values: &FormValues) -> Option<i64> {
131
1
        self.dispatch_mutation(ViewId::Commodities, submit_call(&COMMODITY_EDIT, values))
132
1
    }
133

            
134
22
    pub fn submit_config_set(&mut self, values: &FormValues) -> Option<i64> {
135
22
        let ctx = RouteCtx::ConfigSet {
136
22
            key: values.text("key").unwrap_or_default().trim().to_string(),
137
22
        };
138
22
        self.dispatch_mutation_as(ctx, submit_call(&CONFIG_SET, values))
139
22
    }
140

            
141
5
    pub fn submit_account_create(&mut self, values: &FormValues) -> Option<i64> {
142
5
        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_CREATE, values))
143
5
    }
144

            
145
1
    pub fn submit_account_edit(&mut self, values: &FormValues) -> Option<i64> {
146
1
        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_EDIT, values))
147
1
    }
148

            
149
3
    pub fn submit_account_merge(&mut self, values: &FormValues) -> Option<i64> {
150
3
        self.dispatch_mutation(ViewId::Accounts, submit_call(&ACCOUNT_MERGE, values))
151
3
    }
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
133
    pub fn submit_report(
156
133
        &mut self,
157
133
        window: WindowId,
158
133
        kind: ReportKind,
159
133
        values: &FormValues,
160
133
    ) -> Option<i64> {
161
133
        if self.console_eval.is_none() {
162
            self.status = "console not connected".to_string();
163
            return None;
164
133
        }
165
133
        self.report_seq_of(window).map(Seq::advance);
166
133
        let Some(reports) = self.win_mut::<ReportsTab>(window) else {
167
1
            self.say_no_window(Some(ViewId::Reports));
168
1
            return None;
169
        };
170
132
        reports.abandon_loading();
171
132
        let seq = reports.run_seq.advance();
172
132
        let ctx = RouteCtx::Reports {
173
132
            kind,
174
132
            seq,
175
132
            values: values.clone(),
176
132
        };
177
132
        let id = self.dispatch_eval(window, ctx, submit_call(kind.descriptor(), values));
178
132
        if id.is_none() {
179
            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
180
132
        }
181
132
        id
182
133
    }
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
22
    pub fn submit_transaction_filter(
187
22
        &mut self,
188
22
        window: WindowId,
189
22
        values: &FormValues,
190
22
        summary: String,
191
22
    ) -> Option<i64> {
192
22
        if self.console_eval.is_none() {
193
            self.set_status("console not connected");
194
            return None;
195
22
        }
196
22
        let Some(call) = self
197
22
            .win::<ListTab>(window)
198
21
            .and_then(|list| list.filter_request(values))
199
        else {
200
1
            self.say_no_window(Some(ViewId::Transactions));
201
1
            return None;
202
        };
203
        let ctx = RouteCtx::TransactionFilter {
204
21
            seq: self.next_filter_seq(window)?,
205
21
            filter: values.clone(),
206
21
            summary,
207
        };
208
21
        let id = self.dispatch_eval(window, ctx, call);
209
21
        if id.is_none() {
210
            self.set_status(crate::tabs::nms_eval::UNAVAILABLE);
211
21
        }
212
21
        id
213
22
    }
214

            
215
    /// Submit a `transaction-create` or `transaction-edit` form.
216
8
    pub fn submit_transaction(
217
8
        &mut self,
218
8
        descriptor: &forms::Form,
219
8
        values: &FormValues,
220
8
    ) -> Option<i64> {
221
8
        self.dispatch_mutation(ViewId::Transactions, submit_call(descriptor, values))
222
8
    }
223
}