Refactor MachineContainer: convert to TypeScript, implement SWR for data fetching, and update action handling

master^2
Tudor Stanciu 2024-11-16 01:29:18 +02:00
parent 253ee1953c
commit 7234b857a6
6 changed files with 66 additions and 39 deletions

View File

@ -66,9 +66,9 @@ const GridCell: React.FC<GridCellProps> = ({ label, value }) => {
type Props = { type Props = {
machine: models.Machine; machine: models.Machine;
actions: Array<any>; // Replace any with the actual type of the actions actions: Array<any>;
logs: Array<any>; // Replace any with the actual type of the logs logs: Array<any>;
addLog: () => void; // Replace with the actual function signature addLog: (message: string) => void;
}; };
const MachineAccordion: React.FC<Props> = ({ machine, actions, logs, addLog }) => { const MachineAccordion: React.FC<Props> = ({ machine, actions, logs, addLog }) => {

View File

@ -1,26 +1,31 @@
import React, { useState, useCallback } from "react"; import React, { useState, useCallback } from "react";
import PropTypes from "prop-types";
import MachineTableRow from "./MachineTableRow"; import MachineTableRow from "./MachineTableRow";
import MachineAccordion from "./MachineAccordion"; import MachineAccordion from "./MachineAccordion";
import { ViewModes } from "./ViewModeSelection"; import { ViewModes } from "./ViewModeSelection";
import { blip } from "../../../utils"; import { blip } from "../../../utils";
import { LastPage, RotateLeft, Launch, Stop } from "@mui/icons-material"; import { LastPage, RotateLeft, Launch, Stop } from "@mui/icons-material";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { routes, post } from "../../../utils/api"; import { routes, post, endpoints } from "../../../utils/api";
import { Machine, MachineRestarted, MachineShutdown, RestartMachine, ShutdownMachine } from "types";
import { Key, mutationFetcher, useSWRMutation } from "units/swr";
const MachineContainer = ({ machine, viewMode }) => { type Props = {
const [logs, setLogs] = useState([]); machine: Machine;
viewMode: string;
};
const MachineContainer: React.FC<Props> = ({ machine, viewMode }) => {
const [logs, setLogs] = useState<string[]>([]);
const { t } = useTranslation(); const { t } = useTranslation();
const addLog = useCallback( const addLog = useCallback(
text => { (text: string) => {
setLogs(prev => [...prev, text]); setLogs(prev => [...prev, text]);
}, },
[setLogs] [setLogs]
); );
const manageActionResponse = useCallback( const manageActionResponse = useCallback(
response => { (response: any) => {
addLog(`Success: ${response.success}. Status: ${response.status}`); addLog(`Success: ${response.success}. Status: ${response.status}`);
if (response.success) { if (response.success) {
blip.success(response.status); blip.success(response.status);
@ -31,8 +36,26 @@ const MachineContainer = ({ machine, viewMode }) => {
[addLog] [addLog]
); );
const { trigger: shutdownMachineTrigger } = useSWRMutation<MachineShutdown, Error, Key, ShutdownMachine>(
endpoints.network.machine.shutdown,
mutationFetcher<ShutdownMachine>,
{
onError: err => blip.error(err.message),
onSuccess: manageActionResponse
}
);
const { trigger: restartMachineTrigger } = useSWRMutation<MachineRestarted, Error, Key, RestartMachine>(
endpoints.network.machine.restart,
mutationFetcher<RestartMachine>,
{
onError: err => blip.error(err.message),
onSuccess: manageActionResponse
}
);
const pingMachine = useCallback( const pingMachine = useCallback(
async machine => { async (machine: Machine) => {
await post( await post(
routes.pingMachine, routes.pingMachine,
{ machineId: machine.machineId }, { machineId: machine.machineId },
@ -45,29 +68,13 @@ const MachineContainer = ({ machine, viewMode }) => {
); );
const shutdownMachine = useCallback( const shutdownMachine = useCallback(
async machine => { async (machine: Machine) => shutdownMachineTrigger({ machineId: machine.machineId, delay: 0, force: false }),
await post( [shutdownMachineTrigger]
routes.shutdownMachine,
{ machineId: machine.machineId, delay: 0, force: false },
{
onCompleted: manageActionResponse
}
);
},
[manageActionResponse]
); );
const restartMachine = useCallback( const restartMachine = useCallback(
async machine => { async (machine: Machine) => restartMachineTrigger({ machineId: machine.machineId, delay: 0, force: false }),
await post( [restartMachineTrigger]
routes.restartMachine,
{ machineId: machine.machineId, delay: 0, force: false },
{
onCompleted: manageActionResponse
}
);
},
[manageActionResponse]
); );
const actions = [ const actions = [
@ -115,9 +122,4 @@ const MachineContainer = ({ machine, viewMode }) => {
); );
}; };
MachineContainer.propTypes = {
machine: PropTypes.object.isRequired,
viewMode: PropTypes.string.isRequired
};
export default MachineContainer; export default MachineContainer;

View File

@ -0,0 +1,11 @@
export type ShutdownMachine = {
machineId: number;
delay?: number;
force?: boolean;
};
export type RestartMachine = {
machineId: number;
delay?: number;
force?: boolean;
};

View File

@ -0,0 +1,9 @@
export type MachineShutdown = {
success: boolean;
status: string;
};
export type MachineRestarted = {
success: boolean;
status: string;
};

View File

@ -1,6 +1,10 @@
import * as models from "./models"; import * as models from "./models";
import * as dtos from "./dtos"; import * as dtos from "./dtos";
import * as commands from "./commands";
import * as events from "./events";
export * from "./models"; export * from "./models";
export * from "./commands";
export * from "./events";
export { models, dtos }; export { models, dtos, commands, events };

View File

@ -12,13 +12,14 @@ const securityRoute = `${apiHost}/security`;
const routes = { const routes = {
wakeMachine: `${powerActionsRoute}/wake`, wakeMachine: `${powerActionsRoute}/wake`,
pingMachine: `${powerActionsRoute}/ping`, pingMachine: `${powerActionsRoute}/ping`,
shutdownMachine: `${powerActionsRoute}/shutdown`,
restartMachine: `${powerActionsRoute}/restart`,
network: { network: {
machines: `${networkRoute}/machines`, machines: `${networkRoute}/machines`,
machine: { machine: {
wake: `${powerActionsRoute}/wake`, wake: `${powerActionsRoute}/wake`,
ping: `${powerActionsRoute}/ping` ping: `${powerActionsRoute}/ping`,
shutdown: `${powerActionsRoute}/shutdown`,
restart: `${powerActionsRoute}/restart`
} }
}, },
system: { system: {