import PropTypes from 'prop-types' import ObjectInfo from '../../common/ObjectInfo' import NewObjectForm from '../../common/NewObjectForm' import WizardView from '../../common/WizardView' import TemplatePreview from '../../common/TemplatePreview' import { ApiServerContext } from '../../context/ApiServerContext' import { useContext, useRef, useState } from 'react' import dayjs from 'dayjs' const NewDocumentJob = ({ onOk, defaultValues = {} }) => { const { sendObjectAction, downloadTemplatePDF, formatFileName } = useContext(ApiServerContext) const [downloading, setDownloading] = useState(false) // Capture initial default values so later prop changes don't re-initialize the form const defaultValuesRef = useRef({ objectType: 'documentJob', ...defaultValues, saveToFile: false }) return ( {({ handleSubmit, submitLoading, objectData, formValid }) => { const steps = [ { title: 'Required', key: 'required', content: ( ) } ] const fileName = formatFileName( objectData?.name + ' ' + dayjs().format('YYYY-MM-DD HH:mm:ss') ) || 'document' return ( { console.log(message) }} /> } onSubmit={async () => { const newDocumentJob = await handleSubmit() if (newDocumentJob.sendToFile == true) { sendObjectAction(newDocumentJob._id, 'documentJob', { type: 'print', data: newDocumentJob }) } if (onOk) { onOk() } }} actions={[ { label: 'Download', steps: ['required'], loading: downloading == true, disabled: downloading == true || submitLoading == true, children: [ { label: 'PDF', key: 'pdf', onClick: () => { setDownloading(true) downloadTemplatePDF( objectData.documentTemplate._id, objectData.documentTemplate.content, objectData.object, fileName, () => { setDownloading(false) } ) } }, { label: 'PNG', key: 'png' }, { label: 'JPEG', key: 'jpeg' } ] } ]} /> ) }} ) } NewDocumentJob.propTypes = { onOk: PropTypes.func.isRequired, reset: PropTypes.bool, defaultValues: PropTypes.object } export default NewDocumentJob