diff --git a/src/components/Dashboard/common/DocumentPrintButton.jsx b/src/components/Dashboard/common/DocumentPrintButton.jsx new file mode 100644 index 0000000..0aac8a3 --- /dev/null +++ b/src/components/Dashboard/common/DocumentPrintButton.jsx @@ -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: , + onClick: () => handleTemplateSelect(template) + })) + + // If no templates available, show disabled state + if (documentTemplates.length === 0 && !loading) { + return ( +