From 7ac56cc69cd74fbc33c7482182b103ab55a0606a Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 24 Nov 2025 03:33:32 +0000 Subject: [PATCH] Refactor NewDocumentJob and NewDocumentTemplate components to utilize WizardView for improved user experience, implement file download functionality with dynamic filename generation, and enhance form handling with context integration. --- .../DocumentJobs/NewDocumentJob.jsx | 72 +++++++++++++++++-- .../DocumentTemplates/NewDocumentTemplate.jsx | 60 ++++------------ 2 files changed, 79 insertions(+), 53 deletions(-) diff --git a/src/components/Dashboard/Management/DocumentJobs/NewDocumentJob.jsx b/src/components/Dashboard/Management/DocumentJobs/NewDocumentJob.jsx index 4e35bcc..94b5704 100644 --- a/src/components/Dashboard/Management/DocumentJobs/NewDocumentJob.jsx +++ b/src/components/Dashboard/Management/DocumentJobs/NewDocumentJob.jsx @@ -3,12 +3,26 @@ 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 = [ @@ -28,6 +42,10 @@ const NewDocumentJob = ({ onOk, defaultValues = {} }) => { ) } ] + const fileName = + formatFileName( + objectData?.name + ' ' + dayjs().format('YYYY-MM-DD HH:mm:ss') + ) || 'document' return ( { title={'Print Document'} formValid={formValid} loading={submitLoading} + disabled={downloading} + sideBarGrow={true} sideBar={ -
+
{ />
} - onSubmit={() => { - handleSubmit() - onOk() + 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' + } + ] + } + ]} /> ) }} diff --git a/src/components/Dashboard/Management/DocumentTemplates/NewDocumentTemplate.jsx b/src/components/Dashboard/Management/DocumentTemplates/NewDocumentTemplate.jsx index 5ead9f2..440d47f 100644 --- a/src/components/Dashboard/Management/DocumentTemplates/NewDocumentTemplate.jsx +++ b/src/components/Dashboard/Management/DocumentTemplates/NewDocumentTemplate.jsx @@ -1,19 +1,9 @@ import PropTypes from 'prop-types' -import { useState } from 'react' -import { useMediaQuery } from 'react-responsive' -import { Typography, Flex, Steps, Divider } from 'antd' - import ObjectInfo from '../../common/ObjectInfo' import NewObjectForm from '../../common/NewObjectForm' -import NewObjectButtons from '../../common/NewObjectButtons' - -const { Title } = Typography +import WizardView from '../../common/WizardView' const NewDocumentTemplate = ({ onOk }) => { - const [currentStep, setCurrentStep] = useState(0) - - const isMobile = useMediaQuery({ maxWidth: 768 }) - return ( { ) } ] + return ( - - {!isMobile && ( -
- -
- )} - - {!isMobile && ( - - )} - - - - New Document Template - -
- {steps[currentStep].content} -
- setCurrentStep((prev) => prev - 1)} - onNext={() => setCurrentStep((prev) => prev + 1)} - onSubmit={() => { - handleSubmit() - onOk() - }} - formValid={formValid} - submitLoading={submitLoading} - /> -
-
+ { + handleSubmit() + onOk() + }} + /> ) }}