diff --git a/client/src/components/AlertModals.jsx b/client/src/components/AlertModals.jsx index 7d1cfd4..4f77e92 100644 --- a/client/src/components/AlertModals.jsx +++ b/client/src/components/AlertModals.jsx @@ -41,6 +41,16 @@ export const EditAlertModal = ({ rule, onClose, onSuccess }) => { useEffect(() => { if (rule) { + // Normalize alert_channels - ensure it's always an array + let alertChannels = rule.alert_channels || ['sms']; + if (typeof alertChannels === 'object' && !Array.isArray(alertChannels)) { + // Convert object like {sms: true, webhook: false, email: true} to array + alertChannels = Object.keys(alertChannels).filter(key => alertChannels[key]); + } + if (!Array.isArray(alertChannels)) { + alertChannels = ['sms']; // fallback to default + } + setFormData({ name: rule.name || '', description: rule.description || '', @@ -48,7 +58,7 @@ export const EditAlertModal = ({ rule, onClose, onSuccess }) => { min_detections: rule.min_detections || 1, time_window: rule.time_window || 300, cooldown_period: rule.cooldown_period || 600, - alert_channels: rule.alert_channels || ['sms'], + alert_channels: alertChannels, min_threat_level: rule.min_threat_level || '', drone_types: rule.drone_types || [], device_ids: rule.device_ids || [], @@ -67,12 +77,17 @@ export const EditAlertModal = ({ rule, onClose, onSuccess }) => { }; const handleChannelChange = (channel, checked) => { - setFormData(prev => ({ - ...prev, - alert_channels: checked - ? [...prev.alert_channels, channel] - : prev.alert_channels.filter(c => c !== channel) - })); + setFormData(prev => { + // Ensure alert_channels is always an array + const currentChannels = Array.isArray(prev.alert_channels) ? prev.alert_channels : []; + + return { + ...prev, + alert_channels: checked + ? [...currentChannels, channel] + : currentChannels.filter(c => c !== channel) + }; + }); }; const handleDroneTypeChange = (droneType, checked) => { @@ -269,7 +284,7 @@ export const EditAlertModal = ({ rule, onClose, onSuccess }) => {