1
//! Opening the transaction-, account- and commodity-edit forms from a `load-form` reply.
2

            
3
use cli_core::eval::escape_str;
4
use cli_core::forms::parse_loaded_reply;
5
use forms::{
6
    ACCOUNT_EDIT, COMMODITY_EDIT, FormValues, SCRIPT_EDIT, TAG_EDIT, TEMPLATE_EDIT,
7
    TRANSACTION_EDIT,
8
};
9

            
10
use crate::app::App;
11
use crate::form::Form;
12
use crate::modal::Modal;
13
use crate::route::{RouteCtx, ScriptRoute, TemplateRoute};
14
use crate::view::ViewId;
15
use crate::window::WindowId;
16

            
17
impl 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
7
    pub fn open_transaction_edit_async(&mut self, id: &str) {
23
7
        self.load_for_edit(
24
7
            ViewId::Transactions,
25
7
            RouteCtx::TransactionEdit,
26
7
            TRANSACTION_EDIT.name,
27
7
            (id, "transaction"),
28
7
        );
29
7
    }
30

            
31
    /// Dispatch `(load-form "account-edit" "<id>")` with `RouteCtx::AccountEdit`.
32
4
    pub fn open_account_edit_async(&mut self, id: &str) {
33
4
        self.load_for_edit(
34
4
            ViewId::Accounts,
35
4
            RouteCtx::AccountEdit,
36
4
            ACCOUNT_EDIT.name,
37
4
            (id, "account"),
38
4
        );
39
4
    }
40

            
41
    /// Dispatch `(load-form "commodity-edit" "<id>")` with `RouteCtx::CommodityEdit`.
42
3
    pub fn open_commodity_edit_async(&mut self, id: &str) {
43
3
        self.load_for_edit(
44
3
            ViewId::Commodities,
45
3
            RouteCtx::CommodityEdit,
46
3
            COMMODITY_EDIT.name,
47
3
            (id, "commodity"),
48
3
        );
49
3
    }
50

            
51
    /// Dispatch `(load-form "tag-edit" "<id>")` with `RouteCtx::TagEdit`.
52
6
    pub fn open_tag_edit_async(&mut self, id: &str) {
53
6
        self.load_for_edit(ViewId::Tags, RouteCtx::TagEdit, TAG_EDIT.name, (id, "tag"));
54
6
    }
55

            
56
    /// Dispatch `(load-form "template-edit" "<id>")` with `TemplateRoute::Edit`.
57
3
    pub fn open_template_edit_async(&mut self, id: &str) {
58
3
        let ctx = RouteCtx::Template(TemplateRoute::Edit);
59
3
        self.load_for_edit(ViewId::Templates, ctx, TEMPLATE_EDIT.name, (id, "template"));
60
3
    }
61

            
62
    /// Dispatch `(load-form "script-edit" "<id>")` with `ScriptRoute::Edit`.
63
3
    pub fn open_script_edit_async(&mut self, id: &str) {
64
3
        let ctx = RouteCtx::Script(ScriptRoute::Edit);
65
3
        self.load_for_edit(ViewId::Scripts, ctx, SCRIPT_EDIT.name, (id, "script"));
66
3
    }
67

            
68
    /// As `open_transaction_edit_async`, from `window` whatever it shows: a detail's edit.
69
4
    pub fn open_transaction_edit_from(&mut self, window: WindowId, id: &str) {
70
4
        self.load_edit_in(
71
4
            window,
72
4
            RouteCtx::TransactionEdit,
73
4
            TRANSACTION_EDIT.name,
74
4
            (id, "transaction"),
75
4
        );
76
4
    }
77

            
78
32
    pub(super) fn load_for_edit(
79
32
        &mut self,
80
32
        view: ViewId,
81
32
        ctx: RouteCtx,
82
32
        name: &str,
83
32
        what: (&str, &str),
84
32
    ) {
85
32
        if let Some(window) = self.front_or_say(view) {
86
31
            self.load_edit_in(window, ctx, name, what);
87
1
        }
88
32
    }
89

            
90
    /// Asks the form of `id` for the window `window` names; the reply opens it as its dialog.
