2025-10-22 14:58:27 +00:00
|
|
|
import { EditingFormItemDto, GridDto, PlatformEditorTypes } from "@/proxy/form/models"
|
|
|
|
|
import { useLocalization } from "@/utils/hooks/useLocalization"
|
|
|
|
|
import { usePermission } from "@/utils/hooks/usePermission"
|
|
|
|
|
import { usePWA } from "@/utils/hooks/usePWA"
|
|
|
|
|
import CustomStore from "devextreme/data/custom_store"
|
|
|
|
|
import { useMemo, useRef, useState } from "react"
|
|
|
|
|
import { useNavigate } from "react-router-dom"
|
|
|
|
|
import { Form as FormDx } from 'devextreme-react/form'
|
|
|
|
|
import { GroupItem } from 'devextreme/ui/form'
|
|
|
|
|
import { PermissionResults, SimpleItemWithColData } from "../form/types"
|
|
|
|
|
import { captionize } from 'devextreme/core/utils/inflector'
|
|
|
|
|
import { ROUTES_ENUM } from "@/routes/route.constant"
|
|
|
|
|
import FormButtons from "../form/FormButtons"
|
|
|
|
|
import FormDevExpress from "../form/FormDevExpress"
|
|
|
|
|
|
|
|
|
|
const CardItem = ({
|
|
|
|
|
isSubForm,
|
|
|
|
|
row,
|
|
|
|
|
gridDto,
|
|
|
|
|
listFormCode,
|
|
|
|
|
dataSource,
|
|
|
|
|
refreshData,
|
|
|
|
|
getCachedLookupDataSource,
|
|
|
|
|
}: {
|
|
|
|
|
isSubForm?: boolean
|
|
|
|
|
row: any
|
|
|
|
|
gridDto: GridDto
|
|
|
|
|
listFormCode: string
|
|
|
|
|
dataSource: CustomStore
|
|
|
|
|
refreshData: () => void
|
|
|
|
|
getCachedLookupDataSource: (editorOptions: any, colData: any, row?: any) => any
|
|
|
|
|
}) => {
|
|
|
|
|
const [formData, setFormData] = useState(row)
|
|
|
|
|
const refForm = useRef<FormDx>(null)
|
|
|
|
|
const navigate = useNavigate()
|
|
|
|
|
const { translate } = useLocalization()
|
|
|
|
|
const { checkPermission } = usePermission()
|
|
|
|
|
const isPwaMode = usePWA()
|
|
|
|
|
|
|
|
|
|
const keyField = gridDto.gridOptions.keyFieldName
|
|
|
|
|
const rowId = row[keyField!]
|
|
|
|
|
|
|
|
|
|
// Form Items
|
|
|
|
|
const formItems: GroupItem[] = useMemo(() => {
|
|
|
|
|
if (!gridDto) return []
|
|
|
|
|
|
|
|
|
|
return gridDto.gridOptions.editingFormDto
|
|
|
|
|
?.sort((a, b) => (a.order >= b.order ? 1 : -1))
|
|
|
|
|
.map((e) => {
|
|
|
|
|
return {
|
|
|
|
|
itemType: e.itemType,
|
|
|
|
|
colCount: e.colCount,
|
|
|
|
|
colSpan: e.colSpan,
|
|
|
|
|
caption: e.caption,
|
|
|
|
|
items: e.items
|
|
|
|
|
?.sort((a, b) => (a.order >= b.order ? 1 : -1))
|
|
|
|
|
.map((i: EditingFormItemDto) => {
|
|
|
|
|
let editorOptions = {}
|
|
|
|
|
const colData = gridDto.columnFormats.find((x) => x.fieldName === i.dataField)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
editorOptions = i.editorOptions && JSON.parse(i.editorOptions)
|
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
|
|
const item: SimpleItemWithColData = {
|
|
|
|
|
canRead: colData?.canRead ?? false,
|
|
|
|
|
canUpdate: colData?.canUpdate ?? false,
|
|
|
|
|
canCreate: colData?.canCreate ?? false,
|
|
|
|
|
canExport: colData?.canExport ?? false,
|
|
|
|
|
dataField: i.dataField,
|
|
|
|
|
name: i.dataField,
|
|
|
|
|
editorType2: i.editorType2,
|
|
|
|
|
editorType:
|
|
|
|
|
i.editorType2 == PlatformEditorTypes.dxGridBox ? 'dxDropDownBox' : i.editorType2,
|
|
|
|
|
colSpan: i.colSpan,
|
|
|
|
|
isRequired: i.isRequired,
|
|
|
|
|
editorOptions: {
|
|
|
|
|
...editorOptions,
|
|
|
|
|
...(colData?.lookupDto?.dataSourceType
|
|
|
|
|
? {
|
|
|
|
|
dataSource: getCachedLookupDataSource(colData?.editorOptions, colData),
|
|
|
|
|
valueExpr: colData?.lookupDto?.valueExpr?.toLowerCase(),
|
|
|
|
|
displayExpr: colData?.lookupDto?.displayExpr?.toLowerCase(),
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
},
|
|
|
|
|
colData,
|
|
|
|
|
tagBoxOptions: i.tagBoxOptions,
|
|
|
|
|
gridBoxOptions: i.gridBoxOptions,
|
|
|
|
|
editorScript: i.editorScript,
|
|
|
|
|
}
|
|
|
|
|
if (i.dataField.indexOf(':') >= 0) {
|
|
|
|
|
item.label = { text: captionize(i.dataField.split(':')[1]) }
|
|
|
|
|
}
|
|
|
|
|
item.editorOptions = {
|
|
|
|
|
...item.editorOptions,
|
|
|
|
|
readOnly: true,
|
|
|
|
|
}
|
|
|
|
|
return item
|
|
|
|
|
}),
|
|
|
|
|
} as GroupItem
|
|
|
|
|
})
|
|
|
|
|
}, [gridDto, getCachedLookupDataSource])
|
|
|
|
|
|
|
|
|
|
const permissionResults: PermissionResults = {
|
|
|
|
|
c:
|
|
|
|
|
gridDto?.gridOptions.editingOptionDto.allowAdding === true &&
|
|
|
|
|
checkPermission(gridDto?.gridOptions.permissionDto.c),
|
|
|
|
|
r: checkPermission(gridDto?.gridOptions.permissionDto.r),
|
|
|
|
|
u:
|
|
|
|
|
gridDto?.gridOptions.editingOptionDto.allowUpdating === true &&
|
|
|
|
|
checkPermission(gridDto?.gridOptions.permissionDto.u),
|
|
|
|
|
d:
|
|
|
|
|
gridDto?.gridOptions.editingOptionDto.allowDeleting === true &&
|
|
|
|
|
checkPermission(gridDto?.gridOptions.permissionDto.d),
|
|
|
|
|
e: checkPermission(gridDto?.gridOptions.permissionDto.e),
|
|
|
|
|
i: checkPermission(gridDto?.gridOptions.permissionDto.i),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onActionEdit = () => {
|
|
|
|
|
navigate(
|
|
|
|
|
ROUTES_ENUM.protected.admin.formEdit
|
|
|
|
|
.replace(':listFormCode', listFormCode)
|
|
|
|
|
.replace(':id', rowId!),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onActionNew = () => {
|
|
|
|
|
navigate(ROUTES_ENUM.protected.admin.formNew.replace(':listFormCode', listFormCode))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="bg-white dark:bg-neutral-800 border dark:border-neutral-700 flex flex-col">
|
|
|
|
|
<div
|
|
|
|
|
className={`flex items-center pt-2 px-2 ${isSubForm ? 'justify-end' : 'justify-between'}`}
|
|
|
|
|
>
|
|
|
|
|
{!isSubForm && <h3>{translate('::' + gridDto?.gridOptions.title)}</h3>}
|
|
|
|
|
{permissionResults && (
|
|
|
|
|
<FormButtons
|
|
|
|
|
isSubForm={true}
|
|
|
|
|
mode="view"
|
|
|
|
|
listFormCode={listFormCode}
|
|
|
|
|
id={rowId}
|
|
|
|
|
gridDto={gridDto}
|
|
|
|
|
commandColumnData={{ buttons: [] }}
|
|
|
|
|
dataSource={dataSource}
|
|
|
|
|
permissions={permissionResults}
|
|
|
|
|
handleSubmit={() => ({})}
|
|
|
|
|
refreshData={refreshData}
|
|
|
|
|
getSelectedRowKeys={() => [rowId]}
|
|
|
|
|
getSelectedRowsData={() => [row]}
|
|
|
|
|
getFilter={() => null}
|
|
|
|
|
onActionEdit={onActionEdit}
|
|
|
|
|
onActionNew={onActionNew}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex-grow">
|
|
|
|
|
<FormDevExpress
|
2025-11-11 11:50:54 +00:00
|
|
|
listFormCode={listFormCode}
|
2025-10-22 14:58:27 +00:00
|
|
|
mode="view"
|
|
|
|
|
refForm={refForm}
|
|
|
|
|
formData={formData}
|
|
|
|
|
formItems={formItems}
|
|
|
|
|
setFormData={setFormData}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CardItem
|