This commit is contained in:
Jake Kasper
2025-09-02 12:31:33 -05:00
parent 4d52aae85b
commit 7037272258

View File

@@ -9,6 +9,7 @@ const MowingExecutionModal = ({ plan, onClose, onComplete }) => {
const [tracking, setTracking] = useState(false);
const [isPaused, setIsPaused] = useState(false);
const [gpsTrack, setGpsTrack] = useState([]);
const [mapCenter, setMapCenter] = useState(null);
const [currentLocation, setCurrentLocation] = useState(null);
const [currentSpeed, setCurrentSpeed] = useState(0);
const [previousLocation, setPreviousLocation] = useState(null);
@@ -24,11 +25,35 @@ const MowingExecutionModal = ({ plan, onClose, onComplete }) => {
const load = async () => {
try {
const r = await mowingAPI.getPlan(plan.id);
setSections(r.data.data.sections || []);
} catch {}
const secs = r.data.data.sections || [];
setSections(secs);
if (!secs.length && plan.property_id) {
// Fallback: fetch sections from property
const pr = await propertiesAPI.getById(plan.property_id);
const propSecs = pr.data?.data?.property?.sections || [];
setSections(propSecs);
computeCenter(propSecs);
} else {
computeCenter(secs);
}
} catch (e) {
try {
if (plan.property_id) {
const pr = await propertiesAPI.getById(plan.property_id);
const propSecs = pr.data?.data?.property?.sections || [];
setSections(propSecs);
computeCenter(propSecs);
}
} catch {}
}
};
const computeCenter = (secs) => {
let totalLat=0,totalLng=0,count=0;
secs.forEach(s=>{ let poly=s.polygonData || s.polygon_data; if (typeof poly==='string'){ try{poly=JSON.parse(poly);}catch{return;} } if (poly?.coordinates?.[0]){ poly.coordinates[0].forEach(([lat,lng])=>{ totalLat+=lat; totalLng+=lng; count++;});}});
if (count) setMapCenter([totalLat/count, totalLng/count]); else setMapCenter(null);
};
load();
}, [plan?.id]);
}, [plan?.id, plan?.property_id]);
const toRad = (d) => (d * Math.PI) / 180;
const haversineMeters = (a, b) => {
@@ -110,8 +135,7 @@ const MowingExecutionModal = ({ plan, onClose, onComplete }) => {
useEffect(() => () => { if (watchId) navigator.geolocation.clearWatch(watchId); releaseWakeLock(); }, [watchId]);
const center = (() => {
let totalLat=0,totalLng=0,count=0; sections.forEach(s=>{ let poly=s.polygonData; if (typeof poly==='string'){ try{poly=JSON.parse(poly);}catch{return;} } if (poly?.coordinates?.[0]){ poly.coordinates[0].forEach(([lat,lng])=>{ totalLat+=lat; totalLng+=lng; count++;});}}); return count? [totalLat/count, totalLng/count]: null; })();
const center = mapCenter;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">