make polygons save

This commit is contained in:
Jake Kasper
2025-08-21 18:06:01 -04:00
parent 8b9a47c589
commit 7f9489fca0

View File

@@ -263,7 +263,20 @@ const PropertyDetail = () => {
setLoading(true); setLoading(true);
const response = await propertiesAPI.getById(id); const response = await propertiesAPI.getById(id);
console.log('Property details:', response); console.log('Property details:', response);
setProperty(response.data.data.property); const propertyData = response.data.data.property;
setProperty(propertyData);
// Convert backend sections to frontend format
if (propertyData.sections && propertyData.sections.length > 0) {
const convertedSections = propertyData.sections.map(section => ({
id: section.id,
name: section.name,
coordinates: section.polygonData?.coordinates?.[0] || [],
color: section.polygonData?.color || SECTION_COLORS[0],
area: section.area
}));
setLawnSections(convertedSections);
}
} catch (error) { } catch (error) {
console.error('Failed to fetch property:', error); console.error('Failed to fetch property:', error);
toast.error('Failed to load property'); toast.error('Failed to load property');
@@ -287,35 +300,61 @@ const PropertyDetail = () => {
setIsDrawing(false); setIsDrawing(false);
}; };
const saveLawnSection = () => { const saveLawnSection = async () => {
if (!sectionName.trim()) { if (!sectionName.trim()) {
toast.error('Please enter a section name'); toast.error('Please enter a section name');
return; return;
} }
const newSection = { try {
id: Date.now(), const sectionData = {
name: sectionName, name: sectionName,
coordinates: pendingSection.coordinates, area: pendingSection.area,
color: pendingSection.color, polygonData: {
area: pendingSection.area coordinates: [pendingSection.coordinates],
}; color: pendingSection.color
},
grassType: null,
soilType: null
};
setLawnSections(prev => [...prev, newSection]); const response = await propertiesAPI.createSection(id, sectionData);
toast.success(`${sectionName} section created!`); const savedSection = response.data.data.section;
// Reset and cycle color // Convert backend format to frontend format
setSectionName(''); const newSection = {
setPendingSection(null); id: savedSection.id,
setShowNameModal(false); name: savedSection.name,
const nextIndex = (SECTION_COLORS.findIndex(c => c.value === currentColor.value) + 1) % SECTION_COLORS.length; coordinates: savedSection.polygonData.coordinates[0],
setCurrentColor(SECTION_COLORS[nextIndex]); color: savedSection.polygonData.color,
area: savedSection.area
};
setLawnSections(prev => [...prev, newSection]);
toast.success(`${sectionName} section created and saved!`);
// Reset and cycle color
setSectionName('');
setPendingSection(null);
setShowNameModal(false);
const nextIndex = (SECTION_COLORS.findIndex(c => c.value === currentColor.value) + 1) % SECTION_COLORS.length;
setCurrentColor(SECTION_COLORS[nextIndex]);
} catch (error) {
console.error('Failed to save section:', error);
toast.error('Failed to save section. Please try again.');
}
}; };
const deleteLawnSection = (sectionId) => { const deleteLawnSection = async (sectionId) => {
if (window.confirm('Delete this lawn section?')) { if (window.confirm('Delete this lawn section?')) {
setLawnSections(prev => prev.filter(s => s.id !== sectionId)); try {
toast.success('Section deleted'); await propertiesAPI.deleteSection(id, sectionId);
setLawnSections(prev => prev.filter(s => s.id !== sectionId));
toast.success('Section deleted');
} catch (error) {
console.error('Failed to delete section:', error);
toast.error('Failed to delete section. Please try again.');
}
} }
}; };
@@ -326,29 +365,64 @@ const PropertyDetail = () => {
setShowEditModal(true); setShowEditModal(true);
}; };
const saveEditedSection = () => { const saveEditedSection = async () => {
if (!sectionName.trim()) { if (!sectionName.trim()) {
toast.error('Please enter a section name'); toast.error('Please enter a section name');
return; return;
} }
const updatedSection = { try {
...editingSection, const sectionData = {
name: sectionName, name: sectionName,
color: currentColor area: editingSection.area,
}; polygonData: {
coordinates: [editingSection.coordinates],
color: currentColor
},
grassType: null,
soilType: null
};
setLawnSections(prev => prev.map(s => s.id === editingSection.id ? updatedSection : s)); await propertiesAPI.updateSection(id, editingSection.id, sectionData);
toast.success('Section updated!');
const updatedSection = {
// Reset ...editingSection,
setSectionName(''); name: sectionName,
setEditingSection(null); color: currentColor
setShowEditModal(false); };
setLawnSections(prev => prev.map(s => s.id === editingSection.id ? updatedSection : s));
toast.success('Section updated and saved!');
// Reset
setSectionName('');
setEditingSection(null);
setShowEditModal(false);
} catch (error) {
console.error('Failed to update section:', error);
toast.error('Failed to update section. Please try again.');
}
}; };
const updateSection = (sectionId, updatedSection) => { const updateSection = async (sectionId, updatedSection) => {
setLawnSections(prev => prev.map(s => s.id === sectionId ? updatedSection : s)); try {
const sectionData = {
name: updatedSection.name,
area: updatedSection.area,
polygonData: {
coordinates: [updatedSection.coordinates],
color: updatedSection.color
},
grassType: null,
soilType: null
};
await propertiesAPI.updateSection(id, sectionId, sectionData);
setLawnSections(prev => prev.map(s => s.id === sectionId ? updatedSection : s));
} catch (error) {
console.error('Failed to update section coordinates:', error);
toast.error('Failed to save polygon changes. Please try again.');
}
}; };
const getTotalArea = () => { const getTotalArea = () => {