Create API routes to request relay information

This commit is contained in:
Pierre HUBERT 2024-07-30 23:04:53 +02:00
parent 3004b03d92
commit 5497c36c75
2 changed files with 32 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { APIClient } from "./ApiClient"; import { APIClient } from "./ApiClient";
import { DeviceRelay } from "./DeviceApi"; import { Device, DeviceRelay } from "./DeviceApi";
export class RelayApi { export class RelayApi {
/** /**
@ -13,4 +13,29 @@ export class RelayApi {
}) })
).data; ).data;
} }
/**
* Create a new relay
*/
static async Create(device: Device, relay: DeviceRelay): Promise<void> {
await APIClient.exec({
method: "POST",
uri: "/relay/create",
jsonData: {
...relay,
device_id: device.id,
},
});
}
/**
* Update a relay information
*/
static async Update(relay: DeviceRelay): Promise<void> {
await APIClient.exec({
method: "PUT",
uri: `/relay/${relay.id}`,
jsonData: relay,
});
}
} }

View File

@ -10,6 +10,7 @@ import {
import { TimePicker } from "@mui/x-date-pickers"; import { TimePicker } from "@mui/x-date-pickers";
import React from "react"; import React from "react";
import { Device, DeviceRelay } from "../api/DeviceApi"; import { Device, DeviceRelay } from "../api/DeviceApi";
import { RelayApi } from "../api/RelayApi";
import { ServerApi } from "../api/ServerApi"; import { ServerApi } from "../api/ServerApi";
import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider";
import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider"; import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider";
@ -53,7 +54,8 @@ export function EditDeviceRelaysDialog(p: {
`${creating ? "Creating" : "Updating"} relay information` `${creating ? "Creating" : "Updating"} relay information`
); );
// TODO if (creating) await RelayApi.Create(p.device, relay);
else await RelayApi.Update(relay);
snackbar( snackbar(
`The relay have been successfully ${creating ? "created" : "updated"}!` `The relay have been successfully ${creating ? "created" : "updated"}!`
@ -73,7 +75,9 @@ export function EditDeviceRelaysDialog(p: {
return ( return (
<Dialog open> <Dialog open>
<DialogTitle>Edit relay information</DialogTitle> <DialogTitle>
{creating ? "Create a new relay" : "Edit relay information"}
</DialogTitle>
<DialogContent> <DialogContent>
<DialogFormTitle>General info</DialogFormTitle> <DialogFormTitle>General info</DialogFormTitle>
<Grid container spacing={2}> <Grid container spacing={2}>