From 1ed2ff01ee5ad89edd3742d0d19c36f667ac6c6b Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 17 Apr 2021 18:45:56 +0300 Subject: [PATCH] logs --- .../components/{MachineItem.js => Machine.js} | 11 +++--- .../machines/components/MachineContainer.js | 11 ++++-- .../machines/components/MachineLog.js | 36 ++++++++----------- 3 files changed, 29 insertions(+), 29 deletions(-) rename src/features/machines/components/{MachineItem.js => Machine.js} (92%) diff --git a/src/features/machines/components/MachineItem.js b/src/features/machines/components/Machine.js similarity index 92% rename from src/features/machines/components/MachineItem.js rename to src/features/machines/components/Machine.js index 1bc8a8a..eec9bca 100644 --- a/src/features/machines/components/MachineItem.js +++ b/src/features/machines/components/Machine.js @@ -20,7 +20,7 @@ const useRowStyles = makeStyles({ } }); -const MachineItem = ({ machine, actions }) => { +const Machine = ({ machine, actions, logs }) => { const [open, setOpen] = React.useState(false); const classes = useRowStyles(); const { t } = useTranslation(); @@ -67,7 +67,7 @@ const MachineItem = ({ machine, actions }) => { - + @@ -75,7 +75,7 @@ const MachineItem = ({ machine, actions }) => { ); }; -MachineItem.propTypes = { +Machine.propTypes = { machine: PropTypes.shape({ machineId: PropTypes.number.isRequired, machineName: PropTypes.string.isRequired, @@ -84,7 +84,8 @@ MachineItem.propTypes = { iPv4Address: PropTypes.string, description: PropTypes.string }).isRequired, - actions: PropTypes.array.isRequired + actions: PropTypes.array.isRequired, + logs: PropTypes.array.isRequired }; -export default MachineItem; +export default Machine; diff --git a/src/features/machines/components/MachineContainer.js b/src/features/machines/components/MachineContainer.js index bb5e723..5ee7230 100644 --- a/src/features/machines/components/MachineContainer.js +++ b/src/features/machines/components/MachineContainer.js @@ -1,15 +1,21 @@ import React, { useState } from "react"; import PropTypes from "prop-types"; -import MachineItem from "./MachineItem"; +import Machine from "./Machine"; import * as api from "../api"; import { useToast } from "../../../hooks"; import { PowerSettingsNew, LastPage } from "@material-ui/icons"; const MachineContainer = ({ machine }) => { + const [logs, setLogs] = useState([]); const { success, error } = useToast(); + const addLog = text => { + setLogs(prev => [...prev, text]); + }; + const wakeMachine = machine => async () => { const result = await api.wakeMachine(machine.macAddress); + addLog(`Success: ${result.success}. Status: ${result.status}`); if (result.success) { success(result.status); } else { @@ -21,6 +27,7 @@ const MachineContainer = ({ machine }) => { const result = await api.pingMachine( machine.iPv4Address || machine.machineName ); + addLog(`Success: ${result.success}. Status: ${result.status}`); if (result.success) { success(result.status); } else { @@ -43,7 +50,7 @@ const MachineContainer = ({ machine }) => { } ]; - return ; + return ; }; MachineContainer.propTypes = { diff --git a/src/features/machines/components/MachineLog.js b/src/features/machines/components/MachineLog.js index 5446807..5d7ddf1 100644 --- a/src/features/machines/components/MachineLog.js +++ b/src/features/machines/components/MachineLog.js @@ -1,22 +1,13 @@ -import React, { useState } from "react"; -import { Box, Button } from "@material-ui/core"; +import React, { useMemo } from "react"; +import PropTypes from "prop-types"; +import { Box } from "@material-ui/core"; import { LazyLog, ScrollFollow } from "react-lazylog"; -const MachineLog = () => { - const [logs, setLogs] = useState(["Welcome..."]); - const addLog = text => { - setLogs(prev => [...prev, text]); - }; - - const randomLog = () => { - addLog( - "Text messaging, or texting, is the act of composing and sending electronic messages" - ); - addLog( - "The term originally referred to messages sent using the Short Message Service (SMS). " - ); - }; - +const MachineLog = ({ logs }) => { + const displayLogs = useMemo( + () => (logs.length > 0 ? logs.join("\n") : "..."), + [logs] + ); return (
@@ -24,9 +15,9 @@ const MachineLog = () => { startFollowing={true} render={({ follow, onScroll }) => ( { )} />
-
); }; +MachineLog.propTypes = { + logs: PropTypes.array.isRequired +}; + export default MachineLog;