Files
baby-monitor/src/baby_monitor/static/log-diaper.html
T
Brian Bjarke Jensen ec0e1f6140
Build and Push Docker Image / build-and-push (pull_request) Successful in 1m3s
Python Code Quality / python-code-quality (pull_request) Successful in 10s
Python Test / python-test (pull_request) Successful in 17s
code quality changes
2026-01-27 21:21:55 +01:00

532 lines
16 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Log Diaper Change - Baby Monitor</title>
<link rel="stylesheet" href="/dark-mode.css" />
<script src="/theme-init.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
Cantarell, sans-serif;
background-color: var(--bg-primary);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: var(--bg-secondary);
border-radius: 12px;
box-shadow: var(--shadow-md);
padding: 40px;
max-width: 500px;
width: 100%;
}
h1 {
color: var(--text-primary);
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: var(--text-secondary);
margin-bottom: 30px;
font-size: 14px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: var(--text-primary);
font-weight: 600;
font-size: 14px;
}
input,
select {
width: 100%;
padding: 12px;
border: 2px solid var(--border-color, #ddd);
border-radius: 6px;
font-size: 14px;
transition: border-color 0.2s;
background-color: var(--input-bg);
color: var(--input-text);
}
input:focus,
select:focus {
outline: none;
border-color: #f093fb;
}
input[readonly] {
opacity: 0.6;
cursor: default;
}
.button {
background: var(--gradient-pink);
background-color: #f093fb;
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition:
transform 0.2s,
box-shadow 0.2s;
width: 100%;
margin-top: 10px;
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(240, 147, 251, 0.4);
}
.button:active {
transform: translateY(0);
}
.button:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.button.secondary {
background: #6c757d;
margin-top: 10px;
}
.button.secondary:hover {
box-shadow: 0 4px 12px rgba(108, 117, 125, 0.4);
}
.message {
padding: 12px;
border-radius: 6px;
margin-bottom: 20px;
display: none;
}
.message.show {
display: block;
}
.message.error {
background: #ffebee;
border-left: 4px solid #f44336;
color: #c62828;
}
.message.success {
background: #e8f5e9;
border-left: 4px solid #4caf50;
color: #2e7d32;
}
.help-text {
font-size: 12px;
color: var(--text-secondary);
margin-top: 4px;
}
.loading {
text-align: center;
padding: 20px;
color: var(--text-secondary);
}
.section-title {
color: var(--text-secondary);
font-size: 13px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
margin: 25px 0 15px 0;
padding-top: 15px;
border-top: 1px solid var(--border-lighter);
}
.section-title:first-of-type {
border-top: none;
margin-top: 0;
}
</style>
</head>
<body>
<div class="container">
<h1 id="pageTitle">🧷 Log Diaper Change</h1>
<p class="subtitle">Record a diaper change</p>
<div id="message" class="message"></div>
<form id="diaperForm">
<div class="form-group">
<label for="childSelect">Child *</label>
<select id="childSelect" required>
<option value="">Select a child</option>
</select>
</div>
<div class="form-group">
<label for="changeTime">Change Time *</label>
<input type="datetime-local" id="changeTime" required />
</div>
<div class="section-title">💩 Poop Information (Optional)</div>
<div class="form-group">
<label for="poopAmount">Poop Amount</label>
<select id="poopAmount">
<option value="">None</option>
<option value="light">Light</option>
<option value="medium">Medium</option>
<option value="heavy">Heavy</option>
</select>
</div>
<div class="form-group">
<label for="poopColor">Poop Color</label>
<select id="poopColor">
<option value="">None</option>
<option value="black">Black</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="brown">Brown</option>
</select>
</div>
<div class="section-title">💧 Pee Information (Optional)</div>
<div class="form-group">
<label for="peeAmount">Pee Amount</label>
<select id="peeAmount">
<option value="">None</option>
<option value="light">Light</option>
<option value="medium">Medium</option>
<option value="heavy">Heavy</option>
</select>
</div>
<div class="form-group">
<label for="peeColor">Pee Color</label>
<select id="peeColor">
<option value="">None</option>
<option value="clear">Clear</option>
<option value="yellow">Yellow</option>
<option value="red">Red</option>
</select>
</div>
<button type="submit" class="button" id="submitBtn">
Log Diaper Change
</button>
<button
type="button"
class="button secondary"
onclick="window.location.href = getReturnUrl()"
>
Cancel
</button>
</form>
</div>
<script>
// Check if user is authenticated
const token = localStorage.getItem("access_token");
if (!token) {
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");
const isEditMode = !!diaperChangeId;
// Update UI for edit mode
if (isEditMode) {
document.getElementById("pageTitle").textContent =
"🧷 Edit Diaper Change";
document.getElementById("submitBtn").textContent =
"Update Diaper Change";
}
let children = [];
// Load children when page loads
async function loadChildren() {
try {
const response = await fetch("/api/children", {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (response.ok) {
children = await response.json();
const select = document.getElementById("childSelect");
// 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
/>
<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) {
await loadDiaperChangeData();
} else {
// Set default time to now for new entries
const now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById("changeTime").value = now
.toISOString()
.slice(0, 16);
// 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;
}
}
// Prepopulate poop and pee fields from most recent entry
try {
const response = await fetch("/api/diaper-changes", {
headers: { Authorization: `Bearer ${token}` },
});
if (response.ok) {
const changes = await response.json();
if (changes.length > 0) {
const last = changes[0];
document.getElementById("poopAmount").value =
last.poop_amount || "";
document.getElementById("poopColor").value =
last.poop_color || "";
document.getElementById("peeAmount").value =
last.pee_amount || "";
document.getElementById("peeColor").value =
last.pee_color || "";
}
}
} catch (error) {
// Ignore error, just use default
}
}
} else {
showMessage("Failed to load children", "error");
}
} catch (error) {
console.error("Error:", error);
showMessage("Network error. Please try again.", "error");
}
}
// Load diaper change data for editing
async function loadDiaperChangeData() {
try {
const response = await fetch(
`/api/diaper-changes/${diaperChangeId}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
if (response.ok) {
const diaperChange = await response.json();
// Populate form fields
document.getElementById("childSelect").value =
diaperChange.child_id;
// Convert UTC time to local datetime-local format
const changeTime = new Date(diaperChange.change_time);
changeTime.setMinutes(
changeTime.getMinutes() - changeTime.getTimezoneOffset(),
);
document.getElementById("changeTime").value = changeTime
.toISOString()
.slice(0, 16);
document.getElementById("poopAmount").value =
diaperChange.poop_amount || "";
document.getElementById("poopColor").value =
diaperChange.poop_color || "";
document.getElementById("peeAmount").value =
diaperChange.pee_amount || "";
document.getElementById("peeColor").value =
diaperChange.pee_color || "";
} else {
showMessage("Failed to load diaper change data", "error");
}
} catch (error) {
console.error("Error:", error);
showMessage("Network error. Please try again.", "error");
}
}
// Handle form submission
document
.getElementById("diaperForm")
.addEventListener("submit", async (e) => {
e.preventDefault();
const childId = parseInt(
document.getElementById("childSelect").value,
);
const changeTime = new Date(
document.getElementById("changeTime").value,
).toISOString();
const poopAmount =
document.getElementById("poopAmount").value || null;
const poopColor = document.getElementById("poopColor").value || null;
const peeAmount = document.getElementById("peeAmount").value || null;
const peeColor = document.getElementById("peeColor").value || null;
const submitBtn = document.getElementById("submitBtn");
submitBtn.disabled = true;
submitBtn.textContent = isEditMode ? "Updating..." : "Logging...";
try {
const url = isEditMode
? `/api/diaper-changes/${diaperChangeId}`
: "/api/diaper-changes";
const method = isEditMode ? "PUT" : "POST";
const body = isEditMode
? {
change_time: changeTime,
poop_amount: poopAmount,
poop_color: poopColor,
pee_amount: peeAmount,
pee_color: peeColor,
}
: {
child_id: childId,
change_time: changeTime,
poop_amount: poopAmount,
poop_color: poopColor,
pee_amount: peeAmount,
pee_color: peeColor,
};
const response = await fetch(url, {
method: method,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(body),
});
if (response.ok) {
// Save last used child for next time (only for new entries, not edits)
if (!isEditMode) {
localStorage.setItem(
"lastDiaperChange",
JSON.stringify({
child_id: childId,
}),
);
}
showMessage(
isEditMode
? "Diaper change updated successfully!"
: "Diaper change logged successfully!",
"success",
);
setTimeout(() => {
window.location.href = getReturnUrl();
}, 1000);
} else {
const errorData = await response.json();
showMessage(
errorData.detail || "Failed to log diaper change",
"error",
);
submitBtn.disabled = false;
submitBtn.textContent = isEditMode
? "Update Diaper Change"
: "Log Diaper Change";
}
} catch (error) {
console.error("Error:", error);
showMessage("Network error. Please try again.", "error");
submitBtn.disabled = false;
submitBtn.textContent = isEditMode
? "Update Diaper Change"
: "Log Diaper Change";
}
});
function showMessage(text, type) {
const messageDiv = document.getElementById("message");
messageDiv.textContent = text;
messageDiv.className = `message ${type} show`;
}
// Initialize
loadChildren();
</script>
</body>
</html>