diff --git a/frontend/src/pages/Applications/Applications.js b/frontend/src/pages/Applications/Applications.js index f506121..eaf9926 100644 --- a/frontend/src/pages/Applications/Applications.js +++ b/frontend/src/pages/Applications/Applications.js @@ -104,7 +104,9 @@ const ApplicationPlanModal = ({ onClose, onSubmit }) => { const [properties, setProperties] = useState([]); const [products, setProducts] = useState([]); const [equipment, setEquipment] = useState([]); + const [selectedPropertyDetails, setSelectedPropertyDetails] = useState(null); const [loading, setLoading] = useState(true); + const [loadingProperty, setLoadingProperty] = useState(false); const [planData, setPlanData] = useState({ propertyId: '', @@ -124,12 +126,21 @@ const ApplicationPlanModal = ({ onClose, onSubmit }) => { setLoading(true); const [propertiesResponse, productsResponse, equipmentResponse] = await Promise.all([ propertiesAPI.getAll(), - productsAPI.getUserProducts(), + productsAPI.getAll(), equipmentAPI.getAll() ]); + console.log('Properties response:', propertiesResponse.data); + console.log('Products response:', productsResponse.data); + console.log('Equipment response:', equipmentResponse.data); + setProperties(propertiesResponse.data.data.properties || []); - setProducts(productsResponse.data.data.products || []); + // Combine shared and user products + const allProducts = [ + ...(productsResponse.data.data.sharedProducts || []), + ...(productsResponse.data.data.userProducts || []) + ]; + setProducts(allProducts); setEquipment(equipmentResponse.data.data.equipment || []); } catch (error) { console.error('Failed to fetch planning data:', error); @@ -139,7 +150,28 @@ const ApplicationPlanModal = ({ onClose, onSubmit }) => { } }; - const selectedProperty = properties.find(p => p.id === parseInt(planData.propertyId)); + const fetchPropertyDetails = async (propertyId) => { + try { + setLoadingProperty(true); + const response = await propertiesAPI.getById(parseInt(propertyId)); + console.log('Property details response:', response.data); + setSelectedPropertyDetails(response.data.data.property); + } catch (error) { + console.error('Failed to fetch property details:', error); + toast.error('Failed to load property details'); + setSelectedPropertyDetails(null); + } finally { + setLoadingProperty(false); + } + }; + + const handlePropertyChange = (propertyId) => { + setPlanData({ ...planData, propertyId, selectedAreas: [] }); + setSelectedPropertyDetails(null); + if (propertyId) { + fetchPropertyDetails(propertyId); + } + }; // Filter equipment based on application type const availableEquipment = equipment.filter(eq => { @@ -199,7 +231,7 @@ const ApplicationPlanModal = ({ onClose, onSubmit }) => { { className="rounded border-gray-300 text-blue-600 focus:ring-blue-500" /> - {section.name} ({section.sqFt ? `${section.sqFt} sq ft` : 'No size'}) + {section.name} ({section.area ? `${Math.round(section.area)} sq ft` : 'No size'}) ))} {planData.selectedAreas.length > 0 && (

- Total area: {selectedProperty.sections + Total area: {selectedPropertyDetails.sections .filter(s => planData.selectedAreas.includes(s.id)) - .reduce((total, s) => total + (s.sqFt || 0), 0)} sq ft + .reduce((total, s) => total + (s.area || 0), 0).toFixed(0)} sq ft

)} )} + + {selectedPropertyDetails && (!selectedPropertyDetails.sections || selectedPropertyDetails.sections.length === 0) && ( +
+

+ This property has no lawn sections defined. Please add lawn sections to the property first. +

+
+ )} {/* Product Selection */}
@@ -251,21 +298,43 @@ const ApplicationPlanModal = ({ onClose, onSubmit }) => { value={planData.productId} onChange={(e) => { const selectedProduct = products.find(p => p.id === parseInt(e.target.value)); + console.log('Selected product:', selectedProduct); + + // Determine application type from product type + let applicationType = ''; + if (selectedProduct) { + const productType = selectedProduct.productType || selectedProduct.customProductType; + // Map product types to application types + if (productType && (productType.toLowerCase().includes('liquid') || productType.toLowerCase().includes('concentrate'))) { + applicationType = 'liquid'; + } else if (productType && (productType.toLowerCase().includes('granular') || productType.toLowerCase().includes('granule'))) { + applicationType = 'granular'; + } + } + setPlanData({ ...planData, productId: e.target.value, - applicationType: selectedProduct?.applicationType || '' + applicationType: applicationType }); }} required > - {products.map((product) => ( - - ))} + {products.map((product) => { + const displayName = product.customName || product.name; + const productType = product.productType || product.customProductType; + const brand = product.brand || product.customBrand; + const rateInfo = product.customRateAmount && product.customRateUnit + ? ` (${product.customRateAmount} ${product.customRateUnit})` + : ''; + + return ( + + ); + })}