Compare commits
2 Commits
edf5ffc244
...
3f9da22b58
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f9da22b58 | |||
| 88674f0d6e |
@ -1,5 +1,5 @@
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { useState, useEffect, useContext } from 'react'
|
import { useState, useEffect, useContext, useCallback, useMemo, useRef } from 'react'
|
||||||
// import { getModelByName } from '../../../database/ObjectModels'
|
// import { getModelByName } from '../../../database/ObjectModels'
|
||||||
import DocumentPrinterIcon from '../../Icons/DocumentPrinterIcon'
|
import DocumentPrinterIcon from '../../Icons/DocumentPrinterIcon'
|
||||||
import { Button, Dropdown, Modal } from 'antd'
|
import { Button, Dropdown, Modal } from 'antd'
|
||||||
@ -15,6 +15,9 @@ const DocumentPrintButton = ({
|
|||||||
...buttonProps
|
...buttonProps
|
||||||
}) => {
|
}) => {
|
||||||
const { fetchObjects } = useContext(ApiServerContext)
|
const { fetchObjects } = useContext(ApiServerContext)
|
||||||
|
const fetchObjectsRef = useRef(fetchObjects)
|
||||||
|
fetchObjectsRef.current = fetchObjects
|
||||||
|
|
||||||
const [documentTemplates, setDocumentTemplates] = useState([])
|
const [documentTemplates, setDocumentTemplates] = useState([])
|
||||||
const [currentDocumentTemplate, setCurrentDocumentTemplate] = useState(null)
|
const [currentDocumentTemplate, setCurrentDocumentTemplate] = useState(null)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
@ -25,34 +28,40 @@ const DocumentPrintButton = ({
|
|||||||
// Get the model by name
|
// Get the model by name
|
||||||
//const model = getModelByName(type)
|
//const model = getModelByName(type)
|
||||||
|
|
||||||
// Fetch document templates when component mounts or type changes
|
const loadDocumentTemplates = useCallback(async () => {
|
||||||
useEffect(() => {
|
if (!type || token == null) return
|
||||||
const loadDocumentTemplates = async () => {
|
|
||||||
if (!type || token == null) return
|
|
||||||
|
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
try {
|
try {
|
||||||
const result = await fetchObjects('documentTemplate', {
|
const result = await fetchObjectsRef.current('documentTemplate', {
|
||||||
filter: {
|
filter: {
|
||||||
objectType: type,
|
objectType: type,
|
||||||
global: false,
|
global: false,
|
||||||
active: true
|
active: true
|
||||||
},
|
},
|
||||||
limit: 100 // Get more templates to show in dropdown
|
limit: 100 // Get more templates to show in dropdown
|
||||||
})
|
})
|
||||||
|
|
||||||
if (result && result.data) {
|
if (result && result.data) {
|
||||||
setDocumentTemplates(result.data)
|
setDocumentTemplates(result.data)
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching document templates:', error)
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching document templates:', error)
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
}, [type, token])
|
||||||
|
|
||||||
|
// Stable key from objectData._id (excludes _isEditing so toggling edit mode doesn't trigger template reload)
|
||||||
|
const objectKey = useMemo(() => {
|
||||||
|
if (!objectData) return null
|
||||||
|
return objectData._id ?? objectData.id ?? null
|
||||||
|
}, [objectData])
|
||||||
|
|
||||||
|
// Fetch document templates when component mounts or objectData, type or token changes
|
||||||
|
useEffect(() => {
|
||||||
loadDocumentTemplates()
|
loadDocumentTemplates()
|
||||||
}, [type, fetchObjects, token])
|
}, [objectKey, token, type, loadDocumentTemplates])
|
||||||
|
|
||||||
// Handle template selection
|
// Handle template selection
|
||||||
const handleTemplateSelect = (template) => {
|
const handleTemplateSelect = (template) => {
|
||||||
|
|||||||
@ -29,7 +29,7 @@ const FilePreview = ({ file, style = {} }) => {
|
|||||||
}
|
}
|
||||||
setFileObjectUrl(objectUrl)
|
setFileObjectUrl(objectUrl)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}, [file, fetchFileContent])
|
}, [file, fetchFileContent, error])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (file?.type && token != null) {
|
if (file?.type && token != null) {
|
||||||
|
|||||||
@ -57,7 +57,7 @@ const FileUpload = ({
|
|||||||
: !currentFiles
|
: !currentFiles
|
||||||
setHasNoItems(noItems)
|
setHasNoItems(noItems)
|
||||||
onChange(currentFiles)
|
onChange(currentFiles)
|
||||||
}, [currentFiles, multiple])
|
}, [currentFiles, multiple, onChange])
|
||||||
|
|
||||||
const handleFileUpload = async (file) => {
|
const handleFileUpload = async (file) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -231,7 +231,8 @@ const AuthProvider = ({ children }) => {
|
|||||||
return () => {
|
return () => {
|
||||||
cancelled = true
|
cancelled = true
|
||||||
}
|
}
|
||||||
}, [isElectron, getAuthSession, clearPersistedSession, getUserInfo])
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- run only on mount to load persisted session; deps are stable in behavior
|
||||||
|
}, [])
|
||||||
|
|
||||||
// Set up cookie synchronization between tabs
|
// Set up cookie synchronization between tabs
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user