erp-platform/ui/src/views/form/FormNew.tsx

253 lines
6.7 KiB
TypeScript
Raw Normal View History

2025-08-21 12:26:25 +00:00
import { Container, Loading } from '@/components/shared'
2025-05-06 06:45:49 +00:00
import { useLocalization } from '@/utils/hooks/useLocalization'
import { DataType } from 'devextreme/common'
import { SimpleItem } from 'devextreme/ui/form'
import { useEffect, useState } from 'react'
import { Helmet } from 'react-helmet'
2025-10-22 20:18:03 +00:00
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'
2025-05-06 06:45:49 +00:00
import FormButtons from './FormButtons'
import FormDevExpress from './FormDevExpress'
import { FormProps } from './types'
2025-11-08 20:22:50 +00:00
import { useGridData } from './useFormData'
2025-09-23 10:30:57 +00:00
import { useCurrentMenuIcon } from '@/utils/hooks/useCurrentMenuIcon'
import { Badge } from '@/components/ui'
2025-05-06 06:45:49 +00:00
const FormNew = (
props: FormProps = {
isSubForm: false,
},
) => {
const mode = 'new'
const { isSubForm, level, sParams } = props
const params = useParams()
const listFormCode = props?.listFormCode ?? params?.listFormCode ?? ''
const [searchParams] = useSearchParams()
const { translate } = useLocalization()
2025-09-23 10:30:57 +00:00
const MenuIcon = useCurrentMenuIcon('w-5 h-5')
2025-10-22 20:18:03 +00:00
const navigate = useNavigate()
2025-09-23 10:30:57 +00:00
2025-05-06 06:45:49 +00:00
const {
2025-10-22 20:18:03 +00:00
fetchData,
2025-05-06 06:45:49 +00:00
setFormData,
loading,
handleSubmit,
gridDto,
dataSource,
commandColumnData,
formData,
formItems,
refForm,
permissionResults,
} = useGridData({
mode,
listFormCode,
level,
isSubForm,
2025-10-22 20:18:03 +00:00
onSubmitAction: (props?.onActionView),
2025-05-06 06:45:49 +00:00
})
const [newParams, setNewParams] = useState<URLSearchParams>(new URLSearchParams())
useEffect(() => {
setNewParams(
new URLSearchParams({
...Object.fromEntries(searchParams),
...Object.fromEntries(sParams ?? []),
}),
)
}, [searchParams, sParams])
useEffect(() => {
if (!formItems) {
return
}
2025-10-22 20:18:03 +00:00
// Sadece formData yoksa (ilk yüklemede) initial data oluştur
// Kullanıcının girdiği bilgileri korumak için mevcut formData varsa setleme
if (!formData) {
setFormData(createInitialFormData())
}
}, [formItems])
// newParams değiştiğinde sadece newParams'tan gelen alanları güncelle
useEffect(() => {
if (!formItems || !formData || !gridDto?.columnFormats) {
return
}
const updatedData = { ...formData }
let hasChanges = false
for (const colFormat of gridDto.columnFormats) {
if (!colFormat.fieldName || !newParams.has(colFormat.fieldName)) {
continue
}
const dType = colFormat.dataType as DataType
let newValue: any
switch (dType) {
case 'date':
case 'datetime':
newValue = new Date(newParams.get(colFormat.fieldName)!)
break
case 'number':
newValue = Number(newParams.get(colFormat.fieldName))
break
case 'boolean':
if (newParams.get(colFormat.fieldName) === 'true') {
newValue = true
} else if (newParams.get(colFormat.fieldName) === 'false') {
newValue = false
}
break
case 'object':
try {
newValue = JSON.parse(newParams.get(colFormat.fieldName) as string)
} catch (error) {
newValue = updatedData[colFormat.fieldName]
}
break
default:
newValue = newParams.get(colFormat.fieldName)
break
}
if (updatedData[colFormat.fieldName] !== newValue) {
updatedData[colFormat.fieldName] = newValue
hasChanges = true
}
}
if (hasChanges) {
setFormData(updatedData)
}
}, [newParams, gridDto?.columnFormats])
// newParams'tan gelen alanları readonly yap
useEffect(() => {
if (!formItems || !gridDto?.columnFormats || !newParams) {
return
}
for (const colFormat of gridDto.columnFormats) {
if (!colFormat.fieldName || !newParams.has(colFormat.fieldName)) {
continue
}
// Eğer URL'den değer geliyorsa, bu editörü readonly yapıyoruz
formItems.forEach((form) => {
form?.items?.forEach((item: SimpleItem) => {
if (item.dataField === colFormat.fieldName) {
item.editorOptions = { ...item.editorOptions, readOnly: true }
}
})
})
}
}, [formItems, newParams, gridDto?.columnFormats])
2025-05-06 06:45:49 +00:00
function createInitialFormData() {
const data: any = {}
if (!formItems?.length) {
return data
}
// Default tüm alanlar undefined olsun
for (const formGroupItem of formItems) {
if (formGroupItem.items) {
//SimpleItem
for (const formItem of formGroupItem.items) {
const f = formItem as SimpleItem
data[f.dataField!] = undefined
}
}
}
if (!gridDto?.columnFormats) {
2025-10-22 20:18:03 +00:00
return data
2025-05-06 06:45:49 +00:00
}
2025-10-22 20:18:03 +00:00
// Grid'den gelen columnFormat'ları kullanarak default değerleri set et
2025-05-06 06:45:49 +00:00
for (const colFormat of gridDto?.columnFormats) {
if (!colFormat.fieldName) {
continue
}
if (colFormat.defaultValue != null) {
data[colFormat.fieldName] = colFormat.defaultValue
}
}
return data
}
if (!listFormCode) {
return <></>
}
if (loading) {
return <Loading type="cover" loading={loading}></Loading>
}
if (!formData) {
return <>{translate('::Error:0002')}</>
}
return (
2025-08-21 12:26:25 +00:00
<Container>
2025-05-06 06:45:49 +00:00
{!isSubForm && (
<Helmet
2025-11-03 11:25:05 +00:00
titleTemplate="%s | Erp Platform"
2025-05-06 06:45:49 +00:00
title={translate('::' + gridDto?.gridOptions.title)}
2025-11-03 11:25:05 +00:00
defaultTitle="Erp Platform"
2025-05-06 06:45:49 +00:00
></Helmet>
)}
<div
className={`flex items-center pb-2 px-2 ${isSubForm ? 'justify-end' : 'justify-between'}`}
>
2025-09-23 10:30:57 +00:00
<div className="flex items-center gap-2">
{MenuIcon}
{!isSubForm && (
<>
<h4 className="text-slate-700 text-sm font-medium leading-none">
{translate('::' + gridDto?.gridOptions?.title)}
</h4>
2025-09-23 12:52:40 +00:00
2025-09-23 10:30:57 +00:00
<Badge content={mode} />
</>
)}
</div>
2025-05-06 06:45:49 +00:00
{permissionResults && (
<FormButtons
isSubForm={isSubForm}
2025-05-06 06:45:49 +00:00
mode={mode}
listFormCode={listFormCode}
id={formData?.Id}
gridDto={gridDto!}
commandColumnData={commandColumnData!}
dataSource={dataSource!}
permissions={permissionResults}
handleSubmit={handleSubmit}
refreshData={() => {}}
getSelectedRowKeys={() => []}
getSelectedRowsData={() => [formData]}
getFilter={() => []}
onActionView={props?.onActionView}
/>
)}
</div>
<div className="px-2">
<FormDevExpress
mode={mode}
refForm={refForm}
formData={formData}
formItems={formItems}
setFormData={setFormData}
listFormCode={listFormCode}
/>
</div>
2025-08-21 12:26:25 +00:00
</Container>
2025-05-06 06:45:49 +00:00
)
}
export default FormNew