This commit is contained in:
Jake Kasper
2025-08-27 10:28:43 -04:00
parent 7d4d6517c3
commit 3250f22458

View File

@@ -21,58 +21,71 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
useEffect(() => { useEffect(() => {
console.log('ApplicationExecutionModal - Application data:', application); console.log('ApplicationExecutionModal - Application data:', application);
console.log('ApplicationExecutionModal - Property details:', propertyDetails); console.log('ApplicationExecutionModal - Property details:', propertyDetails);
}, [application, propertyDetails]); console.log('ApplicationExecutionModal - Application sections:', application.sections);
console.log('ApplicationExecutionModal - Application areas:', application.areas);
console.log('ApplicationExecutionModal - Sections state:', sections);
console.log('ApplicationExecutionModal - Map center:', mapCenter);
}, [application, propertyDetails, sections, mapCenter]);
// Fetch section details for the application // Fetch section details for the application
useEffect(() => { useEffect(() => {
const fetchSectionData = async () => { const fetchSectionData = async () => {
if (!propertyDetails || !application.sections?.length) { if (!application?.id) {
return; return;
} }
try { try {
// If we have property details, get the sections from the property // First try to get detailed plan information from the API
// The property should already contain all sections const planResponse = await applicationsAPI.getPlan(application.id);
if (propertyDetails.sections) { const planDetails = planResponse.data.data.plan;
const applicationSectionIds = application.sections.map(s => s.id);
console.log('Fetched plan details:', planDetails);
console.log('Plan details areas:', planDetails.areas);
if (planDetails.areas && planDetails.areas.length > 0) {
setSections(planDetails.areas);
// Calculate center from section coordinates
let totalLat = 0;
let totalLng = 0;
let pointCount = 0;
planDetails.areas.forEach(area => {
let polygonData = area.polygonData;
if (typeof polygonData === 'string') {
try {
polygonData = JSON.parse(polygonData);
} catch (e) {
console.error('Failed to parse polygon data:', e);
return;
}
}
if (polygonData?.coordinates?.[0]) {
polygonData.coordinates[0].forEach(([lat, lng]) => {
totalLat += lat;
totalLng += lng;
pointCount++;
});
}
});
if (pointCount > 0) {
const avgLat = totalLat / pointCount;
const avgLng = totalLng / pointCount;
setMapCenter([avgLat, avgLng]);
}
}
// Fallback: If we have property details, use those sections
if ((!planDetails.areas || planDetails.areas.length === 0) && propertyDetails?.sections) {
const applicationSectionIds = application.sections?.map(s => s.id) || [];
const relevantSections = propertyDetails.sections.filter(section => const relevantSections = propertyDetails.sections.filter(section =>
applicationSectionIds.includes(section.id) applicationSectionIds.includes(section.id)
); );
setSections(relevantSections); setSections(relevantSections);
// Calculate center from section coordinates
if (relevantSections.length > 0) {
let totalLat = 0;
let totalLng = 0;
let pointCount = 0;
relevantSections.forEach(section => {
let polygonData = section.polygonData;
if (typeof polygonData === 'string') {
try {
polygonData = JSON.parse(polygonData);
} catch (e) {
console.error('Failed to parse polygon data:', e);
return;
}
}
if (polygonData?.coordinates?.[0]) {
polygonData.coordinates[0].forEach(([lat, lng]) => {
totalLat += lat;
totalLng += lng;
pointCount++;
});
}
});
if (pointCount > 0) {
const avgLat = totalLat / pointCount;
const avgLng = totalLng / pointCount;
setMapCenter([avgLat, avgLng]);
}
}
} }
} catch (error) { } catch (error) {
console.error('Failed to fetch section data:', error); console.error('Failed to fetch section data:', error);
} }
@@ -329,7 +342,7 @@ const ApplicationExecutionModal = ({ application, propertyDetails, onClose, onCo
<PropertyMap <PropertyMap
property={propertyDetails} property={propertyDetails}
sections={sections} sections={sections}
selectedSections={application.sections?.map(s => s.id) || []} selectedSections={sections.map(s => s.id) || []}
mode="execution" mode="execution"
gpsTrack={gpsTrack} gpsTrack={gpsTrack}
currentLocation={currentLocation} currentLocation={currentLocation}