From e9c7bacb065688eb3b767b50d5e36745e96be53a Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 17 Apr 2021 03:21:43 +0300 Subject: [PATCH] call server --- .env | 3 +- src/features/machines/api.js | 17 ++++++++-- .../machines/components/MachinesContainer.js | 32 ++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/.env b/.env index f4a2e96..31d922b 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ #REACT_APP_IDENTITY_AUTHENTICATION_URL=http://localhost:5063/identity/authenticate?UserName={username}&Password={password} REACT_APP_IDENTITY_AUTHENTICATION_URL=https://toodle.ddns.net/identity-server-api/identity/authenticate?UserName={username}&Password={password} -REACT_APP_NETWORK_RESURRECTOR_API_URL=http://localhost:5064 \ No newline at end of file +REACT_APP_NETWORK_RESURRECTOR_API_URL=http://localhost:5064 +REACT_APP_NETWORK_RESURRECTOR_SERVER_URL=http://localhost:5062 \ No newline at end of file diff --git a/src/features/machines/api.js b/src/features/machines/api.js index 38d1fb0..2f73eb5 100644 --- a/src/features/machines/api.js +++ b/src/features/machines/api.js @@ -1,10 +1,21 @@ import { get } from "../../utils/axios"; -const url = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`; +const apiUrl = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`; +const serverUrl = `${process.env.REACT_APP_NETWORK_RESURRECTOR_SERVER_URL}/resurrector`; const readMachines = () => { - const machinesPromise = get(`${url}/machines`); + const machinesPromise = get(`${apiUrl}/machines`); return machinesPromise; }; -export { readMachines }; +const wakeMachine = macAddress => { + const promise = get(`${serverUrl}/wake`, { macAddress }); + return promise; +}; + +const pingMachine = ipAddressOrMachineName => { + const promise = get(`${serverUrl}/ping`, { ipAddressOrMachineName }); + return promise; +}; + +export { readMachines, wakeMachine, pingMachine }; diff --git a/src/features/machines/components/MachinesContainer.js b/src/features/machines/components/MachinesContainer.js index 395d0c2..0954e9c 100644 --- a/src/features/machines/components/MachinesContainer.js +++ b/src/features/machines/components/MachinesContainer.js @@ -3,31 +3,53 @@ import { ApplicationStateContext, ApplicationDispatchContext } from "../../../state/ApplicationContexts"; -import { readMachines } from "../api"; +import * as api from "../api"; import MachinesList from "./MachinesList"; import { PowerSettingsNew, LastPage } from "@material-ui/icons"; +import { useToast } from "../../../hooks"; const MachinesContainer = () => { const state = useContext(ApplicationStateContext); const dispatchActions = useContext(ApplicationDispatchContext); + const { success, error } = useToast(); + const handleReadMachines = useCallback(async () => { - const machines = await readMachines(); + const machines = await api.readMachines(); const data = Object.assign(machines, { loaded: true }); dispatchActions.onNetworkChange("machines", data); }, [dispatchActions]); - const onClick = machine => () => alert(machine); + const wakeMachine = machine => async () => { + const result = await api.wakeMachine(machine.macAddress); + if (result.success) { + success(result.status); + } else { + error(result.status); + } + }; + + const pingMachine = machine => async () => { + const result = await api.pingMachine( + machine.iPv4Address || machine.machineName + ); + if (result.success) { + success(result.status); + } else { + error(result.status); + } + }; + const actions = [ { code: "wake", - effect: onClick, + effect: wakeMachine, icon: PowerSettingsNew, tooltip: "Wake" }, { code: "ping", - effect: onClick, + effect: pingMachine, icon: LastPage, tooltip: "Ping" }