This commit is contained in:
Jake Kasper
2025-08-28 08:04:12 -05:00
parent 1ee3d2f572
commit e22cc10310
2 changed files with 42 additions and 5 deletions

View File

@@ -239,6 +239,8 @@ const ApplicationPlanModal = ({
? `/api/product-spreader-settings/product/${productApiId}`
: `/api/product-spreader-settings/user-product/${productApiId}`;
console.log('Checking spreader settings:', { product, equipmentId, endpoint });
const response = await fetch(endpoint, {
headers: {
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
@@ -247,20 +249,33 @@ const ApplicationPlanModal = ({
if (response.ok) {
const data = await response.json();
console.log('Spreader settings response:', data);
const settings = data.data?.settings || [];
const equipmentSetting = settings.find(s => s.equipmentId === parseInt(equipmentId));
// For shared products, check if any setting has the matching equipmentId
// For user products, the query already filters by equipment
const equipmentSetting = settings.find(s => {
console.log('Comparing setting:', s, 'with equipmentId:', equipmentId);
return s.equipmentId === parseInt(equipmentId);
});
console.log('Found equipment setting:', equipmentSetting);
if (!equipmentSetting) {
// No spreader setting found, prompt user to add one
console.log('No setting found, showing form');
setCurrentProductForSettings(product);
setShowSpreaderSettingsForm(true);
} else {
// Store the spreader setting for calculations
console.log('Setting found, storing for calculations');
setSpreaderSettings(prev => ({
...prev,
[`${product.id}_${equipmentId}`]: equipmentSetting
}));
}
} else {
console.log('Response not ok:', response.status);
}
} catch (error) {
console.error('Failed to check spreader settings:', error);
@@ -300,7 +315,20 @@ const ApplicationPlanModal = ({
});
if (response.ok) {
const result = await response.json();
console.log('Spreader setting saved successfully:', result);
toast.success('Spreader setting added successfully');
// Store the new setting for calculations
if (result.data?.setting) {
setSpreaderSettings(prev => ({
...prev,
[`${currentProductForSettings.id}_${selectedEquipmentId}`]: result.data.setting
}));
}
// Close the form
setShowSpreaderSettingsForm(false);
setCurrentProductForSettings(null);
setSpreaderFormData({
@@ -309,7 +337,9 @@ const ApplicationPlanModal = ({
notes: ''
});
} else {
throw new Error('Failed to save spreader settings');
const errorData = await response.json();
console.error('Failed to save spreader settings:', errorData);
throw new Error(errorData.message || 'Failed to save spreader settings');
}
} catch (error) {
console.error('Failed to save spreader settings:', error);