refactor
parent
896f56b3dd
commit
385f3522ad
|
@ -0,0 +1,53 @@
|
||||||
|
import React, { useState } from "react";
|
||||||
|
import PropTypes from "prop-types";
|
||||||
|
import MachineItem from "./MachineItem";
|
||||||
|
import * as api from "../api";
|
||||||
|
import { useToast } from "../../../hooks";
|
||||||
|
import { PowerSettingsNew, LastPage } from "@material-ui/icons";
|
||||||
|
|
||||||
|
const MachineContainer = ({ machine }) => {
|
||||||
|
const { success, error } = useToast();
|
||||||
|
|
||||||
|
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: wakeMachine,
|
||||||
|
icon: PowerSettingsNew,
|
||||||
|
tooltip: "Wake"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "ping",
|
||||||
|
effect: pingMachine,
|
||||||
|
icon: LastPage,
|
||||||
|
tooltip: "Ping"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
return <MachineItem machine={machine} actions={actions} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
MachineContainer.propTypes = {
|
||||||
|
machine: PropTypes.object.isRequired
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MachineContainer;
|
|
@ -5,69 +5,24 @@ import {
|
||||||
} from "../../../state/ApplicationContexts";
|
} from "../../../state/ApplicationContexts";
|
||||||
import * as api from "../api";
|
import * as api from "../api";
|
||||||
import MachinesList from "./MachinesList";
|
import MachinesList from "./MachinesList";
|
||||||
import { PowerSettingsNew, LastPage } from "@material-ui/icons";
|
|
||||||
import { useToast } from "../../../hooks";
|
|
||||||
|
|
||||||
const MachinesContainer = () => {
|
const MachinesContainer = () => {
|
||||||
const state = useContext(ApplicationStateContext);
|
const state = useContext(ApplicationStateContext);
|
||||||
const dispatchActions = useContext(ApplicationDispatchContext);
|
const dispatchActions = useContext(ApplicationDispatchContext);
|
||||||
|
|
||||||
const { success, error } = useToast();
|
|
||||||
|
|
||||||
const handleReadMachines = useCallback(async () => {
|
const handleReadMachines = useCallback(async () => {
|
||||||
const machines = await api.readMachines();
|
const machines = await api.readMachines();
|
||||||
const data = Object.assign(machines, { loaded: true });
|
const data = Object.assign(machines, { loaded: true });
|
||||||
dispatchActions.onNetworkChange("machines", data);
|
dispatchActions.onNetworkChange("machines", data);
|
||||||
}, [dispatchActions]);
|
}, [dispatchActions]);
|
||||||
|
|
||||||
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: wakeMachine,
|
|
||||||
icon: PowerSettingsNew,
|
|
||||||
tooltip: "Wake"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "ping",
|
|
||||||
effect: pingMachine,
|
|
||||||
icon: LastPage,
|
|
||||||
tooltip: "Ping"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!state.network.machines.loaded) {
|
if (!state.network.machines.loaded) {
|
||||||
handleReadMachines();
|
handleReadMachines();
|
||||||
}
|
}
|
||||||
}, [handleReadMachines, state.network.machines.loaded]);
|
}, [handleReadMachines, state.network.machines.loaded]);
|
||||||
|
|
||||||
return (
|
return <MachinesList dense={true} machines={state.network.machines} />;
|
||||||
<MachinesList
|
|
||||||
dense={true}
|
|
||||||
machines={state.network.machines}
|
|
||||||
actions={actions}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default MachinesContainer;
|
export default MachinesContainer;
|
||||||
|
|
|
@ -9,7 +9,7 @@ import {
|
||||||
TableRow
|
TableRow
|
||||||
} from "@material-ui/core";
|
} from "@material-ui/core";
|
||||||
import Paper from "@material-ui/core/Paper";
|
import Paper from "@material-ui/core/Paper";
|
||||||
import MachineItem from "./MachineItem";
|
import MachineContainer from "./MachineContainer";
|
||||||
|
|
||||||
const MachinesList = ({ dense, machines, actions }) => {
|
const MachinesList = ({ dense, machines, actions }) => {
|
||||||
return (
|
return (
|
||||||
|
@ -26,10 +26,9 @@ const MachinesList = ({ dense, machines, actions }) => {
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{machines.map(machine => (
|
{machines.map(machine => (
|
||||||
<MachineItem
|
<MachineContainer
|
||||||
key={`machine-${machine.machineId}`}
|
key={`machine-${machine.machineId}`}
|
||||||
machine={machine}
|
machine={machine}
|
||||||
actions={actions}
|
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
|
@ -40,8 +39,7 @@ const MachinesList = ({ dense, machines, actions }) => {
|
||||||
|
|
||||||
MachinesList.propTypes = {
|
MachinesList.propTypes = {
|
||||||
dense: PropTypes.bool.isRequired,
|
dense: PropTypes.bool.isRequired,
|
||||||
machines: PropTypes.array.isRequired,
|
machines: PropTypes.array.isRequired
|
||||||
actions: PropTypes.array.isRequired
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default MachinesList;
|
export default MachinesList;
|
||||||
|
|
Loading…
Reference in New Issue