1
//! Pending-form lifecycle: a submitted form stays on screen, locked, until its
2
//! own request settles, and every outcome is terminal.
3

            
4
use cli_core::render::{FieldError, RenderError};
5

            
6
use crate::app::App;
7
use crate::form::Form;
8
use crate::modal::Modal;
9

            
10
/// A form that was dispatched and awaits the reply to `request_id`.
11
///
12
/// Input is swallowed while pending, `Esc` included: the mutation is already
13
/// in flight, so cancelling would hide an outcome the user must see.
14
#[derive(Debug)]
15
pub struct PendingForm {
16
    pub request_id: i64,
17
    pub form: Form,
18
}
19

            
20
impl App {
21
137
    pub(crate) fn lock_pending_form(&mut self, request_id: i64, form: Form) {
22
137
        self.pending_form = Some(PendingForm { request_id, form });
23
137
    }
24

            
25
    /// Whether no dialog is open or awaiting its reply; otherwise `what` is
26
    /// reported as dropped. Every late opener asks before it shows anything.
27
222
    pub(super) fn dialog_free(&mut self, what: &str) -> bool {
28
222
        let busy = self.pending_form.is_some() || !self.overlays.is_empty();
29
222
        if busy {
30
4
            self.set_status(format!("{what} dropped: another dialog is open"));
31
218
        }
32
222
        !busy
33
222
    }
34

            
35
    /// Takes the pending form only when `request_id` is its own request.
36
811
    pub(super) fn take_pending_form(&mut self, request_id: i64) -> Option<Form> {
37
811
        self.pending_form
38
85
            .take_if(|p| p.request_id == request_id)
39
81
            .map(|p| p.form)
40
811
    }
41

            
42
    /// Reopens `form` with its values intact, the rejection shown and the
43
    /// first rejected field focused.
44
39
    pub(super) fn unlock_pending_form(&mut self, mut form: Form, errors: Vec<FieldError>) {
45
39
        form.show_errors(&errors);
46
39
        if let Some(first) = form.errors.first() {
47
38
            self.status = format!("submit failed: {first}");
48
1
        }
49
39
        if form.is_stale(&errors) {
50
6
            self.status
51
6
                .push_str(" (close the form and open it again to reload)");
52
33
        }
53
39
        self.overlays.push(Modal::Form(form));
54
39
    }
55
}
56

            
57
7
fn form_level(message: &str) -> FieldError {
58
7
    FieldError {
59
7
        field: String::new(),
60
7
        message: message.to_string(),
61
7
    }
62
7
}
63

            
64
/// Field errors of a rejected submit; anything but `form-invalid` with a
65
/// readable detail is one form-level error.
66
41
pub(super) fn rejection(error: &RenderError) -> Vec<FieldError> {
67
41
    let RenderError::Server(reply) = error else {
68
        return vec![form_level(&error.to_string())];
69
    };
70
41
    match reply.field_errors() {
71
41
        fields if fields.is_empty() => vec![form_level(&reply.message)],
72
34
        fields => fields,
73
    }
74
41
}