Files
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

160 lines
5.2 KiB
TypeScript

import React, { useState, useEffect } from 'react';
import { IotDevice, IotDeviceType, Location, Coop, Floor } from '../../types.ts';
import LocationSelector from '../shared/LocationSelector.tsx';
interface IotDeviceFormProps {
isOpen: boolean;
onClose: () => void;
onSave: (deviceData: Omit<IotDevice, 'id' | 'status' | 'lastHeartbeat'>, id?: string) => void;
deviceToEdit: IotDevice | null;
locations: Location[];
coops: Coop[];
floors: Floor[];
}
const IotDeviceForm: React.FC<IotDeviceFormProps> = ({
isOpen,
onClose,
onSave,
deviceToEdit,
locations,
coops,
floors,
}) => {
const [name, setName] = useState('');
const [type, setType] = useState<IotDeviceType>(IotDeviceType.TEMPERATURE_SENSOR);
const [floorId, setFloorId] = useState<string | null>(null);
useEffect(() => {
if (deviceToEdit) {
setName(deviceToEdit.name);
setType(deviceToEdit.type);
setFloorId(deviceToEdit.floorId);
} else {
setName('');
setType(IotDeviceType.TEMPERATURE_SENSOR);
setFloorId(null);
}
}, [deviceToEdit]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const randomValue = () => {
switch (type) {
case IotDeviceType.TEMPERATURE_SENSOR:
return '31.5 °C';
case IotDeviceType.HUMIDITY_SENSOR:
return '76%';
case IotDeviceType.AMMONIA_SENSOR:
return '15 ppm';
case IotDeviceType.WEIGHT_SCALE:
return '1583 g';
case IotDeviceType.CO2_SENSOR:
return '400 ppm';
case IotDeviceType.LIGHT_SENSOR:
return '15 lux';
case IotDeviceType.DUST_SENSOR:
return '25 µg/m³';
case IotDeviceType.WATER_FLOW_SENSOR:
return '14.8 L/jam';
case IotDeviceType.FEED_LEVEL_SENSOR:
return '75% Penuh';
case IotDeviceType.IOT_GATEWAY:
return 'Connected';
default:
return 'N/A';
}
};
const deviceData = { name, type, floorId, currentValue: randomValue() };
onSave(deviceData, deviceToEdit?.id);
};
if (!isOpen) return null;
return (
<div
className="fixed inset-0 bg-black bg-opacity-60 flex justify-center items-center z-50 p-4 transition-opacity"
onClick={onClose}
>
<div
className="bg-white rounded-xl shadow-2xl w-full max-w-lg transform transition-all"
onClick={(e) => e.stopPropagation()}
>
<form onSubmit={handleSubmit}>
<div className="p-6">
<div className="flex justify-between items-center mb-6">
<h2 className="text-2xl font-bold text-gray-800">
{deviceToEdit ? 'Edit Perangkat' : 'Tambah Perangkat Baru'}
</h2>
<button
type="button"
onClick={onClose}
className="text-gray-400 hover:text-gray-600 text-2xl"
>
&times;
</button>
</div>
<div className="space-y-4">
<div>
<label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-1">
Nama Perangkat
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500"
placeholder="e.g., Sensor Suhu Zona A-1"
required
/>
</div>
<div>
<label htmlFor="type" className="block text-sm font-medium text-gray-700 mb-1">
Tipe Perangkat
</label>
<select
id="type"
value={type}
onChange={(e) => setType(e.target.value as IotDeviceType)}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500 bg-white"
>
{Object.values(IotDeviceType).map((t) => (
<option key={t} value={t}>
{t}
</option>
))}
</select>
</div>
<LocationSelector
locations={locations}
coops={coops}
floors={floors}
initialFloorId={floorId}
onFloorChange={setFloorId}
/>
</div>
</div>
<div className="bg-gray-50 px-6 py-4 rounded-b-xl flex justify-end space-x-3">
<button
type="button"
onClick={onClose}
className="bg-gray-200 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-300 transition duration-300 font-medium"
>
Batal
</button>
<button
type="submit"
className="bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 transition duration-300 font-medium shadow-sm"
>
Simpan
</button>
</div>
</form>
</div>
</div>
);
};
export default IotDeviceForm;