import { Button, Checkbox, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, FormControl, FormControlLabel, FormGroup, FormLabel, Radio, RadioGroup, Typography, } from "@mui/material"; import React from "react"; import { Device, DeviceApi } from "../api/DeviceApi"; import { OTAAPI, OTAUpdate } from "../api/OTAApi"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; import { useConfirm } from "../hooks/context_providers/ConfirmDialogProvider"; import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider"; import { useSnackbar } from "../hooks/context_providers/SnackbarProvider"; import { AsyncWidget } from "../widgets/AsyncWidget"; export function DeployOTAUpdateDialogProvider(p: { update: OTAUpdate; onClose: () => void; }): React.ReactElement { const loadingMessage = useLoadingMessage(); const alert = useAlert(); const confirm = useConfirm(); const snackbar = useSnackbar(); const [devicesList, setDevicesList] = React.useState(); const loadDevicesList = async () => { let list = await DeviceApi.ValidatedList(); list = list.filter((e) => e.info.reference == p.update.platform); setDevicesList(list); }; const [allDevices, setAllDevices] = React.useState(false); const [selectedDevices, setSelectedDevices] = React.useState([]); const startDeployment = async () => { if ( allDevices && !(await confirm( "Do you really want to deploy the update to all devices?" )) ) return; try { loadingMessage.show("Applying OTA update..."); await OTAAPI.SetDesiredVersion(p.update, allDevices, selectedDevices); snackbar("The update was successfully applied!"); p.onClose(); } catch (e) { console.error("Failed to deploy the udpate!", e); alert(`Failed to deploy the udpate! ${e}`); } finally { loadingMessage.hide(); } }; return ( Deploy update {p.update.version} for platform{" "} {p.update.platform} ( You can choose to deploy update to all device or to target only a part of devices: Gender setAllDevices(v.target.value == "true")} > } label="Deploy the update to all the devices of the platform" /> } label="Deploy the update to a limited range of devices" /> {!allDevices && ( There are no devices to which the update can be deployed. )} {!allDevices && ( {devicesList?.map((d) => ( { if (v) { selectedDevices.push(d.id); setSelectedDevices([...selectedDevices]); } else setSelectedDevices( selectedDevices.filter((e) => e != d.id) ); }} /> } label={d.name} /> ))} )} )} /> ); }