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

175 lines
6.0 KiB
TypeScript

import React, { useState, useEffect } from 'react';
import { Camera, Location, Coop, Floor, CameraType } from '../../types.ts';
import LocationSelector from '../shared/LocationSelector.tsx';
interface CameraFormProps {
isOpen: boolean;
onClose: () => void;
onSave: (cameraData: Omit<Camera, 'id' | 'status'>, id?: string) => void;
cameraToEdit: Camera | null;
locations: Location[];
coops: Coop[];
floors: Floor[];
}
const CameraForm: React.FC<CameraFormProps> = ({
isOpen,
onClose,
onSave,
cameraToEdit,
locations,
coops,
floors,
}) => {
const [name, setName] = useState('');
const [rtspUrl, setRtspUrl] = useState('');
const [model, setModel] = useState('');
const [floorId, setFloorId] = useState<string | null>(null);
const [cameraType, setCameraType] = useState<CameraType>(CameraType.STANDARD);
useEffect(() => {
if (cameraToEdit) {
setName(cameraToEdit.name);
setRtspUrl(cameraToEdit.rtspUrl);
setModel(cameraToEdit.model);
setFloorId(cameraToEdit.floorId);
setCameraType(cameraToEdit.cameraType);
} else {
setName('');
setRtspUrl('');
setModel('');
setFloorId(null);
setCameraType(CameraType.STANDARD);
}
}, [cameraToEdit]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const cameraData = { name, rtspUrl, model, floorId, cameraType };
onSave(cameraData, cameraToEdit?.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">
{cameraToEdit ? 'Edit Kamera' : 'Tambah Kamera 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 Kamera
</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., Kamera Sudut Kanan Zona A"
required
/>
</div>
<div>
<label htmlFor="rtspUrl" className="block text-sm font-medium text-gray-700 mb-1">
URL RTSP
</label>
<input
type="text"
id="rtspUrl"
value={rtspUrl}
onChange={(e) => setRtspUrl(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="rtsp://user:pass@192.168.1.10..."
required
/>
</div>
<LocationSelector
locations={locations}
coops={coops}
floors={floors}
initialFloorId={floorId}
onFloorChange={setFloorId}
/>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div>
<label
htmlFor="cameraType"
className="block text-sm font-medium text-gray-700 mb-1"
>
Tipe Kamera
</label>
<select
id="cameraType"
value={cameraType}
onChange={(e) => setCameraType(e.target.value as CameraType)}
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(CameraType).map((t) => (
<option key={t} value={t}>
{t}
</option>
))}
</select>
</div>
<div>
<label htmlFor="model" className="block text-sm font-medium text-gray-700 mb-1">
Model Kamera
</label>
<input
type="text"
id="model"
value={model}
onChange={(e) => setModel(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., Hikvision DS-2CD2143G0-I"
required
/>
</div>
</div>
</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 CameraForm;