From 1cc77390df5e112856da131759d5f84ff24004f5 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sat, 1 Aug 2026 19:18:20 +0100 Subject: [PATCH] Refactor printer control actions and introduce new restart components - Removed outdated restart functions from ControlPrinter component to streamline code. - Added new RestartPrinter, RestartPrinterFirmware, and RestartMoonraker components for improved modularity and user interaction. - Updated Printer model to integrate new restart components into the action menu, enhancing user experience and maintainability. --- .../Production/Printers/ControlPrinter.jsx | 24 -------- .../Production/Printers/RestartMoonraker.jsx | 47 +++++++++++++++ .../Production/Printers/RestartPrinter.jsx | 47 +++++++++++++++ .../Printers/RestartPrinterFirmware.jsx | 47 +++++++++++++++ src/database/models/Printer.js | 58 +++++++++++++------ 5 files changed, 182 insertions(+), 41 deletions(-) create mode 100644 src/components/Dashboard/Production/Printers/RestartMoonraker.jsx create mode 100644 src/components/Dashboard/Production/Printers/RestartPrinter.jsx create mode 100644 src/components/Dashboard/Production/Printers/RestartPrinterFirmware.jsx diff --git a/src/components/Dashboard/Production/Printers/ControlPrinter.jsx b/src/components/Dashboard/Production/Printers/ControlPrinter.jsx index 98b1754..b495fd8 100644 --- a/src/components/Dashboard/Production/Printers/ControlPrinter.jsx +++ b/src/components/Dashboard/Production/Printers/ControlPrinter.jsx @@ -118,30 +118,6 @@ const ControlPrinter = ({ slicerIntegration = false }) => { objectFormRef?.current.handleUpdate() return true }, - restartFirmware: () => { - if (connected == true) { - sendObjectAction(printerId, 'printer', { - type: 'restartPrinterFirmware' - }) - } - return true - }, - restartMoonraker: () => { - if (connected == true) { - sendObjectAction(printerId, 'printer', { - type: 'restartMoonraker' - }) - } - return true - }, - restart: () => { - if (connected == true) { - sendObjectAction(printerId, 'printer', { - type: 'restartPrinter' - }) - } - return true - }, startQueue: () => { if (connected == true) { sendObjectAction(printerId, 'printer', { diff --git a/src/components/Dashboard/Production/Printers/RestartMoonraker.jsx b/src/components/Dashboard/Production/Printers/RestartMoonraker.jsx new file mode 100644 index 0000000..62a32b4 --- /dev/null +++ b/src/components/Dashboard/Production/Printers/RestartMoonraker.jsx @@ -0,0 +1,47 @@ +import { useState, useContext } from 'react' +import PropTypes from 'prop-types' +import { ApiServerContext } from '../../context/ApiServerContext' +import { message } from 'antd' +import MessageDialogView from '../../common/MessageDialogView.jsx' + +const RestartMoonraker = ({ onOk, objectData }) => { + const [restartLoading, setRestartLoading] = useState(false) + const { connected, sendObjectAction } = useContext(ApiServerContext) + + const handleRestart = async () => { + if (!connected) { + message.error('Not connected to server') + return + } + + setRestartLoading(true) + try { + sendObjectAction(objectData._id, 'printer', { + type: 'restartMoonraker' + }) + message.success('Moonraker restart initiated') + onOk() + } catch (error) { + console.error('Error restarting Moonraker:', error) + } finally { + setRestartLoading(false) + } + } + + return ( + + ) +} + +RestartMoonraker.propTypes = { + onOk: PropTypes.func.isRequired, + objectData: PropTypes.object +} + +export default RestartMoonraker diff --git a/src/components/Dashboard/Production/Printers/RestartPrinter.jsx b/src/components/Dashboard/Production/Printers/RestartPrinter.jsx new file mode 100644 index 0000000..d0bd2b5 --- /dev/null +++ b/src/components/Dashboard/Production/Printers/RestartPrinter.jsx @@ -0,0 +1,47 @@ +import { useState, useContext } from 'react' +import PropTypes from 'prop-types' +import { ApiServerContext } from '../../context/ApiServerContext' +import { message } from 'antd' +import MessageDialogView from '../../common/MessageDialogView.jsx' + +const RestartPrinter = ({ onOk, objectData }) => { + const [restartLoading, setRestartLoading] = useState(false) + const { connected, sendObjectAction } = useContext(ApiServerContext) + + const handleRestart = async () => { + if (!connected) { + message.error('Not connected to server') + return + } + + setRestartLoading(true) + try { + sendObjectAction(objectData._id, 'printer', { + type: 'restartPrinter' + }) + message.success('Printer restart initiated') + onOk() + } catch (error) { + console.error('Error restarting printer:', error) + } finally { + setRestartLoading(false) + } + } + + return ( + + ) +} + +RestartPrinter.propTypes = { + onOk: PropTypes.func.isRequired, + objectData: PropTypes.object +} + +export default RestartPrinter diff --git a/src/components/Dashboard/Production/Printers/RestartPrinterFirmware.jsx b/src/components/Dashboard/Production/Printers/RestartPrinterFirmware.jsx new file mode 100644 index 0000000..c00364d --- /dev/null +++ b/src/components/Dashboard/Production/Printers/RestartPrinterFirmware.jsx @@ -0,0 +1,47 @@ +import { useState, useContext } from 'react' +import PropTypes from 'prop-types' +import { ApiServerContext } from '../../context/ApiServerContext' +import { message } from 'antd' +import MessageDialogView from '../../common/MessageDialogView.jsx' + +const RestartPrinterFirmware = ({ onOk, objectData }) => { + const [restartLoading, setRestartLoading] = useState(false) + const { connected, sendObjectAction } = useContext(ApiServerContext) + + const handleRestart = async () => { + if (!connected) { + message.error('Not connected to server') + return + } + + setRestartLoading(true) + try { + sendObjectAction(objectData._id, 'printer', { + type: 'restartPrinterFirmware' + }) + message.success('Printer firmware restart initiated') + onOk() + } catch (error) { + console.error('Error restarting printer firmware:', error) + } finally { + setRestartLoading(false) + } + } + + return ( + + ) +} + +RestartPrinterFirmware.propTypes = { + onOk: PropTypes.func.isRequired, + objectData: PropTypes.object +} + +export default RestartPrinterFirmware diff --git a/src/database/models/Printer.js b/src/database/models/Printer.js index 58abab9..7de9ffc 100644 --- a/src/database/models/Printer.js +++ b/src/database/models/Printer.js @@ -4,13 +4,27 @@ const PrinterInfo = lazy( () => import('../../components/Dashboard/Production/Printers/PrinterInfo') ) const NewPrinterProfile = lazy( - () => import('../../components/Dashboard/Production/PrinterProfiles/NewPrinterProfile') + () => + import('../../components/Dashboard/Production/PrinterProfiles/NewPrinterProfile') ) const LoadFilamentStock = lazy( - () => import('../../components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock') + () => + import('../../components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock') ) const UnloadFilamentStock = lazy( - () => import('../../components/Dashboard/Inventory/FilamentStocks/UnloadFilamentStock') + () => + import('../../components/Dashboard/Inventory/FilamentStocks/UnloadFilamentStock') +) +const RestartPrinter = lazy( + () => import('../../components/Dashboard/Production/Printers/RestartPrinter') +) +const RestartPrinterFirmware = lazy( + () => + import('../../components/Dashboard/Production/Printers/RestartPrinterFirmware') +) +const RestartMoonraker = lazy( + () => + import('../../components/Dashboard/Production/Printers/RestartMoonraker') ) import PrinterIcon from '../../components/Icons/PrinterIcon' import InfoCircleIcon from '../../components/Icons/InfoCircleIcon' @@ -89,7 +103,11 @@ export const Printer = { label: 'New Printer Profile', icon: PlusIcon, content: (objectData, { onOk } = {}) => { - return createElement(NewPrinterProfile, { defaultValues: { printer: objectData }, onOk, reset: true }) + return createElement(NewPrinterProfile, { + defaultValues: { printer: objectData }, + onOk, + reset: true + }) } }, { type: 'divider' }, @@ -103,34 +121,46 @@ export const Printer = { children: [ { name: 'restart', + type: 'modal', + modalWidth: 520, + modalCentered: true, label: 'Restart', icon: ReloadIcon, disabled: (objectData) => { return objectData?.online == false }, - url: (_id) => - `/dashboard/production/printers/control?printerId=${_id}&action=restart` + content: (objectData, { onOk } = {}) => { + return createElement(RestartPrinter, { objectData, onOk }) + } }, { name: 'restartFirmware', + type: 'modal', + modalWidth: 520, + modalCentered: true, label: 'Restart Firmware', icon: ReloadIcon, disabled: (objectData) => { return objectData?.online == false }, - url: (_id) => - `/dashboard/production/printers/control?printerId=${_id}&action=restartFirmware` + content: (objectData, { onOk } = {}) => { + return createElement(RestartPrinterFirmware, { objectData, onOk }) + } }, { type: 'divider' }, { name: 'restartMoonraker', + type: 'modal', + modalWidth: 520, + modalCentered: true, label: 'Restart Moonraker', icon: ReloadIcon, disabled: (objectData) => { return objectData?.online == false }, - url: (_id) => - `/dashboard/production/printers/control?printerId=${_id}&action=restartMoonraker` + content: (objectData, { onOk } = {}) => { + return createElement(RestartMoonraker, { objectData, onOk }) + } } ] }, @@ -276,13 +306,7 @@ export const Printer = { 'createdAt', 'updatedAt' ], - sorters: [ - 'name', - 'state', - 'connectedAt', - 'createdAt', - 'updatedAt' - ], + sorters: ['name', 'state', 'connectedAt', 'createdAt', 'updatedAt'], group: ['tags'], properties: [ {