This commit is contained in:
Jake Kasper
2025-09-02 09:32:25 -05:00
parent f29876b041
commit ae3275299e
2 changed files with 109 additions and 36 deletions

View File

@@ -1,10 +1,11 @@
import React, { useState, useEffect, useMemo } from 'react';
import React, { useState, useEffect, useMemo, useRef } from 'react';
import { applicationsAPI, propertiesAPI } from '../../services/api';
import PropertyMap from '../Maps/PropertyMap';
import toast from 'react-hot-toast';
const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onComplete }) => {
const [isTracking, setIsTracking] = useState(false);
const [isPaused, setIsPaused] = useState(false);
const [gpsTrack, setGpsTrack] = useState([]);
const [currentLocation, setCurrentLocation] = useState(null);
const [currentSpeed, setCurrentSpeed] = useState(0);
@@ -14,6 +15,8 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
const [previousTime, setPreviousTime] = useState(null);
const [totalDistance, setTotalDistance] = useState(0);
const [averageSpeed, setAverageSpeed] = useState(0);
const totalDistanceRef = useRef(0);
const wakeLockRef = useRef(null);
const [sections, setSections] = useState([]);
const [mapCenter, setMapCenter] = useState(null);
const [planDetails, setPlanDetails] = useState(null);
@@ -124,6 +127,11 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
}, [currentSpeed, targetSpeed]);
// Start GPS tracking
const requestWakeLock = async () => {
try { if ('wakeLock' in navigator) { wakeLockRef.current = await navigator.wakeLock.request('screen'); } } catch {}
};
const releaseWakeLock = async () => { try { await wakeLockRef.current?.release(); } catch {} wakeLockRef.current = null; };
const startTracking = () => {
if (!navigator.geolocation) {
toast.error('GPS not available on this device');
@@ -131,9 +139,9 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
}
setIsTracking(true);
setStartTime(new Date());
setGpsTrack([]);
setTotalDistance(0);
setIsPaused(false);
if (!startTime) setStartTime(new Date());
requestWakeLock();
setPreviousLocation(null);
setPreviousTime(null);
@@ -163,15 +171,13 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
if (timeDiff > 0) {
const speedMph = (distance / timeDiff) * 2.237; // Convert m/s to mph
setCurrentSpeed(speedMph);
setTotalDistance(prev => prev + distance);
const newTotal = totalDistanceRef.current + distance;
totalDistanceRef.current = newTotal;
setTotalDistance(newTotal);
// Update average speed
const totalTime = (timestamp - startTime) / 1000; // seconds
if (totalTime > 0) {
const avgSpeedMph = (totalDistance + distance) / totalTime * 2.237;
setAverageSpeed(avgSpeedMph);
}
if (totalTime > 0) setAverageSpeed((newTotal / totalTime) * 2.237);
}
}
@@ -201,6 +207,8 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
setWatchId(null);
}
setIsTracking(false);
setIsPaused(true);
releaseWakeLock();
};
// Complete application
@@ -305,6 +313,12 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
const response = await applicationsAPI.createLog(logData);
console.log('CreateLog response:', response);
toast.success('Application completed successfully');
// reset local tracking state
setGpsTrack([]);
setTotalDistance(0);
totalDistanceRef.current = 0;
setAverageSpeed(0);
setIsPaused(false);
onComplete();
} catch (error) {
console.error('Failed to save application log:', error);
@@ -333,9 +347,8 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
// Cleanup on unmount
useEffect(() => {
return () => {
if (watchId) {
navigator.geolocation.clearWatch(watchId);
}
if (watchId) navigator.geolocation.clearWatch(watchId);
releaseWakeLock();
};
}, [watchId]);
@@ -468,12 +481,7 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
{/* Action Buttons */}
<div className="flex gap-3">
{!isTracking ? (
<button
onClick={startTracking}
className="btn-primary flex-1"
>
Start Application
</button>
<button onClick={startTracking} className="btn-primary flex-1">{isPaused ? 'Resume' : 'Start'} Application</button>
) : (
<>
<button
@@ -502,4 +510,4 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
);
};
export default ApplicationExecutionModal;
export default ApplicationExecutionModal;