admin products

This commit is contained in:
Jake Kasper
2025-09-03 14:52:12 -04:00
parent d201ab3d56
commit 151fa24ef7

View File

@@ -219,21 +219,52 @@ const AdminProducts = () => {
setSelectedProduct(null); setSelectedProduct(null);
}; };
const openEditModal = (product) => { const openEditModal = async (product) => {
setSelectedProduct(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({ setFormData({
name: product.name || product.customName || '', name: product.name || product.customName || '',
brand: product.brand || product.customBrand || '', brand: product.brand || product.customBrand || '',
categoryId: product.categoryId || '', categoryId: product.categoryId || '',
productType: product.productType || product.customProductType || 'granular', productType,
activeIngredients: product.activeIngredients || product.customActiveIngredients || '', activeIngredients: product.activeIngredients || product.customActiveIngredients || '',
description: product.description || product.customDescription || '', description: product.description || product.customDescription || '',
seedBlend: product.seedBlend || [], seedBlend: nextSeedBlend,
rates: product.rates && product.rates.length > 0 ? product.rates : [{ rates: nextRates.length > 0 ? nextRates : [{
applicationType: product.productType || 'granular', applicationType: productType,
rateAmount: product.customRateAmount || '', rateAmount: product.customRateAmount || '',
rateUnit: product.customRateUnit || 'lbs/1000 sq ft', rateUnit: product.customRateUnit || 'lbs/1000 sq ft',
notes: '' notes: ''
}] }]
}); });
setShowEditModal(true); setShowEditModal(true);