Lines
97.44 %
Functions
56.25 %
Branches
100 %
//! Pending-form lifecycle: a submitted form stays on screen, locked, until its
//! own request settles, and every outcome is terminal.
use cli_core::render::{FieldError, RenderError};
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
/// A form that was dispatched and awaits the reply to `request_id`.
///
/// Input is swallowed while pending, `Esc` included: the mutation is already
/// in flight, so cancelling would hide an outcome the user must see.
#[derive(Debug)]
pub struct PendingForm {
pub request_id: i64,
pub form: Form,
}
impl App {
pub(crate) fn lock_pending_form(&mut self, request_id: i64, form: Form) {
self.pending_form = Some(PendingForm { request_id, form });
/// Whether no dialog is open or awaiting its reply; otherwise `what` is
/// reported as dropped. Every late opener asks before it shows anything.
pub(super) fn dialog_free(&mut self, what: &str) -> bool {
let busy = self.pending_form.is_some() || !self.overlays.is_empty();
if busy {
self.set_status(format!("{what} dropped: another dialog is open"));
!busy
/// Takes the pending form only when `request_id` is its own request.
pub(super) fn take_pending_form(&mut self, request_id: i64) -> Option<Form> {
self.pending_form
.take_if(|p| p.request_id == request_id)
.map(|p| p.form)
/// Reopens `form` with its values intact, the rejection shown and the
/// first rejected field focused.
pub(super) fn unlock_pending_form(&mut self, mut form: Form, errors: Vec<FieldError>) {
form.show_errors(&errors);
if let Some(first) = form.errors.first() {
self.status = format!("submit failed: {first}");
if form.is_stale(&errors) {
self.status
.push_str(" (close the form and open it again to reload)");
self.overlays.push(Modal::Form(form));
fn form_level(message: &str) -> FieldError {
FieldError {
field: String::new(),
message: message.to_string(),
/// Field errors of a rejected submit; anything but `form-invalid` with a
/// readable detail is one form-level error.
pub(super) fn rejection(error: &RenderError) -> Vec<FieldError> {
let RenderError::Server(reply) = error else {
return vec![form_level(&error.to_string())];
};
match reply.field_errors() {
fields if fields.is_empty() => vec![form_level(&reply.message)],
fields => fields,