91
35
    fn load_edit_in(
92
35
        &mut self,
93
35
        window: WindowId,
94
35
        ctx: RouteCtx,
95
35
        name: &str,
96
35
        (id, what): (&str, &str),
97
35
    ) {
98
35
        if self.console_eval.is_none() {
99
4
            self.status = "console not connected".to_string();
100
4
            return;
101
31
        }
102
31
        if self
103
31
            .pending_routes
104
31
            .values()
105
3
            .any(|route| route.ctx.is_same_request(&ctx))
106
        {
107
1
            self.status = format!("the {what} is already loading");
108
1
            return;
109
30
        }
110
30
        let form = format!("(load-form {} {})", escape_str(name), escape_str(id));
111
30
        if self.dispatch_eval(window, ctx, form).is_none() {
112
            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
113
30
        }
114
35
    }
115

            
116
    /// Called from `deliver_reply` when a `TransactionEdit` route reply arrives.
117
32
    pub(super) fn deliver_transaction_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
118
32
        self.open_loaded(
119
32
            target,
120
32
            &TRANSACTION_EDIT,
121
32
            wire,
122
32
            "transaction",
123
29
            |app, loaded| {
124
29
                let form = Form::transaction_edit(loaded);
125
29
                app.overlays.push(Modal::Form(form));
126
29
                app.fetch_transaction_form_options();
127
29
            },
128
        );
129
32
    }
130

            
131
    /// Called from `deliver_reply` when an `AccountEdit` route reply arrives.
132
11
    pub(super) fn deliver_account_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
133
11
        self.open_loaded(target, &ACCOUNT_EDIT, wire, "account", |app, loaded| {
134
10
            let form = Form::account_edit(loaded);
135
10
            app.overlays.push(Modal::Form(form));
136
10
            app.fetch_account_form_options();
137
10
        });
138
11
    }
139

            
140
    /// Called from `deliver_reply` when a `CommodityEdit` route reply arrives.
141
6
    pub(super) fn deliver_commodity_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
142
6
        self.open_loaded(target, &COMMODITY_EDIT, wire, "commodity", |app, loaded| {
143
5
            let form = Form::commodity_edit(loaded);
144
5
            app.overlays.push(Modal::Form(form));
145
5
        });
146
6
    }
147

            
148
    /// Called from `deliver_reply` when a `TagEdit` route reply arrives.
149
8
    pub(super) fn deliver_tag_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
150
8
        self.open_loaded(target, &TAG_EDIT, wire, "tag", |app, loaded| {
151
7
            let form = Form::tag_edit(loaded);
152
7
            app.overlays.push(Modal::Form(form));
153
7
        });
154
8
    }
155

            
156
    /// Called when a `TemplateRoute::Edit` reply arrives.
157
8
    pub(super) fn deliver_template_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
158
8
        self.open_loaded(target, &TEMPLATE_EDIT, wire, "template", |app, loaded| {
159
7
            let form = Form::template_edit(loaded);
160
7
            app.overlays.push(Modal::Form(form));
161
7
        });
162
8
    }
163

            
164
    /// Called when a `ScriptRoute::Edit` reply arrives; the server's notice rides on the form.
165
5
    pub(super) fn deliver_script_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
166
5
        let notices = rpc::parse_notices(wire);
167
5
        self.open_loaded(target, &SCRIPT_EDIT, wire, "script", |app, loaded| {
168
4
            let mut form = Form::script_edit(loaded);
169
4
            form.notices = notices;
170
4
            app.overlays.push(Modal::Form(form));
171
4
        });
172
5
    }
173

            
174
70
    fn open_loaded(
175
70
        &mut self,
176
70
        target: Option<WindowId>,
177
70
        descriptor: &forms::Form,
178
70
        wire: &str,
179
70
        what: &str,
180
70
        open: impl FnOnce(&mut Self, &FormValues),
181
70
    ) {
182
70
        match parse_loaded_reply(descriptor, wire) {
183
63
            Ok(loaded) if self.dialog_free("edit") => {
184
62
                if self.begin_dialog_of(target) {
185
62
                    open(self, &loaded);
186
                }
187
            }
188
1
            Ok(_) => {}
189
7
            Err(reason) => self.status = format!("can't edit this {what}: {reason}"),
190
        }
191
70
    }
192
}