call server

master
Tudor Stanciu 2021-04-17 03:21:43 +03:00
parent 964ceb9372
commit e9c7bacb06
3 changed files with 43 additions and 9 deletions

1
.env
View File

@ -2,3 +2,4 @@
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
REACT_APP_NETWORK_RESURRECTOR_SERVER_URL=http://localhost:5062

View File

@ -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 };

View File

@ -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"
}