diff --git a/app/views/qbo_estimates/_form.html.erb b/app/views/qbo_estimates/_form.html.erb index 06233d4..9c268cb 100644 --- a/app/views/qbo_estimates/_form.html.erb +++ b/app/views/qbo_estimates/_form.html.erb @@ -298,10 +298,12 @@ <%# Action bar, like the bar along the bottom of QuickBooks' estimate screen %> -
+
<%= link_to l(:button_cancel), cancel_path, class: 'qbo-button qbo-button-dark', id: 'qbo-estimate-cancel' %> <% unless form.locked %> + <%# Shown while a save is on its way to QuickBooks (qbo_estimates/_script) %> + <% unless form.persisted? %> <%# Kept in Redmine only, however incomplete (QboDraftHandling) %> <% if draft %> @@ -309,12 +311,13 @@ data: { confirm: l(:text_are_you_sure) }, class: 'qbo-button qbo-button-dark', id: 'qbo-delete-draft' %> <% end %> <%= submit_tag l(:button_qbo_save_draft), name: 'save_draft', class: 'qbo-button qbo-button-dark', - id: 'qbo-save-draft', formnovalidate: true %> + id: 'qbo-save-draft', formnovalidate: true, data: { saving_text: l(:text_qbo_saving_draft) } %> <% end %> <%= submit_tag l(:button_save), name: 'save', class: 'qbo-button qbo-button-dark' %> <% if sender %> <%= submit_tag l(:button_save_and_send), name: 'save_and_send', class: 'qbo-button qbo-button-primary', - id: 'qbo-estimate-save-and-send', data: { needs_email_text: l(:text_estimate_send_needs_email) } %> + id: 'qbo-estimate-save-and-send', + data: { needs_email_text: l(:text_estimate_send_needs_email), saving_text: l(:text_qbo_saving_and_sending) } %> <% end %> <% end %> diff --git a/app/views/qbo_estimates/_script.html.erb b/app/views/qbo_estimates/_script.html.erb index 1f19bf8..670e70e 100644 --- a/app/views/qbo_estimates/_script.html.erb +++ b/app/views/qbo_estimates/_script.html.erb @@ -773,7 +773,11 @@ $(function () { if (event.key === 'Enter' && !$(this).closest('.ui-autocomplete').length) event.preventDefault(); }); + // Set once a save is on its way, so more clicks don't save (or send) it twice + var saving = false; + $form.on('submit', function (event) { + if (saving) { event.preventDefault(); return; } var submitter = event.originalEvent && event.originalEvent.submitter; if (submitter && submitter.id === 'qbo-estimate-save-and-send' && !$email.val().trim()) { event.preventDefault(); @@ -785,6 +789,37 @@ $(function () { rows().each(function () { tidyNumbers($(this)); }); tidyDiscount(); dirty = false; + + // After every submit handler has run (and the form's values are already taken, so disabling the buttons + // doesn't drop which one was clicked): unless one stopped the save, show it's saving and lock the buttons + setTimeout(function () { + var stopped = event.originalEvent ? event.originalEvent.defaultPrevented : event.isDefaultPrevented(); + if (stopped) return; + saving = true; + showSaving(submitter); + }, 0); + }); + + function showSaving(submitter) { + var $bar = $form.find('.qbo-estimate-actions'); + var $clicked = submitter ? $(submitter) : $bar.find('input[type=submit][name=save]'); + var text = $clicked.data('savingText') || $bar.data('savingText'); + + $bar.addClass('qbo-saving'); + $bar.find('input[type=submit]').prop('disabled', true); + $bar.find('a.qbo-button').addClass('qbo-button-disabled').attr('aria-disabled', 'true'); + $bar.find('.qbo-saving-status').text(text).prop('hidden', false); + } + + // Coming back with the browser's Back button can show the page as it was left, still "saving" + window.addEventListener('pageshow', function (event) { + if (!event.persisted || !saving) return; + saving = false; + var $bar = $form.find('.qbo-estimate-actions'); + $bar.removeClass('qbo-saving'); + $bar.find('input[type=submit]').prop('disabled', false); + $bar.find('a.qbo-button').removeClass('qbo-button-disabled').removeAttr('aria-disabled'); + $bar.find('.qbo-saving-status').prop('hidden', true); }); window.addEventListener('beforeunload', function (event) { diff --git a/app/views/qbo_estimates/_styles.html.erb b/app/views/qbo_estimates/_styles.html.erb index c761618..2e9b94d 100644 --- a/app/views/qbo_estimates/_styles.html.erb +++ b/app/views/qbo_estimates/_styles.html.erb @@ -119,6 +119,17 @@ .qbo-estimate .qbo-button-primary { background: var(--qbo-green); border-color: var(--qbo-green); color: #fff; } .qbo-estimate .qbo-button-primary:hover { background: var(--qbo-green-dark); border-color: var(--qbo-green-dark); color: #fff; } + /* While saving: the buttons are locked and a spinner says what's happening */ + .qbo-estimate .qbo-saving .qbo-button[disabled], + .qbo-estimate .qbo-saving .qbo-button-disabled { opacity: .6; cursor: progress; pointer-events: none; } + .qbo-estimate .qbo-saving-status { display: inline-flex; align-items: center; gap: 8px; color: #fff; font-weight: 600; margin-right: 8px; } + .qbo-estimate .qbo-saving-status[hidden] { display: none; } + .qbo-estimate .qbo-saving-status::before { content: ""; width: 16px; height: 16px; border-radius: 50%; box-sizing: border-box; + border: 2px solid rgba(255,255,255,.35); border-top-color: #fff; animation: qbo-saving-spin .8s linear infinite; } + @keyframes qbo-saving-spin { to { transform: rotate(360deg); } } + @media (prefers-reduced-motion: reduce) { .qbo-estimate .qbo-saving-status::before { animation-duration: 2.4s; } } + .qbo-estimate .qbo-saving { cursor: progress; } + /* Warnings with a list (linked issues, invoices): the text, then one item per line */ div.warning.qbo-estimate-notice { text-align: left; } div.warning.qbo-estimate-notice ul { margin: 6px 0 0; padding-left: 22px; } diff --git a/config/locales/en.yml b/config/locales/en.yml index af433a0..c6b6ec6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -161,6 +161,10 @@ en: text_qbo_hidden_custom_fields: "Ticked fields are left off estimates and invoices. QuickBooks doesn't tell apps which custom fields are inactive, and a field stays on its old estimates after it's turned off, so tick ones you no longer use (e.g. a renamed \"Sales Rep old\"). The sales person goes in the first field shown whose name has \"sales\" or \"rep\" in it." button_reload_estimate: "Reload the estimate" button_save_and_send: "Save and send" + # Shown in the action bar while a save is on its way, with the buttons locked + text_qbo_saving: "Saving to QuickBooks…" + text_qbo_saving_and_sending: "Saving and sending…" + text_qbo_saving_draft: "Saving draft…" error_estimate_changed_in_quickbooks: "This estimate was changed in QuickBooks after you opened it, so it wasn't saved." error_estimate_field_invalid: "%{field} is not valid"