feat(#2443): asyncBtn — shared reusable component, replace ad-hoc disable/spinner patterns
add `asyncBtn(btn, fn)` to `@hive/shared/forms.js` as the single reusable component for async button actions: 1. double-click guard: returns immediately if btn is already disabled 2. saves btn.innerHTML, replaces with spinner while in-flight 3. restores btn on resolve or reject via finally wire it into all ad-hoc disable/spinner/restore patterns: - common.js: bindAsyncForms uses asyncBtn internally - core.js: 'clear perms' button - permissions.js: clearStaleAgent - schedules.js: saveSchedule submit, editSchedule submit - app.js: buildAnswerForm, buildInboxMarkAllRow fireScheduleNow in schedules.js is left with its existing childNode save/restore because it shows a custom result flash on the button content after a successful fire-now (the auto-restore of asyncBtn would overwrite it); the surrounding themedConfirm dialog already acts as a natural double-click barrier before the fetch. saveAll in permissions.js is also left as-is: it uses a custom 'queued ✓' success label + a 900ms delay before re-fetch; the btn.dataset.busy flag is its own double-submit guard.
This commit is contained in:
parent
6d281e4606
commit
4b45c5cd3d
7 changed files with 143 additions and 119 deletions
|
|
@ -23,6 +23,7 @@
|
|||
|
||||
import { $, el } from './common.js';
|
||||
import { containersState } from './state.js';
|
||||
import { asyncBtn } from '@hive/shared/forms.js';
|
||||
|
||||
// ── SSE re-render guards ────────────────────────────────────────────
|
||||
// Skip the live re-render when the operator has unsaved edits in that
|
||||
|
|
@ -325,27 +326,28 @@ function updateSaveBar() {
|
|||
// agent isn't in the live container list. Re-fetches both tables after
|
||||
// the delete so the row disappears immediately.
|
||||
async function clearStaleAgent(name, sectionRoot) {
|
||||
// Disable the row's remove button while the request is in flight to
|
||||
// prevent a double-submit.
|
||||
const btn = sectionRoot
|
||||
? sectionRoot.querySelector(`[data-agent="${CSS.escape(name)}"] .perm-remove-btn`)
|
||||
: null;
|
||||
if (btn) btn.disabled = true;
|
||||
try {
|
||||
const resp = await fetch('/api/permissions/' + encodeURIComponent(name), { method: 'DELETE' });
|
||||
if (!resp.ok) {
|
||||
const text = await resp.text().catch(() => resp.status);
|
||||
setSaveNote('failed to remove ' + name + ': ' + text, true);
|
||||
if (btn) btn.disabled = false;
|
||||
const doDelete = async () => {
|
||||
try {
|
||||
const resp = await fetch('/api/permissions/' + encodeURIComponent(name), { method: 'DELETE' });
|
||||
if (!resp.ok) {
|
||||
const text = await resp.text().catch(() => resp.status);
|
||||
setSaveNote('failed to remove ' + name + ': ' + text, true);
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
setSaveNote('failed to remove ' + name + ': ' + err, true);
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
setSaveNote('failed to remove ' + name + ': ' + err, true);
|
||||
if (btn) btn.disabled = false;
|
||||
return;
|
||||
}
|
||||
// Re-fetch both sections so the stale row disappears.
|
||||
await Promise.all([fetchAndRenderCapabilities(), fetchAndRenderToolGroups()]);
|
||||
// Re-fetch both sections so the stale row disappears.
|
||||
await Promise.all([fetchAndRenderCapabilities(), fetchAndRenderToolGroups()]);
|
||||
};
|
||||
// asyncBtn guards double-submit; fall through without guard when there
|
||||
// is no button (e.g. called programmatically without a DOM context).
|
||||
if (btn) asyncBtn(btn, doDelete);
|
||||
else await doDelete();
|
||||
}
|
||||
|
||||
function clearSaveStatus() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue