2025-09-12 13:28:14 +01:00
|
|
|
import { useFormikContext } from "formik";
|
|
|
|
|
import FormGroup from "../components/FormGroup";
|
|
|
|
|
import { toast } from "sonner";
|
2025-09-15 10:27:31 +01:00
|
|
|
import { useSystemConfig } from "../../../hooks/useSystemConfig";
|
2025-09-12 13:28:14 +01:00
|
|
|
|
|
|
|
|
type SystemFileUploadProps = {
|
|
|
|
|
name: string;
|
|
|
|
|
selectedFile: File | null | undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SystemFileUpload = ({ name, selectedFile }: SystemFileUploadProps) => {
|
|
|
|
|
const { setFieldValue } = useFormikContext();
|
2025-09-15 10:27:31 +01:00
|
|
|
const { uploadSettings } = useSystemConfig();
|
|
|
|
|
|
|
|
|
|
const handleFileUploadClick = () => {
|
|
|
|
|
if (!selectedFile) return;
|
2025-09-17 11:39:26 +01:00
|
|
|
const settings = {
|
|
|
|
|
file: selectedFile,
|
|
|
|
|
opts: {
|
|
|
|
|
timeoutMs: 30000,
|
|
|
|
|
fieldName: "upload",
|
2025-09-17 14:39:56 +01:00
|
|
|
uploadUrl: "http://192.168.75.11/upload/software-update/2",
|
2025-09-17 11:39:26 +01:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
uploadSettings(settings);
|
2025-09-15 10:27:31 +01:00
|
|
|
};
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
return (
|
|
|
|
|
<div className="py-8 w-full">
|
2025-09-17 11:39:26 +01:00
|
|
|
<div className="border-b border-gray-600">
|
|
|
|
|
<h2>Software Update file upload</h2>
|
|
|
|
|
</div>
|
2025-09-12 13:28:14 +01:00
|
|
|
<FormGroup>
|
2025-09-17 11:39:26 +01:00
|
|
|
<div className="flex-1 flex md:w-2/3 my-5">
|
2025-09-12 13:28:14 +01:00
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
name="softwareUpdate"
|
|
|
|
|
id="softwareUpdate"
|
2025-10-20 10:50:16 +01:00
|
|
|
className="mt-4 w-full flex flex-col items-center justify-center rounded-2xl border border-slate-800 bg-slate-900/40 p-10 text-center file:px-3 file:border file:border-gray-500 file:rounded-lg file:bg-blue-800 file:mr-5"
|
2025-09-12 13:28:14 +01:00
|
|
|
onChange={(event) => {
|
|
|
|
|
const file = event.currentTarget.files?.[0];
|
|
|
|
|
if (!file) {
|
|
|
|
|
toast.error("No File selected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 10:50:16 +01:00
|
|
|
if (file?.size > 8 * 1024 * 1024) toast.error("File is too large (max 8MB).");
|
2025-09-12 13:28:14 +01:00
|
|
|
setFieldValue(name, file);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2025-10-20 10:50:16 +01:00
|
|
|
className="w-1/4 text-white bg-green-700 hover:bg-green-800 font-small rounded-lg text-sm px-2 py-2.5 disabled:opacity-50 disabled:cursor-not-allowed"
|
2025-09-12 13:28:14 +01:00
|
|
|
disabled={!selectedFile}
|
|
|
|
|
onClick={handleFileUploadClick}
|
|
|
|
|
>
|
|
|
|
|
Upload Software Update
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SystemFileUpload;
|