2025-08-15 09:32:33 +01:00
|
|
|
import { Formik, Field, Form } from "formik";
|
|
|
|
|
import type {
|
2025-09-16 11:07:35 +01:00
|
|
|
CameraConfig,
|
2025-08-15 09:32:33 +01:00
|
|
|
CameraSettingErrorValues,
|
|
|
|
|
CameraSettingValues,
|
|
|
|
|
} from "../../types/types";
|
2025-09-16 14:20:38 +01:00
|
|
|
import { useMemo, useState } from "react";
|
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
|
import { faEye, faEyeSlash } from "@fortawesome/free-regular-svg-icons";
|
2025-08-15 09:32:33 +01:00
|
|
|
|
2025-09-16 11:07:35 +01:00
|
|
|
type CameraSettingsProps = {
|
|
|
|
|
initialData: CameraConfig;
|
2025-09-16 14:20:38 +01:00
|
|
|
updateCameraConfig: (values: CameraSettingValues) => Promise<void> | void;
|
2025-09-16 11:07:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CameraSettingFields = ({
|
|
|
|
|
initialData,
|
|
|
|
|
updateCameraConfig,
|
|
|
|
|
}: CameraSettingsProps) => {
|
2025-09-16 14:20:38 +01:00
|
|
|
const [showPwd, setShowPwd] = useState(false);
|
|
|
|
|
|
|
|
|
|
const initialValues = useMemo<CameraSettingValues>(
|
|
|
|
|
() => ({
|
|
|
|
|
friendlyName: initialData?.propLEDDriverControlURI?.value ?? "",
|
|
|
|
|
cameraAddress: "",
|
|
|
|
|
userName: "",
|
|
|
|
|
password: "",
|
|
|
|
|
id: initialData?.id,
|
|
|
|
|
}),
|
|
|
|
|
[initialData?.id, initialData?.propLEDDriverControlURI?.value]
|
|
|
|
|
);
|
2025-08-18 12:53:30 +01:00
|
|
|
|
2025-08-15 09:32:33 +01:00
|
|
|
const validateValues = (values: CameraSettingValues) => {
|
|
|
|
|
const errors: CameraSettingErrorValues = {};
|
|
|
|
|
if (!values.friendlyName) errors.friendlyName = "Required";
|
|
|
|
|
if (!values.cameraAddress) errors.cameraAddress = "Required";
|
|
|
|
|
if (!values.userName) errors.userName = "Required";
|
|
|
|
|
if (!values.password) errors.password = "Required";
|
2025-09-10 09:05:47 +01:00
|
|
|
|
2025-08-15 09:32:33 +01:00
|
|
|
return errors;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (values: CameraSettingValues) => {
|
2025-09-12 08:21:52 +01:00
|
|
|
updateCameraConfig(values);
|
2025-08-15 09:32:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
validate={validateValues}
|
|
|
|
|
validateOnChange={false}
|
2025-09-16 14:20:38 +01:00
|
|
|
enableReinitialize
|
2025-08-15 09:32:33 +01:00
|
|
|
>
|
2025-09-10 09:05:47 +01:00
|
|
|
{({ errors, touched }) => (
|
2025-08-18 12:53:30 +01:00
|
|
|
<Form className="flex flex-col space-y-4 p-2">
|
|
|
|
|
<div className="flex flex-col space-y-2 relative">
|
2025-09-10 09:05:47 +01:00
|
|
|
<label htmlFor="friendlyName">Name</label>
|
2025-08-18 12:53:30 +01:00
|
|
|
{touched.friendlyName && errors.friendlyName && (
|
|
|
|
|
<small className="absolute right-0 top-0 text-red-500">
|
|
|
|
|
{errors.friendlyName}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="friendlyName"
|
|
|
|
|
name="friendlyName"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
|
|
|
|
placeholder="Enter camera name"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-08-15 09:32:33 +01:00
|
|
|
|
2025-08-18 12:53:30 +01:00
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="cameraAddress">Camera Address</label>
|
|
|
|
|
{touched.cameraAddress && errors.cameraAddress && (
|
|
|
|
|
<small className="absolute right-0 top-0 text-red-500">
|
|
|
|
|
{errors.cameraAddress}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="cameraAddress"
|
|
|
|
|
name="cameraAddress"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
2025-09-10 09:05:47 +01:00
|
|
|
placeholder="RTSP://..."
|
2025-08-18 12:53:30 +01:00
|
|
|
autoComplete="street-address"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="userName">User Name</label>
|
|
|
|
|
{touched.userName && errors.userName && (
|
|
|
|
|
<small className="absolute right-0 top-0 text-red-500">
|
|
|
|
|
{errors.userName}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="userName"
|
|
|
|
|
name="userName"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg"
|
|
|
|
|
placeholder="Enter user name"
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col space-y-2 relative">
|
|
|
|
|
<label htmlFor="password">Password</label>
|
|
|
|
|
{touched.password && errors.password && (
|
|
|
|
|
<small className="absolute right-0 top-0 text-red-500">
|
|
|
|
|
{errors.password}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
2025-09-16 14:20:38 +01:00
|
|
|
<div className="flex gap-2 items-center relative">
|
|
|
|
|
<Field
|
|
|
|
|
id="password"
|
|
|
|
|
name="password"
|
|
|
|
|
type={showPwd ? "text" : "password"}
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full "
|
|
|
|
|
placeholder="Enter password"
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
/>
|
|
|
|
|
<FontAwesomeIcon
|
|
|
|
|
type="button"
|
|
|
|
|
className="absolute right-5 end-0"
|
|
|
|
|
onClick={() => setShowPwd((s) => !s)}
|
|
|
|
|
icon={showPwd ? faEyeSlash : faEye}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-08-18 12:53:30 +01:00
|
|
|
|
2025-09-16 14:20:38 +01:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
2025-09-21 20:10:05 +01:00
|
|
|
className="bg-blue-800 text-white rounded-lg p-2 mx-auto h-[100%] w-full"
|
2025-09-16 14:20:38 +01:00
|
|
|
>
|
|
|
|
|
Save settings
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-08-18 12:53:30 +01:00
|
|
|
</Form>
|
|
|
|
|
)}
|
2025-08-15 09:32:33 +01:00
|
|
|
</Formik>
|
|
|
|
|
);
|
2025-08-13 14:23:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CameraSettingFields;
|