Implement DocumentPrintButton component for document template selection and printing functionality
This commit is contained in:
parent
5a656d43f1
commit
c5355c8cf2
136
src/components/Dashboard/common/DocumentPrintButton.jsx
Normal file
136
src/components/Dashboard/common/DocumentPrintButton.jsx
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
import PropTypes from 'prop-types'
|
||||||
|
import { useState, useEffect, useContext } from 'react'
|
||||||
|
// import { getModelByName } from '../../../database/ObjectModels'
|
||||||
|
import DocumentPrinterIcon from '../../Icons/DocumentPrinterIcon'
|
||||||
|
import { Button, Dropdown, Modal } from 'antd'
|
||||||
|
import { ApiServerContext } from '../context/ApiServerContext'
|
||||||
|
import DocumentTemplateIcon from '../../Icons/DocumentTemplateIcon'
|
||||||
|
import NewDocumentJob from '../Management/DocumentJobs/NewDocumentJob'
|
||||||
|
import { message } from 'antd'
|
||||||
|
import { AuthContext } from '../context/AuthContext'
|
||||||
|
|
||||||
|
const DocumentPrintButton = ({
|
||||||
|
type,
|
||||||
|
objectData,
|
||||||
|
disabled = false,
|
||||||
|
...buttonProps
|
||||||
|
}) => {
|
||||||
|
const { fetchObjects } = useContext(ApiServerContext)
|
||||||
|
const [documentTemplates, setDocumentTemplates] = useState([])
|
||||||
|
const [currentDocumentTemplate, setCurrentDocumentTemplate] = useState(null)
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [messageApi, contextHolder] = message.useMessage()
|
||||||
|
const [newDocumentJobOpen, setNewDocumentJobOpen] = useState(false)
|
||||||
|
|
||||||
|
const { token } = useContext(AuthContext)
|
||||||
|
|
||||||
|
// Get the model by name
|
||||||
|
//const model = getModelByName(type)
|
||||||
|
|
||||||
|
// Fetch document templates when component mounts or type changes
|
||||||
|
useEffect(() => {
|
||||||
|
const loadDocumentTemplates = async () => {
|
||||||
|
if (!type || token == null) return
|
||||||
|
|
||||||
|
setLoading(true)
|
||||||
|
try {
|
||||||
|
const result = await fetchObjects('documentTemplate', {
|
||||||
|
filter: {
|
||||||
|
objectType: type,
|
||||||
|
global: false,
|
||||||
|
active: true
|
||||||
|
},
|
||||||
|
limit: 100 // Get more templates to show in dropdown
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result && result.data) {
|
||||||
|
setDocumentTemplates(result.data)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching document templates:', error)
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadDocumentTemplates()
|
||||||
|
}, [type, fetchObjects, token])
|
||||||
|
|
||||||
|
// Handle template selection
|
||||||
|
const handleTemplateSelect = (template) => {
|
||||||
|
setCurrentDocumentTemplate(template)
|
||||||
|
setNewDocumentJobOpen(true)
|
||||||
|
// TODO: Implement the actual printing logic here
|
||||||
|
// This could open a print dialog, navigate to a print page, etc.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create dropdown menu items
|
||||||
|
const menuItems = documentTemplates.map((template) => ({
|
||||||
|
key: template._id,
|
||||||
|
label: template.name,
|
||||||
|
icon: <DocumentTemplateIcon />,
|
||||||
|
onClick: () => handleTemplateSelect(template)
|
||||||
|
}))
|
||||||
|
|
||||||
|
// If no templates available, show disabled state
|
||||||
|
if (documentTemplates.length === 0 && !loading) {
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
{...buttonProps}
|
||||||
|
icon={<DocumentPrinterIcon />}
|
||||||
|
disabled={true}
|
||||||
|
title='No document templates available for this object type'
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{contextHolder}
|
||||||
|
<Dropdown
|
||||||
|
menu={{
|
||||||
|
items: menuItems,
|
||||||
|
loading: loading
|
||||||
|
}}
|
||||||
|
trigger={['hover']}
|
||||||
|
disabled={disabled || loading}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
{...buttonProps}
|
||||||
|
icon={<DocumentPrinterIcon />}
|
||||||
|
disabled={disabled || loading}
|
||||||
|
loading={loading}
|
||||||
|
title={loading ? 'Loading templates...' : 'Print document'}
|
||||||
|
/>
|
||||||
|
</Dropdown>
|
||||||
|
<Modal
|
||||||
|
open={newDocumentJobOpen}
|
||||||
|
onCancel={() => setNewDocumentJobOpen(false)}
|
||||||
|
footer={null}
|
||||||
|
destroyOnHidden={true}
|
||||||
|
width={900}
|
||||||
|
>
|
||||||
|
<NewDocumentJob
|
||||||
|
onOk={() => {
|
||||||
|
setNewDocumentJobOpen(false)
|
||||||
|
messageApi.success('New document job created successfully.')
|
||||||
|
}}
|
||||||
|
reset={!newDocumentJobOpen}
|
||||||
|
defaultValues={{
|
||||||
|
objectType: type,
|
||||||
|
object: objectData,
|
||||||
|
documentTemplate: currentDocumentTemplate
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
DocumentPrintButton.propTypes = {
|
||||||
|
type: PropTypes.string.isRequired,
|
||||||
|
disabled: PropTypes.bool,
|
||||||
|
objectData: PropTypes.object.isRequired
|
||||||
|
}
|
||||||
|
|
||||||
|
export default DocumentPrintButton
|
||||||
Loading…
x
Reference in New Issue
Block a user