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