From 5497c36c75b0b4e141bd89233cb72f1aaaaf2d64 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 30 Jul 2024 23:04:53 +0200 Subject: [PATCH] Create API routes to request relay information --- central_frontend/src/api/RelayApi.ts | 27 ++++++++++++++++++- .../src/dialogs/EditDeviceRelaysDialog.tsx | 8 ++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/central_frontend/src/api/RelayApi.ts b/central_frontend/src/api/RelayApi.ts index 33f7546..2951884 100644 --- a/central_frontend/src/api/RelayApi.ts +++ b/central_frontend/src/api/RelayApi.ts @@ -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 { + await APIClient.exec({ + method: "POST", + uri: "/relay/create", + jsonData: { + ...relay, + device_id: device.id, + }, + }); + } + + /** + * Update a relay information + */ + static async Update(relay: DeviceRelay): Promise { + await APIClient.exec({ + method: "PUT", + uri: `/relay/${relay.id}`, + jsonData: relay, + }); + } } diff --git a/central_frontend/src/dialogs/EditDeviceRelaysDialog.tsx b/central_frontend/src/dialogs/EditDeviceRelaysDialog.tsx index c042692..ca9a161 100644 --- a/central_frontend/src/dialogs/EditDeviceRelaysDialog.tsx +++ b/central_frontend/src/dialogs/EditDeviceRelaysDialog.tsx @@ -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 ( - Edit relay information + + {creating ? "Create a new relay" : "Edit relay information"} + General info