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 getRefId = (value) => { if (!value) return null if (typeof value === 'string') return value if (typeof value._id === 'string') return value._id if (typeof value._id === 'object' && value._id?._id) return value._id._id return null } const CancelSubJob = ({ onOk, objectData }) => { const [cancelLoading, setCancelLoading] = useState(false) const { connected, sendObjectAction } = useContext(ApiServerContext) const handleCancel = async () => { if (!connected) { message.error('Not connected to server') return } const printerId = getRefId(objectData?.printer) const subJobId = getRefId(objectData) if (!printerId || !subJobId) { message.error('Sub job is not assigned to a printer') return } setCancelLoading(true) try { sendObjectAction(printerId, 'printer', { type: 'cancelSubJob', data: { _id: subJobId } }) message.success('Sub job cancel initiated') onOk() } catch (error) { console.error('Error cancelling sub job:', error) } finally { setCancelLoading(false) } } const subJobLabel = objectData?.name || objectData?._reference || objectData?._id return ( ) } CancelSubJob.propTypes = { onOk: PropTypes.func.isRequired, objectData: PropTypes.object } export default CancelSubJob