diff --git a/src/components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock.jsx b/src/components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock.jsx
index a003ba2..72e69dd 100644
--- a/src/components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock.jsx
+++ b/src/components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock.jsx
@@ -1,27 +1,14 @@
import { useState, useContext, useEffect } from 'react'
-import {
- Form,
- Button,
- Typography,
- Flex,
- Steps,
- Divider,
- Descriptions,
- Alert
-} from 'antd'
-import { useMediaQuery } from 'react-responsive'
+import { Form, Flex, Descriptions, Alert } from 'antd'
import PropTypes from 'prop-types'
-import { PrintServerContext } from '../../context/PrintServerContext'
-import FilamentStockSelect from '../../common/FilamentStockSelect'
-import PrinterSelect from '../../common/PrinterSelect'
-import FilamentStockDisplay from '../../common/FilamentStockDisplay'
import PrinterTemperaturePanel from '../../common/PrinterTemperaturePanel'
import { LoadingOutlined } from '@ant-design/icons'
-import PrinterState from '../../common/StateDisplay'
-
-const { Title } = Typography
+import ObjectSelect from '../../common/ObjectSelect'
+import ObjectDisplay from '../../common/ObjectDisplay'
+import WizardView from '../../common/WizardView'
+import { ApiServerContext } from '../../context/ApiServerContext'
const LoadFilamentStock = ({
onOk,
@@ -29,7 +16,8 @@ const LoadFilamentStock = ({
printer = null,
filamentStockLoaded = false
}) => {
- const isMobile = useMediaQuery({ maxWidth: 768 })
+ const { connected, subscribeToObjectEvent, sendObjectAction } =
+ useContext(ApiServerContext)
LoadFilamentStock.propTypes = {
onOk: PropTypes.func.isRequired,
@@ -38,8 +26,6 @@ const LoadFilamentStock = ({
filamentStockLoaded: PropTypes.bool
}
- const { printServer } = useContext(PrintServerContext)
-
const initialLoadFilamentStockForm = {
printer: printer,
filamentStock: null
@@ -47,8 +33,7 @@ const LoadFilamentStock = ({
const [loadFilamentStockLoading, setLoadFilamentStockLoading] =
useState(false)
- const [currentStep, setCurrentStep] = useState(0)
- const [nextEnabled, setNextEnabled] = useState(false)
+ const [formValid, setFormValid] = useState(false)
const [currentTemperature, setCurrentTemperature] = useState(-1)
const [targetTemperature, setTargetTemperature] = useState(0)
const [filamentSensorDetected, setFilamentSensorDetected] =
@@ -62,77 +47,69 @@ const LoadFilamentStock = ({
loadFilamentStockForm
)
- // Add websocket temperature monitoring
useEffect(() => {
- if (loadFilamentStockFormValues.printer) {
- const params = {
- printerId: loadFilamentStockFormValues.printer._id,
- objects: {
- extruder: null,
- 'filament_switch_sensor fsensor': null
+ if (printer?._id && connected) {
+ const temperatureEventUnsubscribe = subscribeToObjectEvent(
+ printer._id,
+ 'printer',
+ 'temperature',
+ (event) => {
+ if (event.data?.extruder?.current) {
+ setCurrentTemperature(event.data.extruder.current)
+ }
+ if (event.data?.extruder?.target) {
+ setTargetTemperature(event.data.extruder.target)
+ }
}
- }
-
- const notifyStatusUpdate = (statusUpdate) => {
- if (statusUpdate?.extruder?.temperature !== undefined) {
- setCurrentTemperature(statusUpdate.extruder.temperature)
+ )
+ const filamentStockEventUnsubscribe = subscribeToObjectEvent(
+ printer._id,
+ 'printer',
+ 'filamentSensor',
+ (event) => {
+ console.log('filamentSensor', event.data)
+ setFilamentSensorDetected(event.data.detected)
}
- if (statusUpdate?.extruder?.target !== undefined) {
- setTargetTemperature(statusUpdate.extruder.target)
- }
- if (
- statusUpdate?.['filament_switch_sensor fsensor']
- ?.filament_detected !== undefined
- ) {
- setFilamentSensorDetected(
- Boolean(
- statusUpdate['filament_switch_sensor fsensor'].filament_detected
- )
- )
- }
- }
-
- printServer.emit('printer.objects.subscribe', params)
- printServer.emit('printer.objects.query', params)
- printServer.on('notify_status_update', notifyStatusUpdate)
-
+ )
return () => {
- printServer.off('notify_status_update', notifyStatusUpdate)
- printServer.emit('printer.objects.unsubscribe', params)
+ if (temperatureEventUnsubscribe) temperatureEventUnsubscribe()
+ if (filamentStockEventUnsubscribe) filamentStockEventUnsubscribe()
}
}
- }, [printServer, loadFilamentStockFormValues.printer])
+ }, [printer?._id, connected])
useEffect(() => {
+ // Validate form fields
loadFilamentStockForm
.validateFields({
validateOnly: true
})
- .then(() => setNextEnabled(filamentSensorDetected))
- .catch(() => setNextEnabled(false))
+ .then(() => {
+ const hasPrinter = Boolean(loadFilamentStockFormValues.printer)
+ const hasFilamentStock = Boolean(
+ loadFilamentStockFormValues.filamentStock
+ )
+
+ // Step 0 (Preheat): needs printer + filament detected + (temp reached or no temp set)
+ const preheatReady =
+ hasPrinter &&
+ filamentSensorDetected &&
+ (targetTemperature === 0 || currentTemperature >= targetTemperature)
+
+ // Step 1+ (Required/Summary): needs filamentStock selected
+ // Form is valid if preheat is ready (for step 0) OR filamentStock is selected (for step 1+)
+ // This allows progression: step 0 can proceed when preheat is ready,
+ // and step 1+ can proceed when filamentStock is selected
+ setFormValid(preheatReady || (hasPrinter && hasFilamentStock))
+ })
+ .catch(() => setFormValid(false))
}, [
loadFilamentStockForm,
loadFilamentStockFormUpdateValues,
- filamentSensorDetected
- ])
-
- useEffect(() => {
- if (
- filamentSensorDetected == true &&
- currentTemperature >= targetTemperature
- ) {
- setNextEnabled(filamentSensorDetected)
- if (currentStep == 0) {
- setCurrentStep(1)
- }
- } else if (filamentSensorDetected == false) {
- setCurrentStep(0)
- }
- }, [
+ loadFilamentStockFormValues,
filamentSensorDetected,
- targetTemperature,
currentTemperature,
- currentStep
+ targetTemperature
])
const summaryItems = [
@@ -140,8 +117,9 @@ const LoadFilamentStock = ({
key: 'filamentStock',
label: 'Stock',
children: loadFilamentStockFormValues.filamentStock ? (
-