added functionality to log diaper changes
This commit is contained in:
@@ -431,6 +431,9 @@
|
||||
<p><strong>Logged in as:</strong> ${username}</p>
|
||||
</div>
|
||||
${feedingButtonHtml}
|
||||
<button class="feeding-button" onclick="window.location.href='/log-diaper.html'" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); margin-top: 20px;">
|
||||
🧷 Log Diaper Change
|
||||
</button>
|
||||
<p>Your session is active.</p>
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,454 @@
|
||||
<!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>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family:
|
||||
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
|
||||
Cantarell, sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
|
||||
padding: 40px;
|
||||
max-width: 500px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
margin-bottom: 10px;
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #666;
|
||||
margin-bottom: 30px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 8px;
|
||||
color: #333;
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
input,
|
||||
select {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
input:focus,
|
||||
select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
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(102, 126, 234, 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: #666;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
color: #666;
|
||||
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 #eee;
|
||||
}
|
||||
|
||||
.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='/'">
|
||||
Cancel
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Check if user is authenticated
|
||||
const token = localStorage.getItem("access_token");
|
||||
if (!token) {
|
||||
window.location.href = "/login.html";
|
||||
}
|
||||
|
||||
// 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");
|
||||
|
||||
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
|
||||
const lastDiaperChange = JSON.parse(
|
||||
localStorage.getItem("lastDiaperChange") || "{}"
|
||||
);
|
||||
|
||||
if (lastDiaperChange.child_id) {
|
||||
document.getElementById("childSelect").value =
|
||||
lastDiaperChange.child_id;
|
||||
}
|
||||
}
|
||||
} 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 = "/";
|
||||
}, 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>
|
||||
Reference in New Issue
Block a user