From 151fa24ef71e31d1407f440a8f5d69685754ebbb Mon Sep 17 00:00:00 2001 From: Jake Kasper Date: Wed, 3 Sep 2025 14:52:12 -0400 Subject: [PATCH] admin products --- frontend/src/pages/Admin/AdminProducts.js | 47 +++++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/frontend/src/pages/Admin/AdminProducts.js b/frontend/src/pages/Admin/AdminProducts.js index 75e2863..6c45671 100644 --- a/frontend/src/pages/Admin/AdminProducts.js +++ b/frontend/src/pages/Admin/AdminProducts.js @@ -219,21 +219,52 @@ const AdminProducts = () => { setSelectedProduct(null); }; - const openEditModal = (product) => { + const openEditModal = async (product) => { setSelectedProduct(product); + const productType = product.productType || product.customProductType || 'granular'; + + // Seed blend defaults from row (shared products expose seedBlend directly) + let nextSeedBlend = product.seedBlend || []; + let nextRates = product.rates && product.rates.length > 0 ? product.rates : []; + + // Fetch rates for shared products so preview/editor has full data + if (product.isShared && product.id && adminAPI.getProductRates) { + try { + const r = await adminAPI.getProductRates(product.id); + nextRates = r.data?.data?.rates || nextRates; + } catch (e) { + console.warn('Admin fetch rates failed', e?.response?.data || e.message); + } + } + + // For custom seed products, parse activeIngredients JSON for blend and seed rates + if (!product.isShared && productType === 'seed' && product.customActiveIngredients) { + try { + const ai = typeof product.customActiveIngredients === 'string' ? JSON.parse(product.customActiveIngredients) : product.customActiveIngredients; + if (Array.isArray(ai?.seedBlend)) nextSeedBlend = ai.seedBlend; + if (ai?.seedRates) { + const unit = ai.seedRates.unit || product.customRateUnit || 'lbs/1000 sq ft'; + const rates = []; + if (ai.seedRates.new) rates.push({ applicationType: 'New Lawn', rateAmount: ai.seedRates.new, rateUnit: unit, notes: '' }); + if (ai.seedRates.overseed) rates.push({ applicationType: 'Overseeding', rateAmount: ai.seedRates.overseed, rateUnit: unit, notes: '' }); + if (rates.length) nextRates = rates; + } + } catch {} + } + setFormData({ name: product.name || product.customName || '', brand: product.brand || product.customBrand || '', categoryId: product.categoryId || '', - productType: product.productType || product.customProductType || 'granular', + productType, activeIngredients: product.activeIngredients || product.customActiveIngredients || '', description: product.description || product.customDescription || '', - seedBlend: product.seedBlend || [], - rates: product.rates && product.rates.length > 0 ? product.rates : [{ - applicationType: product.productType || 'granular', - rateAmount: product.customRateAmount || '', - rateUnit: product.customRateUnit || 'lbs/1000 sq ft', - notes: '' + seedBlend: nextSeedBlend, + rates: nextRates.length > 0 ? nextRates : [{ + applicationType: productType, + rateAmount: product.customRateAmount || '', + rateUnit: product.customRateUnit || 'lbs/1000 sq ft', + notes: '' }] }); setShowEditModal(true);