Skip to content

Commit b9c303e

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
improvements to button state
1 parent dc915c4 commit b9c303e

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

internal/server/static/index.html

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,35 @@ <h3>Why calculate PR costs?</h3>
20212021
return output;
20222022
}
20232023

2024+
// Track form state to grey out button after successful calculation
2025+
let formModifiedSinceLastCalculation = true;
2026+
let lastCalculationSuccessful = false;
2027+
2028+
function markFormAsModified() {
2029+
formModifiedSinceLastCalculation = true;
2030+
const submitBtn = document.getElementById('submitBtn');
2031+
if (lastCalculationSuccessful) {
2032+
submitBtn.disabled = false;
2033+
submitBtn.textContent = 'Calculate Cost';
2034+
}
2035+
}
2036+
2037+
function markCalculationComplete(success) {
2038+
lastCalculationSuccessful = success;
2039+
formModifiedSinceLastCalculation = false;
2040+
const submitBtn = document.getElementById('submitBtn');
2041+
if (success) {
2042+
submitBtn.disabled = true;
2043+
submitBtn.textContent = 'Costs calculated, scroll down...';
2044+
}
2045+
}
2046+
2047+
// Add change listeners to all form inputs
2048+
document.querySelectorAll('#costForm input, #costForm select').forEach(input => {
2049+
input.addEventListener('change', markFormAsModified);
2050+
input.addEventListener('input', markFormAsModified);
2051+
});
2052+
20242053
document.getElementById('costForm').addEventListener('submit', async (e) => {
20252054
e.preventDefault();
20262055

@@ -2160,14 +2189,23 @@ <h3>Why calculate PR costs?</h3>
21602189
html += '</div>';
21612190

21622191
resultDiv.innerHTML = html;
2192+
markCalculationComplete(true);
21632193
}
21642194

21652195
} catch (error) {
21662196
resultDiv.innerHTML = `<div class="error">Error: ${error.message}</div>`;
2197+
markCalculationComplete(false);
21672198
} finally {
21682199
submitBtn.classList.remove('calculating');
2169-
submitBtn.disabled = false;
2170-
submitBtn.textContent = 'Calculate Cost';
2200+
// Only re-enable button if form was modified or calculation failed
2201+
if (!lastCalculationSuccessful || formModifiedSinceLastCalculation) {
2202+
submitBtn.disabled = false;
2203+
submitBtn.textContent = 'Calculate Cost';
2204+
} else {
2205+
// Calculation succeeded and form unchanged - keep button disabled
2206+
submitBtn.disabled = true;
2207+
submitBtn.textContent = 'Costs calculated, scroll down...';
2208+
}
21712209
}
21722210
});
21732211

@@ -2404,6 +2442,7 @@ <h3>Why calculate PR costs?</h3>
24042442
html += '</div>';
24052443

24062444
resultDiv.innerHTML = html;
2445+
markCalculationComplete(true);
24072446
resolve();
24082447
return;
24092448
}

0 commit comments

Comments
 (0)