import { useState, useContext, useEffect } from 'react' import { Form, Flex, Alert } from 'antd' import PropTypes from 'prop-types' import ObjectSelect from '../../common/ObjectSelect' import PrinterTemperaturePanel from '../../common/PrinterTemperaturePanel' import WizardView from '../../common/WizardView' import { ApiServerContext } from '../../context/ApiServerContext' import { LoadingOutlined } from '@ant-design/icons' const UnloadFilamentStock = ({ onOk, reset, printer = null }) => { const { connected, subscribeToObjectEvent, sendObjectAction } = useContext(ApiServerContext) UnloadFilamentStock.propTypes = { onOk: PropTypes.func.isRequired, reset: PropTypes.bool.isRequired, printer: PropTypes.object } const initialUnloadFilamentStockForm = { printer: printer } const [unloadFilamentStockLoading, setUnloadFilamentStockLoading] = useState(false) const [formValid, setFormValid] = useState(false) const [currentTemperature, setCurrentTemperature] = useState(-1) const [targetTemperature, setTargetTemperature] = useState(0) const [filamentSensorDetected, setFilamentSensorDetected] = useState(true) const [unloadFilamentStockForm] = Form.useForm() const [unloadFilamentStockFormValues, setUnloadFilamentStockFormValues] = useState(initialUnloadFilamentStockForm) useEffect(() => { 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 filamentStockEventUnsubscribe = subscribeToObjectEvent( printer._id, 'printer', 'filamentSensor', (event) => { setFilamentSensorDetected(event.data.detected) } ) return () => { if (temperatureEventUnsubscribe) temperatureEventUnsubscribe() if (filamentStockEventUnsubscribe) filamentStockEventUnsubscribe() } } }, [printer?._id, connected, subscribeToObjectEvent]) useEffect(() => { if (reset) { unloadFilamentStockForm.resetFields() } }, [reset, unloadFilamentStockForm]) useEffect(() => { unloadFilamentStockForm .validateFields({ validateOnly: true }) .then(() => { // Only enable next if we have a printer selected, we're not loading, and we've reached target temperature setFormValid( Boolean(unloadFilamentStockFormValues.printer) && !unloadFilamentStockLoading && currentTemperature + 1 > targetTemperature && targetTemperature != 0 ) }) .catch(() => setFormValid(false)) }, [ unloadFilamentStockForm, unloadFilamentStockFormValues, unloadFilamentStockLoading, currentTemperature, targetTemperature ]) const handleUnloadFilamentStock = async () => { setUnloadFilamentStockLoading(true) // Send G-code to retract the filament await sendObjectAction( unloadFilamentStockFormValues.printer._id, 'printer', { type: 'unloadFilamentStock' } ) //setUnloadFilamentStockLoading(false) } useEffect(() => { if (unloadFilamentStockLoading == true && filamentSensorDetected == false) { setUnloadFilamentStockLoading(false) onOk() } }, [unloadFilamentStockLoading, filamentSensorDetected, onOk]) const steps = [ { title: 'Preheat', key: 'preHeat', content: ( {unloadFilamentStockLoading == false ? ( <> {targetTemperature == 0 ? ( ) : null} {targetTemperature > 0 && currentTemperature < targetTemperature ? ( } /> ) : null} {targetTemperature > 0 && currentTemperature + 1 > targetTemperature && filamentSensorDetected ? ( ) : null} ) : ( } /> )} {unloadFilamentStockFormValues.printer?._id ? ( ) : null} ) } ] return (
setUnloadFilamentStockFormValues((prevValues) => ({ ...prevValues, ...changedValues })) } initialValues={initialUnloadFilamentStockForm} > unloadFilamentStockForm.submit()} formValid={formValid} loading={unloadFilamentStockLoading} submitText={unloadFilamentStockLoading ? 'Unloading...' : 'Unload'} /> ) } export default UnloadFilamentStock