refactor/add headers to client side scripts
This commit is contained in:
9 files changed
+183
-155
No files matched your search
+1
-67
@@ -55,73 +55,7 @@
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
// Basic client-side form submission handler to process JSON response
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('editPlanForm');
|
||||
const errorMessagesDiv = document.getElementById('formErrorMessages');
|
||||
|
||||
// Store initial form values
|
||||
const initialValues = {
|
||||
name: form.name.value,
|
||||
retirementAge: form.retirementAge.value,
|
||||
retirementExpenses: form.retirementExpenses.value,
|
||||
retirementAssets: form.retirementAssets.value,
|
||||
retirementLiabilities: form.retirementLiabilities.value
|
||||
};
|
||||
|
||||
const resetButton = document.getElementById('resetButton');
|
||||
resetButton.addEventListener('click', function() {
|
||||
form.name.value = initialValues.name;
|
||||
form.retirementAge.value = initialValues.retirementAge;
|
||||
form.retirementExpenses.value = initialValues.retirementExpenses;
|
||||
form.retirementAssets.value = initialValues.retirementAssets;
|
||||
form.retirementLiabilities.value = initialValues.retirementLiabilities;
|
||||
if (errorMessagesDiv) {
|
||||
errorMessagesDiv.style.display = 'none'; // Hide error message on reset
|
||||
errorMessagesDiv.textContent = '';
|
||||
}
|
||||
});
|
||||
|
||||
if (form) {
|
||||
form.addEventListener('submit', async function(event) {
|
||||
event.preventDefault(); // Prevent traditional form submission
|
||||
errorMessagesDiv.textContent = '';
|
||||
errorMessagesDiv.style.display = 'none';
|
||||
|
||||
const formData = new FormData(form);
|
||||
const data = {};
|
||||
formData.forEach((value, key) => {
|
||||
data[key] = value;
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (response.ok && result.success) {
|
||||
// alert('Plan updated successfully!'); // Optional: show an alert
|
||||
window.location.href = `/plans/<%= plan._id %>`; // Redirect to plan detail page
|
||||
} else {
|
||||
errorMessagesDiv.textContent = result.message || 'An error occurred while updating the plan.';
|
||||
errorMessagesDiv.style.display = 'block';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error submitting form:', error);
|
||||
errorMessagesDiv.textContent = 'A network error occurred. Please try again.';
|
||||
errorMessagesDiv.style.display = 'block';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script src="/static/scripts/editPlanModals.js"></script>
|
||||
|
||||
<%- include('./partials/navBar') %>
|
||||
<%- include('./partials/scriptLoader') %>
|
||||
|
||||
Reference in new issue
Block a user