Refactor DocumentPrinterInfo component to improve state management and enhance editing functionality. Introduce useRef for action handling and object form references, and update the rendering logic for better performance and user experience.

This commit is contained in:
Tom Butcher 2025-11-24 03:33:57 +00:00
parent 64c4d25982
commit 808d45273d

View File

@ -1,3 +1,4 @@
import { useRef, useState } from 'react'
import { useLocation } from 'react-router-dom' import { useLocation } from 'react-router-dom'
import { Space, Flex, Card } from 'antd' import { Space, Flex, Card } from 'antd'
import { LoadingOutlined } from '@ant-design/icons' import { LoadingOutlined } from '@ant-design/icons'
@ -25,6 +26,8 @@ log.setLevel(config.logLevel)
const DocumentPrinterInfo = () => { const DocumentPrinterInfo = () => {
const location = useLocation() const location = useLocation()
const objectFormRef = useRef(null)
const actionHandlerRef = useRef(null)
const documentPrinterId = new URLSearchParams(location.search).get( const documentPrinterId = new URLSearchParams(location.search).get(
'documentPrinterId' 'documentPrinterId'
) )
@ -32,52 +35,42 @@ const DocumentPrinterInfo = () => {
'DocumentPrinterInfo', 'DocumentPrinterInfo',
{ {
info: true, info: true,
stocks: true,
notes: true, notes: true,
auditLogs: true auditLogs: true
} }
) )
return ( const [objectFormState, setEditFormState] = useState({
<ObjectForm isEditing: false,
id={documentPrinterId} editLoading: false,
type='documentPrinter' formValid: false,
style={{ height: '100%' }} locked: false,
> loading: false,
{({ objectData: {}
loading, })
isEditing,
startEditing,
cancelEditing,
handleUpdate,
formValid,
objectData,
editLoading,
lock,
fetchObject
}) => {
// Define actions for ActionHandler
const actions = { const actions = {
reload: () => { reload: () => {
fetchObject() objectFormRef?.current.handleFetchObject()
return true return true
}, },
edit: () => { edit: () => {
startEditing() objectFormRef?.current.startEditing()
return false return false
}, },
cancelEdit: () => { cancelEdit: () => {
cancelEditing() objectFormRef?.current.cancelEditing()
return true return true
}, },
finishEdit: () => { finishEdit: () => {
handleUpdate() objectFormRef?.current.handleUpdate()
return true return true
} }
} }
return ( return (
<ActionHandler actions={actions} loading={loading}> <>
{({ callAction }) => (
<Flex <Flex
gap='large' gap='large'
vertical='true' vertical='true'
@ -92,17 +85,16 @@ const DocumentPrinterInfo = () => {
<ObjectActions <ObjectActions
type='documentPrinter' type='documentPrinter'
id={documentPrinterId} id={documentPrinterId}
disabled={loading} disabled={objectFormState.loading}
objectData={objectData} objectData={objectFormState.objectData}
/> />
<ViewButton <ViewButton
disabled={loading} disabled={objectFormState.loading}
items={[ items={[
{ {
key: 'info', key: 'info',
label: 'DocumentPrinter Information' label: 'DocumentPrinter Information'
}, },
{ key: 'stocks', label: 'DocumentPrinter Stocks' },
{ key: 'notes', label: 'Notes' }, { key: 'notes', label: 'Notes' },
{ key: 'auditLogs', label: 'Audit Logs' } { key: 'auditLogs', label: 'Audit Logs' }
]} ]}
@ -111,43 +103,57 @@ const DocumentPrinterInfo = () => {
/> />
<DocumentPrintButton <DocumentPrintButton
type='documentPrinter' type='documentPrinter'
objectData={objectData} objectData={objectFormState.objectData}
disabled={loading} disabled={objectFormState.loading}
/> />
</Space> </Space>
<LockIndicator lock={lock} /> <LockIndicator lock={objectFormState.lock} />
</Space> </Space>
<Space> <Space>
<EditButtons <EditButtons
isEditing={isEditing} isEditing={objectFormState.isEditing}
handleUpdate={() => { handleUpdate={() => {
callAction('finishEdit') actionHandlerRef.current.callAction('finishEdit')
}} }}
cancelEditing={() => { cancelEditing={() => {
callAction('cancelEdit') actionHandlerRef.current.callAction('cancelEdit')
}} }}
startEditing={() => { startEditing={() => {
callAction('edit') actionHandlerRef.current.callAction('edit')
}} }}
editLoading={editLoading} editLoading={objectFormState.editLoading}
formValid={formValid} formValid={objectFormState.formValid}
disabled={lock?.locked || loading} disabled={objectFormState.lock?.locked || objectFormState.loading}
loading={editLoading} loading={objectFormState.editLoading}
/> />
</Space> </Space>
</Flex> </Flex>
<div style={{ height: '100%', overflowY: 'scroll' }}> <div style={{ height: '100%', overflowY: 'scroll' }}>
<Flex vertical gap={'large'}> <Flex vertical gap={'large'}>
<ActionHandler
actions={actions}
loading={objectFormState.loading}
ref={actionHandlerRef}
>
<InfoCollapse <InfoCollapse
title='Document Printer Information' title='Document Printer Information'
icon={<InfoCircleIcon />} icon={<InfoCircleIcon />}
active={collapseState.info} active={collapseState.info}
onToggle={(expanded) => onToggle={(expanded) => updateCollapseState('info', expanded)}
updateCollapseState('info', expanded)
}
collapseKey='info' collapseKey='info'
> >
<ObjectForm
id={documentPrinterId}
type='documentPrinter'
style={{ height: '100%' }}
ref={objectFormRef}
onStateChange={(state) => {
setEditFormState((prev) => ({ ...prev, ...state }))
}}
>
{({ loading, isEditing, objectData }) => {
return (
<ObjectInfo <ObjectInfo
loading={loading} loading={loading}
indicator={<LoadingOutlined />} indicator={<LoadingOutlined />}
@ -155,22 +161,21 @@ const DocumentPrinterInfo = () => {
type='documentPrinter' type='documentPrinter'
objectData={objectData} objectData={objectData}
/> />
)
}}
</ObjectForm>
</InfoCollapse> </InfoCollapse>
</ActionHandler>
<InfoCollapse <InfoCollapse
title='Notes' title='Notes'
icon={<NoteIcon />} icon={<NoteIcon />}
active={collapseState.notes} active={collapseState.notes}
onToggle={(expanded) => onToggle={(expanded) => updateCollapseState('notes', expanded)}
updateCollapseState('notes', expanded)
}
collapseKey='notes' collapseKey='notes'
> >
<Card> <Card>
<NotesPanel <NotesPanel _id={documentPrinterId} type='documentPrinter' />
_id={documentPrinterId}
type='documentPrinter'
/>
</Card> </Card>
</InfoCollapse> </InfoCollapse>
@ -183,7 +188,7 @@ const DocumentPrinterInfo = () => {
} }
collapseKey='auditLogs' collapseKey='auditLogs'
> >
{loading ? ( {objectFormState.loading ? (
<InfoCollapsePlaceholder /> <InfoCollapsePlaceholder />
) : ( ) : (
<ObjectTable <ObjectTable
@ -196,11 +201,7 @@ const DocumentPrinterInfo = () => {
</Flex> </Flex>
</div> </div>
</Flex> </Flex>
)} </>
</ActionHandler>
)
}}
</ObjectForm>
) )
} }