{"ast":null,"code":"import React,{useState,useEffect}from'react';import{FiX,FiSave}from'react-icons/fi';import{deviceApi}from'../services/api';import{jsx as _jsx,jsxs as _jsxs}from\"react/jsx-runtime\";const EditDeviceModal=_ref=>{let{isOpen,onClose,device,onDeviceUpdated}=_ref;const[loading,setLoading]=useState(false);const[error,setError]=useState('');const[formData,setFormData]=useState({name:'',description:'',model:'SIM7000E',firmware_version:'',is_active:true,latitude:'',longitude:'',altitude:''});useEffect(()=>{if(device){var _device$location,_device$location2,_device$location3;setFormData({name:device.name||'',description:device.description||'',model:device.model||'SIM7000E',firmware_version:device.firmware_version||'',is_active:device.is_active!==undefined?device.is_active:true,latitude:device.latitude||((_device$location=device.location)===null||_device$location===void 0?void 0:_device$location.latitude)||'',longitude:device.longitude||((_device$location2=device.location)===null||_device$location2===void 0?void 0:_device$location2.longitude)||'',altitude:device.altitude||((_device$location3=device.location)===null||_device$location3===void 0?void 0:_device$location3.altitude)||''});}},[device]);const handleChange=e=>{const{name,value,type,checked}=e.target;setFormData(prev=>({...prev,[name]:type==='checkbox'?checked:value}));};const handleSubmit=async e=>{e.preventDefault();if(!device)return;setLoading(true);setError('');try{// Prepare update data - only include fields that have values\nconst updateData={};if(formData.name.trim())updateData.name=formData.name.trim();if(formData.description.trim())updateData.description=formData.description.trim();if(formData.model)updateData.model=formData.model;if(formData.firmware_version.trim())updateData.firmware_version=formData.firmware_version.trim();updateData.is_active=formData.is_active;// Add coordinates if provided\nif(formData.latitude&&!isNaN(parseFloat(formData.latitude))){updateData.latitude=parseFloat(formData.latitude);}if(formData.longitude&&!isNaN(parseFloat(formData.longitude))){updateData.longitude=parseFloat(formData.longitude);}if(formData.altitude&&!isNaN(parseFloat(formData.altitude))){updateData.altitude=parseFloat(formData.altitude);}const response=await deviceApi.updateDevice(device.id,updateData);if(onDeviceUpdated){onDeviceUpdated(response.data);}onClose();}catch(error){var _error$response,_error$response$data;console.error('Error updating device:',error);setError(((_error$response=error.response)===null||_error$response===void 0?void 0:(_error$response$data=_error$response.data)===null||_error$response$data===void 0?void 0:_error$response$data.detail)||'Failed to update device');}finally{setLoading(false);}};if(!isOpen)return null;return/*#__PURE__*/_jsx(\"div\",{className:\"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50\",children:/*#__PURE__*/_jsxs(\"div\",{className:\"bg-white rounded-lg shadow-xl max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto\",children:[/*#__PURE__*/_jsxs(\"div\",{className:\"flex items-center justify-between p-6 border-b border-gray-200\",children:[/*#__PURE__*/_jsx(\"h2\",{className:\"text-xl font-semibold text-gray-800\",children:\"Edit Device\"}),/*#__PURE__*/_jsx(\"button\",{onClick:onClose,className:\"p-2 text-gray-400 hover:text-gray-600 transition-colors\",children:/*#__PURE__*/_jsx(FiX,{className:\"w-5 h-5\"})})]}),/*#__PURE__*/_jsxs(\"form\",{onSubmit:handleSubmit,className:\"p-6\",children:[error&&/*#__PURE__*/_jsx(\"div\",{className:\"mb-4 p-3 bg-red-50 border border-red-200 rounded-md\",children:/*#__PURE__*/_jsx(\"p\",{className:\"text-red-600 text-sm\",children:error})}),/*#__PURE__*/_jsxs(\"div\",{className:\"space-y-4\",children:[/*#__PURE__*/_jsxs(\"div\",{className:\"grid grid-cols-1 md:grid-cols-2 gap-4\",children:[/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"name\",className:\"block text-sm font-medium text-gray-700 mb-1\",children:\"Device Name *\"}),/*#__PURE__*/_jsx(\"input\",{type:\"text\",id:\"name\",name:\"name\",required:true,value:formData.name,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"Enter device name\"})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"model\",className:\"block text-sm font-medium text-gray-700 mb-1\",children:\"Model\"}),/*#__PURE__*/_jsxs(\"select\",{id:\"model\",name:\"model\",value:formData.model,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",children:[/*#__PURE__*/_jsx(\"option\",{value:\"SIM7000E\",children:\"SIM7000E\"}),/*#__PURE__*/_jsx(\"option\",{value:\"SIM7000A\",children:\"SIM7000A\"}),/*#__PURE__*/_jsx(\"option\",{value:\"SIM7000G\",children:\"SIM7000G\"}),/*#__PURE__*/_jsx(\"option\",{value:\"LILYGO T-A7670G\",children:\"LILYGO T-A7670G\"}),/*#__PURE__*/_jsx(\"option\",{value:\"Custom\",children:\"Custom\"})]})]})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"description\",className:\"block text-sm font-medium text-gray-700 mb-1\",children:\"Description\"}),/*#__PURE__*/_jsx(\"textarea\",{id:\"description\",name:\"description\",rows:\"3\",value:formData.description,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"Enter device description\"})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"firmware_version\",className:\"block text-sm font-medium text-gray-700 mb-1\",children:\"Firmware Version\"}),/*#__PURE__*/_jsx(\"input\",{type:\"text\",id:\"firmware_version\",name:\"firmware_version\",value:formData.firmware_version,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"e.g., 1.2.3\"})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{className:\"block text-sm font-medium text-gray-700 mb-2\",children:\"Location (Optional)\"}),/*#__PURE__*/_jsxs(\"div\",{className:\"grid grid-cols-1 md:grid-cols-3 gap-4\",children:[/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"latitude\",className:\"block text-xs font-medium text-gray-500 mb-1\",children:\"Latitude\"}),/*#__PURE__*/_jsx(\"input\",{type:\"number\",id:\"latitude\",name:\"latitude\",step:\"any\",value:formData.latitude,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"47.3769\"})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"longitude\",className:\"block text-xs font-medium text-gray-500 mb-1\",children:\"Longitude\"}),/*#__PURE__*/_jsx(\"input\",{type:\"number\",id:\"longitude\",name:\"longitude\",step:\"any\",value:formData.longitude,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"8.5417\"})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{htmlFor:\"altitude\",className:\"block text-xs font-medium text-gray-500 mb-1\",children:\"Altitude (m)\"}),/*#__PURE__*/_jsx(\"input\",{type:\"number\",id:\"altitude\",name:\"altitude\",step:\"any\",value:formData.altitude,onChange:handleChange,className:\"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm\",placeholder:\"408\"})]})]})]}),/*#__PURE__*/_jsxs(\"div\",{children:[/*#__PURE__*/_jsx(\"label\",{className:\"block text-sm font-medium text-gray-700 mb-2\",children:\"Status\"}),/*#__PURE__*/_jsxs(\"div\",{className:\"flex items-center\",children:[/*#__PURE__*/_jsx(\"input\",{type:\"checkbox\",id:\"is_active\",name:\"is_active\",checked:formData.is_active,onChange:handleChange,className:\"h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded\"}),/*#__PURE__*/_jsx(\"label\",{htmlFor:\"is_active\",className:\"ml-2 block text-sm text-gray-700\",children:\"Device is active\"})]})]})]}),/*#__PURE__*/_jsxs(\"div\",{className:\"flex justify-end gap-3 mt-6 pt-6 border-t border-gray-200\",children:[/*#__PURE__*/_jsx(\"button\",{type:\"button\",onClick:onClose,className:\"px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 hover:bg-gray-50 transition-colors\",children:\"Cancel\"}),/*#__PURE__*/_jsxs(\"button\",{type:\"submit\",disabled:loading,className:`flex items-center gap-2 px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors ${loading?'opacity-50 cursor-not-allowed':''}`,children:[/*#__PURE__*/_jsx(FiSave,{className:\"w-4 h-4\"}),loading?'Updating...':'Update Device']})]})]})]})});};export default EditDeviceModal;","map":{"version":3,"names":["React","useState","useEffect","FiX","FiSave","deviceApi","jsx","_jsx","jsxs","_jsxs","EditDeviceModal","_ref","isOpen","onClose","device","onDeviceUpdated","loading","setLoading","error","setError","formData","setFormData","name","description","model","firmware_version","is_active","latitude","longitude","altitude","_device$location","_device$location2","_device$location3","undefined","location","handleChange","e","value","type","checked","target","prev","handleSubmit","preventDefault","updateData","trim","isNaN","parseFloat","response","updateDevice","id","data","_error$response","_error$response$data","console","detail","className","children","onClick","onSubmit","htmlFor","required","onChange","placeholder","rows","step","disabled"],"sources":["/home/m3mo/Desktop/temparea/solarbank/frontend/src/components/EditDeviceModal.js"],"sourcesContent":["import React, { useState, useEffect } from 'react';\nimport { FiX, FiSave } from 'react-icons/fi';\nimport { deviceApi } from '../services/api';\n\nconst EditDeviceModal = ({ isOpen, onClose, device, onDeviceUpdated }) => {\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState('');\n const [formData, setFormData] = useState({\n name: '',\n description: '',\n model: 'SIM7000E',\n firmware_version: '',\n is_active: true,\n latitude: '',\n longitude: '',\n altitude: ''\n });\n\n useEffect(() => {\n if (device) {\n setFormData({\n name: device.name || '',\n description: device.description || '',\n model: device.model || 'SIM7000E',\n firmware_version: device.firmware_version || '',\n is_active: device.is_active !== undefined ? device.is_active : true,\n latitude: device.latitude || device.location?.latitude || '',\n longitude: device.longitude || device.location?.longitude || '',\n altitude: device.altitude || device.location?.altitude || ''\n });\n }\n }, [device]);\n\n const handleChange = (e) => {\n const { name, value, type, checked } = e.target;\n setFormData(prev => ({\n ...prev,\n [name]: type === 'checkbox' ? checked : value\n }));\n };\n\n const handleSubmit = async (e) => {\n e.preventDefault();\n if (!device) return;\n\n setLoading(true);\n setError('');\n\n try {\n // Prepare update data - only include fields that have values\n const updateData = {};\n \n if (formData.name.trim()) updateData.name = formData.name.trim();\n if (formData.description.trim()) updateData.description = formData.description.trim();\n if (formData.model) updateData.model = formData.model;\n if (formData.firmware_version.trim()) updateData.firmware_version = formData.firmware_version.trim();\n updateData.is_active = formData.is_active;\n \n // Add coordinates if provided\n if (formData.latitude && !isNaN(parseFloat(formData.latitude))) {\n updateData.latitude = parseFloat(formData.latitude);\n }\n if (formData.longitude && !isNaN(parseFloat(formData.longitude))) {\n updateData.longitude = parseFloat(formData.longitude);\n }\n if (formData.altitude && !isNaN(parseFloat(formData.altitude))) {\n updateData.altitude = parseFloat(formData.altitude);\n }\n\n const response = await deviceApi.updateDevice(device.id, updateData);\n \n if (onDeviceUpdated) {\n onDeviceUpdated(response.data);\n }\n \n onClose();\n } catch (error) {\n console.error('Error updating device:', error);\n setError(error.response?.data?.detail || 'Failed to update device');\n } finally {\n setLoading(false);\n }\n };\n\n if (!isOpen) return null;\n\n return (\n