SolarEnergy/central_frontend/src/routes/PendingDevicesRoute.tsx

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-07-03 19:10:15 +00:00
import DeleteIcon from "@mui/icons-material/Delete";
import {
2024-07-03 19:10:15 +00:00
IconButton,
Paper,
Table,
2024-07-03 19:10:15 +00:00
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
} 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";
import { TimeWidget } from "../widgets/TimeWidget";
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();
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>;
}
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>
<TableCell>Created</TableCell>
2024-07-03 19:10:15 +00:00
<TableCell></TableCell>
</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>
<IconButton onClick={() => deleteDevice(dev)}>
<DeleteIcon />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}