diff --git a/frontend/src/components/Applications/ApplicationPlanModal.js b/frontend/src/components/Applications/ApplicationPlanModal.js index 91deef5..510132e 100644 --- a/frontend/src/components/Applications/ApplicationPlanModal.js +++ b/frontend/src/components/Applications/ApplicationPlanModal.js @@ -202,6 +202,29 @@ const ApplicationPlanModal = ({ console.log('=== End Debug ===\n'); }, [selectedPropertyDetails, mapCenter, equipment, products, nozzles]); + // Helper to pick appropriate seed rate for scenario + const pickSeedRate = (rates, mode) => { + if (!Array.isArray(rates) || rates.length === 0) return null; + const list = rates.map(r => ({ + ...r, + _t: `${(r.applicationType || '')} ${(r.notes || '')}`.toLowerCase() + })); + const isNew = mode === 'new_seed'; + // Prefer explicit text match in applicationType/notes + let match = list.find(r => isNew ? r._t.includes('new') : r._t.includes('over')); + if (match) return match; + // Fallback: choose by amount (new lawn usually higher) + const numeric = rates.filter(r => !isNaN(parseFloat(r.rateAmount))); + if (numeric.length) { + return numeric.reduce((best, r) => { + const a = parseFloat(best.rateAmount); + const b = parseFloat(r.rateAmount); + return isNew ? (b > a ? r : best) : (b < a ? r : best); + }, numeric[0]); + } + return rates[0]; + }; + // Handle area selection on map const handleAreaClick = (area) => { setSelectedAreas(prev => { @@ -250,6 +273,20 @@ const ApplicationPlanModal = ({ }, 0); }; + // When seed scenario changes, auto-pick a sensible default rate for seed products without a rate yet + useEffect(() => { + if (applicationType !== 'seed') return; + setSelectedProducts(prev => prev.map(p => { + if (p.productType !== 'seed' || p.rateAmount) return p; + const pd = products.find(pp => (pp.uniqueId || pp.id) === p.uniqueId); + if (pd && pd.rates && pd.rates.length) { + const chosen = pickSeedRate(pd.rates, seedMode) || pd.rates[0]; + return { ...p, rateAmount: chosen.rateAmount || chosen.amount, rateUnit: chosen.rateUnit || chosen.unit || 'lb/1000sqft' }; + } + return p; + })); + }, [seedMode, applicationType, products]); + // Remove product from plan const removeProduct = (index) => { setSelectedProducts(prev => prev.filter((_, i) => i !== index)); @@ -758,17 +795,13 @@ const ApplicationPlanModal = ({ // For shared products: check rates array if (selectedProduct.isShared && selectedProduct.rates && selectedProduct.rates.length > 0) { - let defaultRate = selectedProduct.rates[0]; + let chosen = selectedProduct.rates[0]; if (applicationType === 'seed') { - const modeKey = seedMode === 'new_seed' ? 'new' : 'over'; - const match = selectedProduct.rates.find(r => { - const t = (r.applicationType || '').toLowerCase(); - return t.includes('seed') && t.includes(modeKey); - }); - if (match) defaultRate = match; + const picked = pickSeedRate(selectedProduct.rates, seedMode); + if (picked) chosen = picked; } - updateProduct(index, 'rateAmount', defaultRate.rateAmount || defaultRate.amount); - updateProduct(index, 'rateUnit', defaultRate.rateUnit || defaultRate.unit); + updateProduct(index, 'rateAmount', chosen.rateAmount || chosen.amount); + updateProduct(index, 'rateUnit', chosen.rateUnit || chosen.unit || ((applicationType === 'seed' || applicationType === 'granular') ? 'lb/1000sqft' : 'oz/1000sqft')); rateSet = true; } // For custom products: check customRateAmount and customRateUnit