changed dropdown to read-only textfield when only 1 associated child and fixed redirects after manual entries
This commit is contained in:
@@ -196,6 +196,16 @@
|
||||
window.location.href = "/login.html";
|
||||
}
|
||||
|
||||
// Determine return URL based on referrer
|
||||
function getReturnUrl() {
|
||||
const referrer = document.referrer;
|
||||
if (referrer.includes('/sleep.html')) {
|
||||
return '/sleep.html';
|
||||
}
|
||||
// Default to home page
|
||||
return '/';
|
||||
}
|
||||
|
||||
const form = document.getElementById("logSleepForm");
|
||||
const messageDiv = document.getElementById("message");
|
||||
const submitBtn = document.getElementById("submitBtn");
|
||||
@@ -249,13 +259,32 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Populate select dropdown
|
||||
children.forEach((child) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = child.id;
|
||||
option.textContent = child.name;
|
||||
childSelect.appendChild(option);
|
||||
});
|
||||
// Populate select dropdown or show single child
|
||||
if (children.length === 1) {
|
||||
// Single child: replace dropdown with text display
|
||||
const child = children[0];
|
||||
const formGroup = childSelect.parentElement;
|
||||
formGroup.innerHTML = `
|
||||
<label for="childName">Child</label>
|
||||
<input
|
||||
type="text"
|
||||
id="childName"
|
||||
value="${child.name}"
|
||||
readonly
|
||||
style="background-color: #f5f5f5; cursor: default;"
|
||||
/>
|
||||
<input type="hidden" id="childId" name="childId" value="${child.id}" />
|
||||
`;
|
||||
} else {
|
||||
// Multiple children: show dropdown (clear placeholder first)
|
||||
childSelect.innerHTML = '';
|
||||
children.forEach((child) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = child.id;
|
||||
option.textContent = child.name;
|
||||
childSelect.appendChild(option);
|
||||
});
|
||||
}
|
||||
|
||||
// Show form, hide loading
|
||||
loadingDiv.style.display = "none";
|
||||
@@ -353,9 +382,9 @@
|
||||
: "Sleep logged successfully!",
|
||||
"success",
|
||||
);
|
||||
// Redirect to sleep page after 1 second
|
||||
// Redirect to appropriate page after 1 second
|
||||
setTimeout(() => {
|
||||
window.location.href = "/sleep.html";
|
||||
window.location.href = getReturnUrl();
|
||||
}, 1000);
|
||||
} else {
|
||||
showMessage(
|
||||
@@ -378,7 +407,7 @@
|
||||
}
|
||||
|
||||
function goBack() {
|
||||
window.location.href = "/sleep.html";
|
||||
window.location.href = getReturnUrl();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user