Add deletion, update time, zero hour

This commit is contained in:
Will Bradley 2025-07-16 17:39:31 -07:00
parent 838e40b055
commit e5ba61c71c
2 changed files with 73 additions and 6 deletions

View File

@ -92,6 +92,7 @@
input[type="text"],
input[type="number"],
input[type="datetime-local"],
textarea,
select {
width: 100%;
@ -287,9 +288,14 @@
<textarea id="updateDescription" name="description" placeholder="e.g., Refilled and working well"></textarea>
</div>
<div class="form-group">
<label for="statusAsOf">Status as of</label>
<input type="datetime-local" id="statusAsOf" name="statusAsOf">
</div>
<div class="form-group">
<label for="estimatedHours">Estimated hours until empty</label>
<input type="number" id="estimatedHours" name="estimatedHours" min="1" max="48" value="6">
<input type="number" id="estimatedHours" name="estimatedHours" min="0" max="48" value="6">
</div>
<button type="submit" class="btn btn-primary">Update Status</button>
@ -343,6 +349,7 @@
<button type="submit" class="btn btn-primary">Save Changes</button>
<button type="button" class="btn btn-secondary" onclick="closeEditModal()">Cancel</button>
<button type="button" class="btn" onclick="deleteStation()" style="background: #dc3545; color: white; margin-top: 0.5rem; width: 100%;">Delete Station</button>
</form>
</div>
</div>
@ -552,6 +559,7 @@
window.closeAddModal = closeAddModal;
window.closeEditModal = closeEditModal;
window.openEditModal = openEditModal;
window.deleteStation = deleteStation;
async function loadStations() {
try {
@ -644,6 +652,11 @@
selectedStation = station;
document.getElementById('updateModal').style.display = 'block';
// Set current date/time as default
const now = new Date();
const localDateTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString().slice(0, 16);
document.getElementById('statusAsOf').value = localDateTime;
// Center map on selected station
map.setView([station.latitude, station.longitude], 16);
}
@ -698,6 +711,34 @@
document.getElementById('edit-message').innerHTML = '';
}
function deleteStation() {
if (!selectedStation) {
showMessage('edit-message', 'No station selected', 'error');
return;
}
if (confirm(`Are you sure you want to delete "${selectedStation.name}"? This action cannot be undone.`)) {
fetch(`/api/stations/${selectedStation.id}`, {
method: 'DELETE'
})
.then(response => response.json())
.then(result => {
if (result.success) {
showMessage('edit-message', 'Station deleted successfully!');
setTimeout(() => {
closeEditModal();
loadStations();
}, 1500);
} else {
showMessage('edit-message', result.error || 'Failed to delete station', 'error');
}
})
.catch(error => {
showMessage('edit-message', 'Failed to delete station', 'error');
});
}
}
// Close modal when clicking outside
window.onclick = function(event) {
const updateModal = document.getElementById('updateModal');
@ -803,7 +844,8 @@
const formData = new FormData(e.target);
const data = {
description: formData.get('description'),
estimatedHours: formData.get('estimatedHours')
estimatedHours: formData.get('estimatedHours'),
statusAsOf: formData.get('statusAsOf')
};
try {

View File

@ -396,15 +396,18 @@ app.post('/api/stations/:id/update', (req, res) => {
return res.status(401).json({ error: 'Authentication required' });
}
const { description, estimatedHours } = req.body;
const { description, estimatedHours, statusAsOf } = req.body;
const stationId = req.params.id;
const estimatedEmptyTime = new Date();
// Use provided status date/time or current time
const refillTime = statusAsOf ? new Date(statusAsOf) : new Date();
const estimatedEmptyTime = new Date(refillTime);
estimatedEmptyTime.setHours(estimatedEmptyTime.getHours() + parseInt(estimatedHours));
console.log("Updated by",req.user);
db.run('INSERT INTO station_updates (station_id, description, estimated_empty_time, updated_by) VALUES (?, ?, ?, ?)',
[stationId, description, estimatedEmptyTime.toISOString(), req.user.id],
db.run('INSERT INTO station_updates (station_id, description, last_refill_time, estimated_empty_time, updated_by) VALUES (?, ?, ?, ?, ?)',
[stationId, description, refillTime.toISOString(), estimatedEmptyTime.toISOString(), req.user.id],
function(err) {
if (err) return res.status(500).json({ error: 'Database error' });
res.json({ success: true });
@ -429,6 +432,28 @@ app.put('/api/stations/:id', (req, res) => {
);
});
app.delete('/api/stations/:id', (req, res) => {
if (!req.user) {
return res.status(401).json({ error: 'Authentication required' });
}
const stationId = req.params.id;
// Delete station updates first (foreign key constraint)
db.run('DELETE FROM station_updates WHERE station_id = ?', [stationId], function(err) {
if (err) return res.status(500).json({ error: 'Database error' });
// Then delete the station
db.run('DELETE FROM water_stations WHERE id = ?', [stationId], function(err) {
if (err) return res.status(500).json({ error: 'Database error' });
if (this.changes === 0) {
return res.status(404).json({ error: 'Station not found' });
}
res.json({ success: true });
});
});
});
app.get('/dashboard', (req, res) => {
if (!req.user) {
return res.redirect('/login');