changed dropdown to read-only textfield when only 1 associated child and fixed redirects after manual entries
This commit is contained in:
@@ -235,7 +235,7 @@
|
||||
<button
|
||||
type="button"
|
||||
class="button secondary"
|
||||
onclick="window.location.href='/diapers.html'"
|
||||
onclick="window.location.href=getReturnUrl()"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
@@ -249,6 +249,16 @@
|
||||
window.location.href = "/login.html";
|
||||
}
|
||||
|
||||
// Determine return URL based on referrer
|
||||
function getReturnUrl() {
|
||||
const referrer = document.referrer;
|
||||
if (referrer.includes('/diapers.html')) {
|
||||
return '/diapers.html';
|
||||
}
|
||||
// Default to home page
|
||||
return '/';
|
||||
}
|
||||
|
||||
// Check if we're in edit mode
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const diaperChangeId = urlParams.get("id");
|
||||
@@ -277,12 +287,32 @@
|
||||
children = await response.json();
|
||||
const select = document.getElementById("childSelect");
|
||||
|
||||
children.forEach((child) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = child.id;
|
||||
option.textContent = child.name;
|
||||
select.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 = select.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="childSelect" name="childSelect" value="${child.id}" />
|
||||
`;
|
||||
} else {
|
||||
// Multiple children: show dropdown (clear the placeholder first)
|
||||
select.innerHTML = '';
|
||||
children.forEach((child) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = child.id;
|
||||
option.textContent = child.name;
|
||||
select.appendChild(option);
|
||||
});
|
||||
}
|
||||
|
||||
// If editing, load the diaper change data
|
||||
if (isEditMode) {
|
||||
@@ -295,14 +325,16 @@
|
||||
.toISOString()
|
||||
.slice(0, 16);
|
||||
|
||||
// Load last used child from localStorage
|
||||
const lastDiaperChange = JSON.parse(
|
||||
localStorage.getItem("lastDiaperChange") || "{}",
|
||||
);
|
||||
// Load last used child from localStorage (only for multiple children)
|
||||
if (children.length > 1) {
|
||||
const lastDiaperChange = JSON.parse(
|
||||
localStorage.getItem("lastDiaperChange") || "{}",
|
||||
);
|
||||
|
||||
if (lastDiaperChange.child_id) {
|
||||
document.getElementById("childSelect").value =
|
||||
lastDiaperChange.child_id;
|
||||
if (lastDiaperChange.child_id) {
|
||||
document.getElementById("childSelect").value =
|
||||
lastDiaperChange.child_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -431,7 +463,7 @@
|
||||
"success",
|
||||
);
|
||||
setTimeout(() => {
|
||||
window.location.href = "/diapers.html";
|
||||
window.location.href = getReturnUrl();
|
||||
}, 1000);
|
||||
} else {
|
||||
const errorData = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user