2024-07-03 19:10:15 +00:00
|
|
|
import DeleteIcon from "@mui/icons-material/Delete";
|
2024-07-03 17:17:47 +00:00
|
|
|
import {
|
2024-07-03 19:10:15 +00:00
|
|
|
IconButton,
|
2024-07-03 17:17:47 +00:00
|
|
|
Paper,
|
|
|
|
Table,
|
2024-07-03 19:10:15 +00:00
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableContainer,
|
2024-07-03 17:17:47 +00:00
|
|
|
TableHead,
|
|
|
|
TableRow,
|
2024-07-03 19:32:32 +00:00
|
|
|
Tooltip,
|
2024-07-03 17:17:47 +00:00
|
|
|
} from "@mui/material";
|
2024-07-03 19:10:15 +00:00
|
|
|
import React from "react";
|
|
|
|
import { Device, DeviceApi } from "../api/DeviceApi";
|
|
|
|
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";
|
|
|
|
import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer";
|
2024-07-03 17:17:47 +00:00
|
|
|
import { TimeWidget } from "../widgets/TimeWidget";
|
2024-07-03 19:32:32 +00:00
|
|
|
import CheckIcon from "@mui/icons-material/Check";
|
2024-07-03 17:17:47 +00:00
|
|
|
|
|
|
|
export function PendingDevicesRoute(): React.ReactElement {
|
|
|
|
const loadKey = React.useRef(1);
|
|
|
|
|
|
|
|
const [pending, setPending] = React.useState<Device[] | undefined>();
|
|
|
|
|
|
|
|
const load = async () => {
|
|
|
|
setPending(await DeviceApi.PendingList());
|
|
|
|
};
|
|
|
|
|
|
|
|
const reload = () => {
|
|
|
|
loadKey.current += 1;
|
|
|
|
setPending(undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SolarEnergyRouteContainer label="Pending devices">
|
|
|
|
<AsyncWidget
|
|
|
|
loadKey={loadKey.current}
|
|
|
|
ready={!!pending}
|
|
|
|
errMsg="Failed to load the list of pending devices!"
|
|
|
|
load={load}
|
|
|
|
build={() => (
|
|
|
|
<PendingDevicesList onReload={reload} pending={pending!} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</SolarEnergyRouteContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function PendingDevicesList(p: {
|
|
|
|
pending: Device[];
|
|
|
|
onReload: () => void;
|
|
|
|
}): React.ReactElement {
|
2024-07-03 19:10:15 +00:00
|
|
|
const alert = useAlert();
|
|
|
|
const confirm = useConfirm();
|
|
|
|
const snackbar = useSnackbar();
|
|
|
|
const loadingMessage = useLoadingMessage();
|
|
|
|
|
2024-07-03 19:32:32 +00:00
|
|
|
const validateDevice = async (d: Device) => {
|
|
|
|
try {
|
|
|
|
loadingMessage.show("Validating device...");
|
|
|
|
await DeviceApi.Validate(d);
|
|
|
|
|
|
|
|
snackbar("The device has been successfully validated!");
|
|
|
|
p.onReload();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to validate device! ${e})`);
|
|
|
|
alert("Failed to validate device!");
|
|
|
|
} finally {
|
|
|
|
loadingMessage.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-03 19:10:15 +00:00
|
|
|
const deleteDevice = async (d: Device) => {
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
!(await confirm(
|
|
|
|
`Do you really want to delete the device ${d.id}? The operation cannot be reverted!`
|
|
|
|
))
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
loadingMessage.show("Deleting device...");
|
|
|
|
await DeviceApi.Delete(d);
|
|
|
|
|
|
|
|
snackbar("The device has been successfully deleted!");
|
|
|
|
p.onReload();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to delete device! ${e})`);
|
|
|
|
alert("Failed to delete device!");
|
|
|
|
} finally {
|
|
|
|
loadingMessage.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (p.pending.length === 0) {
|
|
|
|
return <p>There is no device awaiting confirmation right now.</p>;
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:17:47 +00:00
|
|
|
return (
|
|
|
|
<TableContainer component={Paper}>
|
|
|
|
<Table sx={{ minWidth: 650 }} aria-label="simple table">
|
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell>#</TableCell>
|
|
|
|
<TableCell>Model</TableCell>
|
|
|
|
<TableCell>Version</TableCell>
|
2024-07-03 19:10:15 +00:00
|
|
|
<TableCell>Max number of relays</TableCell>
|
2024-07-03 17:17:47 +00:00
|
|
|
<TableCell>Created</TableCell>
|
2024-07-03 19:10:15 +00:00
|
|
|
<TableCell></TableCell>
|
2024-07-03 17:17:47 +00:00
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
{p.pending.map((dev) => (
|
|
|
|
<TableRow key={dev.id}>
|
|
|
|
<TableCell component="th" scope="row">
|
|
|
|
{dev.id}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell>{dev.info.reference}</TableCell>
|
|
|
|
<TableCell>{dev.info.version}</TableCell>
|
|
|
|
<TableCell>{dev.info.max_relays}</TableCell>
|
|
|
|
<TableCell>
|
|
|
|
<TimeWidget time={dev.time_create} />
|
|
|
|
</TableCell>
|
2024-07-03 19:10:15 +00:00
|
|
|
<TableCell>
|
2024-07-03 19:32:32 +00:00
|
|
|
<Tooltip title="Validate device">
|
|
|
|
<IconButton onClick={() => validateDevice(dev)}>
|
|
|
|
<CheckIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
<Tooltip title="Delete device">
|
|
|
|
<IconButton onClick={() => deleteDevice(dev)}>
|
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
2024-07-03 19:10:15 +00:00
|
|
|
</TableCell>
|
2024-07-03 17:17:47 +00:00
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
);
|
|
|
|
}
|