Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions backend/src/controllers/pointController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ exports.addPoint = async (req, res) => {
try {
const { lat, lon, temp } = req.body;

if (!lat || !lon || !temp) {
return res.status(400).json({ message: 'Missing required fields' });
// only reject if any value is null or undefined
if (lat == null || lon == null || temp == null) {
return res
.status(400)
.json({ success: false, message: 'Latitude, longitude and temperature are required.' });
}

// Get logged-in user
Expand Down
63 changes: 60 additions & 3 deletions frontend/src/components/AddPoint.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,16 @@ export default function AddPoint() {

const handleSubmit = async (e) => {
e.preventDefault();
setSubmitting(true);
setError('');
setSuccess(false);
setSubmitting(true);

// only reject when fields are actually empty strings
if (lat === '' || lon === '' || temp === '') {
setError('Missing required fields');
setSubmitting(false);
return;
}

try {
const token = localStorage.getItem('authToken');
Expand Down Expand Up @@ -223,6 +230,30 @@ export default function AddPoint() {
setSuccess(true);
setTemp('');
setShowFullscreenConfirmation(true);

// Add new point to existing cache instead of clearing it
const newUserPoint = {
lat: parseFloat(lat),
lng: parseFloat(lon), // Note: using 'lng' to match existing format
temp: parseFloat(temp),
timestamp: mostRecentPoint.timestamp || new Date().toISOString(),
createdAt: mostRecentPoint.createdAt || new Date().toISOString(),
updatedAt: mostRecentPoint.updatedAt || new Date().toISOString(),
isUserPoint: true
};

// Update cache with new point
const existingCache = localStorage.getItem('waterData');
if (existingCache) {
const cachedData = JSON.parse(existingCache);
cachedData.push(newUserPoint);
localStorage.setItem('waterData', JSON.stringify(cachedData));
}

// Dispatch custom event to add single point to map and HUD
window.dispatchEvent(new CustomEvent('pointAdded', {
detail: newUserPoint
}));
} else {
setError('Point was submitted but could not be verified in database');
}
Expand All @@ -231,15 +262,41 @@ export default function AddPoint() {
}
} catch (verifyErr) {
// Point was added but verification failed - still show success
setAddedPoint({
const newPoint = {
lat: parseFloat(lat),
lon: parseFloat(lon),
temp: parseFloat(temp),
timestamp: new Date().toISOString()
});
};

setAddedPoint(newPoint);
setSuccess(true);
setTemp('');
setShowFullscreenConfirmation(true);

// Add new point to existing cache instead of clearing it
const newUserPoint = {
lat: newPoint.lat,
lng: newPoint.lon, // Note: using 'lng' to match existing format
temp: newPoint.temp,
timestamp: newPoint.timestamp,
createdAt: newPoint.timestamp,
updatedAt: newPoint.timestamp,
isUserPoint: true
};

// Update cache with new point
const existingCache = localStorage.getItem('waterData');
if (existingCache) {
const cachedData = JSON.parse(existingCache);
cachedData.push(newUserPoint);
localStorage.setItem('waterData', JSON.stringify(cachedData));
}

// Dispatch custom event to add single point to map and HUD
window.dispatchEvent(new CustomEvent('pointAdded', {
detail: newUserPoint
}));
}
} else {
const errorData = await res.json();
Expand Down
16 changes: 15 additions & 1 deletion frontend/src/components/HUDleftPoints.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,23 @@ function LogoBlock() {
checkForData();
};

// Listen for point added event to add single point instantly
const handlePointAdded = (event) => {
console.log('📡 HUD received pointAdded event, adding new point...');
const newPoint = event.detail;

// Add the new point to existing lists
setLocaList(prevList => [...prevList, newPoint]);
setFilteredList(prevList => [...prevList, newPoint]);
};

window.addEventListener('dataloaded', handleDataLoaded);
window.addEventListener('pointAdded', handlePointAdded);

return () => window.removeEventListener('dataloaded', handleDataLoaded);
return () => {
window.removeEventListener('dataloaded', handleDataLoaded);
window.removeEventListener('pointAdded', handlePointAdded);
};
}, []);

// Sort functionality
Expand Down
Loading
Loading