From 04e80a0ac08eb8925feb1aeb87407d649b17e72c Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 16 Nov 2024 02:11:09 +0200 Subject: [PATCH] Refactor WakeComponent: integrate useSWRMutation for waking machines, update API endpoint usage, and enhance type definitions --- .../components/common/WakeComponent.tsx | 48 +++++++++---------- frontend/src/types/commands.ts | 4 ++ frontend/src/types/events.ts | 3 +- frontend/src/utils/api.js | 1 - 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/frontend/src/features/machines/components/common/WakeComponent.tsx b/frontend/src/features/machines/components/common/WakeComponent.tsx index e93d263..d82cdfd 100644 --- a/frontend/src/features/machines/components/common/WakeComponent.tsx +++ b/frontend/src/features/machines/components/common/WakeComponent.tsx @@ -4,9 +4,10 @@ import { PowerSettingsNew } from "@mui/icons-material"; import { useTranslation } from "react-i18next"; import { blip } from "../../../../utils"; import { msToMinAndSec } from "../../../../utils/time"; -import { routes, post } from "../../../../utils/api"; -import { Machine } from "types"; +import { endpoints } from "../../../../utils/api"; +import { Machine, MachineWaked, WakeMachine } from "types"; import { usePingTrigger } from "../../hooks"; +import { Key, mutationFetcher, useSWRMutation } from "units/swr"; const initialState = { on: false }; const defaultPingInterval = 1200000; //20 minutes @@ -57,29 +58,28 @@ const WakeComponent: React.FC = ({ machine, addLog, disabled }) => { } }); - const wakeMachine = useCallback(async () => { - await post( - routes.wakeMachine, - { machineId: machine.machineId }, - { - onCompleted: (result: any) => { - setState(prev => ({ ...prev, on: result.success })); - log(`[Wake]: Success: ${result.success}. Status: ${result.status}`); - if (result.success) { - blip.success(result.status); + const { trigger: wakeMachineTrigger } = useSWRMutation( + endpoints.network.machine.wake, + mutationFetcher, + { + onError: err => blip.error(err.message), + onSuccess: result => { + setState(prev => ({ ...prev, on: result.success })); + log(`[Wake]: Success: ${result.success}. Status: ${result.status}`); + if (result.success) { + blip.success(result.status); - //retrigger - log(`Periodic ping will be re-triggered in ${startingTime} ms [${msToMinAndSec(startingTime)}]`); - // setTimeout(() => { - // setTrigger(prev => !prev); - // }, startingTime); - } else { - blip.error(result.status); - } + //retrigger + log(`Periodic ping will be re-triggered in ${startingTime} ms [${msToMinAndSec(startingTime)}]`); + // setTimeout(() => { + // setTrigger(prev => !prev); + // }, startingTime); + } else { + blip.error(result.status); } } - ); - }, [log, machine.machineId]); + } + ); const pingMachine = useCallback(async () => { if (disabled) return; @@ -90,8 +90,8 @@ const WakeComponent: React.FC = ({ machine, addLog, disabled }) => { pingMachine(); }, [trigger, pingMachine]); - const handleWakeClick = (event: any) => { - wakeMachine(); + const handleWakeClick = (event: React.MouseEvent) => { + wakeMachineTrigger({ machineId: machine.machineId }); event.stopPropagation(); }; diff --git a/frontend/src/types/commands.ts b/frontend/src/types/commands.ts index 13945c5..4ad5b28 100644 --- a/frontend/src/types/commands.ts +++ b/frontend/src/types/commands.ts @@ -1,3 +1,7 @@ +export type WakeMachine = { + machineId: number; +}; + export type PingMachine = { machineId: number; }; diff --git a/frontend/src/types/events.ts b/frontend/src/types/events.ts index eda3b92..64602a3 100644 --- a/frontend/src/types/events.ts +++ b/frontend/src/types/events.ts @@ -3,8 +3,7 @@ export type MachineActionResult = { status: string; }; +export type MachineWaked = MachineActionResult; export type MachinePinged = MachineActionResult; - export type MachineShutdown = MachineActionResult; - export type MachineRestarted = MachineActionResult; diff --git a/frontend/src/utils/api.js b/frontend/src/utils/api.js index 7973e05..c450b66 100644 --- a/frontend/src/utils/api.js +++ b/frontend/src/utils/api.js @@ -10,7 +10,6 @@ const powerActionsRoute = `${apiHost}/resurrector`; const securityRoute = `${apiHost}/security`; const routes = { - wakeMachine: `${powerActionsRoute}/wake`, network: { machines: `${networkRoute}/machines`, machine: {