Can delete a device relay from UI
This commit is contained in:
@ -1,14 +1,32 @@
|
||||
import AddIcon from "@mui/icons-material/Add";
|
||||
import { IconButton, Tooltip } from "@mui/material";
|
||||
import DeleteIcon from "@mui/icons-material/Delete";
|
||||
import EditIcon from "@mui/icons-material/Edit";
|
||||
import {
|
||||
IconButton,
|
||||
ListItem,
|
||||
ListItemText,
|
||||
Tooltip,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import React from "react";
|
||||
import { Device, DeviceRelay } from "../../api/DeviceApi";
|
||||
import { DeviceRouteCard } from "./DeviceRouteCard";
|
||||
import { EditDeviceRelaysDialog } from "../../dialogs/EditDeviceRelaysDialog";
|
||||
import { DeviceRouteCard } from "./DeviceRouteCard";
|
||||
import { useConfirm } from "../../hooks/context_providers/ConfirmDialogProvider";
|
||||
import { useLoadingMessage } from "../../hooks/context_providers/LoadingMessageProvider";
|
||||
import { RelayApi } from "../../api/RelayApi";
|
||||
import { useSnackbar } from "../../hooks/context_providers/SnackbarProvider";
|
||||
import { useAlert } from "../../hooks/context_providers/AlertDialogProvider";
|
||||
|
||||
export function DeviceRelays(p: {
|
||||
device: Device;
|
||||
onReload: () => void;
|
||||
}): React.ReactElement {
|
||||
const confirm = useConfirm();
|
||||
const loadingMessage = useLoadingMessage();
|
||||
const snackbar = useSnackbar();
|
||||
const alert = useAlert();
|
||||
|
||||
const [dialogOpen, setDialogOpen] = React.useState(false);
|
||||
const [currRelay, setCurrRelay] = React.useState<DeviceRelay | undefined>();
|
||||
|
||||
@ -17,6 +35,25 @@ export function DeviceRelays(p: {
|
||||
setCurrRelay(undefined);
|
||||
};
|
||||
|
||||
const deleteRelay = async (r: DeviceRelay) => {
|
||||
if (
|
||||
!(await confirm("Do you really want to delete this relay configuration?"))
|
||||
)
|
||||
return;
|
||||
|
||||
try {
|
||||
await RelayApi.Delete(r);
|
||||
|
||||
p.onReload();
|
||||
snackbar("The relay configuration was successfully deleted!");
|
||||
} catch (e) {
|
||||
console.error("Failed to delete relay!", e);
|
||||
alert(`Failed to delete device relay configuration! ${e}`);
|
||||
} finally {
|
||||
loadingMessage.hide();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{dialogOpen && (
|
||||
@ -43,7 +80,39 @@ export function DeviceRelays(p: {
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
TODO : relays list ({p.device.relays.length}) relays now)
|
||||
{p.device.relays.length === 0 ? (
|
||||
<Typography style={{ textAlign: "center" }}>
|
||||
No relay configured yet.
|
||||
</Typography>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
|
||||
{p.device.relays.map((r, i) => (
|
||||
<ListItem
|
||||
alignItems="flex-start"
|
||||
key={r.id}
|
||||
secondaryAction={
|
||||
<>
|
||||
<Tooltip title="Edit the relay configuration">
|
||||
<IconButton>
|
||||
<EditIcon />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
|
||||
{i === p.device.relays.length - 1 && (
|
||||
<Tooltip title="Delete the relay configuration">
|
||||
<IconButton onClick={() => deleteRelay(r)}>
|
||||
<DeleteIcon />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
>
|
||||
<ListItemText primary={r.name} secondary={"TODO: status"} />
|
||||
</ListItem>
|
||||
))}
|
||||
</DeviceRouteCard>
|
||||
</>
|
||||
);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import DeleteIcon from "@mui/icons-material/Delete";
|
||||
import { Grid, IconButton, Tooltip } from "@mui/material";
|
||||
import { IconButton, Tooltip } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { Device, DeviceApi } from "../../api/DeviceApi";
|
||||
@ -11,6 +11,7 @@ import { AsyncWidget } from "../../widgets/AsyncWidget";
|
||||
import { SolarEnergyRouteContainer } from "../../widgets/SolarEnergyRouteContainer";
|
||||
import { GeneralDeviceInfo } from "./GeneralDeviceInfo";
|
||||
import { DeviceRelays } from "./DeviceRelays";
|
||||
import Grid from "@mui/material/Grid2";
|
||||
|
||||
export function DeviceRoute(): React.ReactElement {
|
||||
const { id } = useParams();
|
||||
@ -81,10 +82,10 @@ function DeviceRouteInner(p: {
|
||||
}
|
||||
>
|
||||
<Grid container spacing={2}>
|
||||
<Grid item xs={12} md={6}>
|
||||
<Grid size={{ xs: 12, md: 6 }}>
|
||||
<GeneralDeviceInfo {...p} />
|
||||
</Grid>
|
||||
<Grid item xs={12} md={6}>
|
||||
<Grid size={{ xs: 12, md: 6 }}>
|
||||
<DeviceRelays {...p} />
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
@ -4,7 +4,6 @@ import Avatar from "@mui/material/Avatar";
|
||||
import Box from "@mui/material/Box";
|
||||
import Button from "@mui/material/Button";
|
||||
import CssBaseline from "@mui/material/CssBaseline";
|
||||
import Grid from "@mui/material/Grid";
|
||||
import Link from "@mui/material/Link";
|
||||
import Paper from "@mui/material/Paper";
|
||||
import TextField from "@mui/material/TextField";
|
||||
@ -12,6 +11,7 @@ import Typography from "@mui/material/Typography";
|
||||
import * as React from "react";
|
||||
import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider";
|
||||
import { AuthApi } from "../api/AuthApi";
|
||||
import Grid from "@mui/material/Grid2";
|
||||
|
||||
function Copyright(props: any) {
|
||||
return (
|
||||
@ -60,10 +60,7 @@ export function LoginRoute() {
|
||||
<Grid container component="main" sx={{ height: "100vh" }}>
|
||||
<CssBaseline />
|
||||
<Grid
|
||||
item
|
||||
xs={false}
|
||||
sm={4}
|
||||
md={7}
|
||||
size={{ sm: 4, md: 7, xs: false }}
|
||||
sx={{
|
||||
backgroundImage: 'url("/sun.jpg")',
|
||||
backgroundColor: (t) =>
|
||||
@ -74,7 +71,12 @@ export function LoginRoute() {
|
||||
backgroundPosition: "left",
|
||||
}}
|
||||
/>
|
||||
<Grid item xs={12} sm={8} md={5} component={Paper} elevation={6} square>
|
||||
<Grid
|
||||
size={{ xs: 12, sm: 8, md: 5 }}
|
||||
component={Paper}
|
||||
elevation={6}
|
||||
square
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
my: 8,
|
||||
|
Reference in New Issue
Block a user