|
|
|
|
@ -14,16 +14,8 @@ import { AuthContext } from '../context/AuthContext'
|
|
|
|
|
import ObjectProperty from './ObjectProperty'
|
|
|
|
|
import { getModelByName } from '../../../database/ObjectModels'
|
|
|
|
|
import merge from 'lodash/merge'
|
|
|
|
|
import { getModelProperty } from '../../../database/ObjectModels'
|
|
|
|
|
const { SHOW_CHILD } = TreeSelect
|
|
|
|
|
|
|
|
|
|
// Helper to check if two values are equal (handling objects/ids)
|
|
|
|
|
const areValuesEqual = (v1, v2) => {
|
|
|
|
|
const id1 = v1 && typeof v1 === 'object' && v1._id ? v1._id : v1
|
|
|
|
|
const id2 = v2 && typeof v2 === 'object' && v2._id ? v2._id : v2
|
|
|
|
|
return String(id1) === String(id2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ObjectSelect = ({
|
|
|
|
|
type = 'unknown',
|
|
|
|
|
showSearch = false,
|
|
|
|
|
@ -39,7 +31,7 @@ const ObjectSelect = ({
|
|
|
|
|
const { token } = useContext(AuthContext)
|
|
|
|
|
// --- State ---
|
|
|
|
|
const [treeData, setTreeData] = useState([])
|
|
|
|
|
const [objectPropertiesTree, setObjectPropertiesTree] = useState([])
|
|
|
|
|
const [objectPropertiesTree, setObjectPropertiesTree] = useState({})
|
|
|
|
|
const [initialized, setInitialized] = useState(false)
|
|
|
|
|
const [error, setError] = useState(false)
|
|
|
|
|
const properties = useMemo(() => getModelByName(type).group || [], [type])
|
|
|
|
|
@ -77,50 +69,6 @@ const ObjectSelect = ({
|
|
|
|
|
[isMinimalObject, fetchObject, type]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const mergeGroups = useCallback((current, incoming) => {
|
|
|
|
|
if (!current) return incoming
|
|
|
|
|
if (!incoming) return current
|
|
|
|
|
if (!Array.isArray(current) || !Array.isArray(incoming)) return incoming
|
|
|
|
|
|
|
|
|
|
const merged = [...current]
|
|
|
|
|
|
|
|
|
|
// Helper to generate a unique key for a group node
|
|
|
|
|
const getGroupKey = (item) => {
|
|
|
|
|
const val = item.value
|
|
|
|
|
const valPart =
|
|
|
|
|
val && typeof val === 'object' && val._id
|
|
|
|
|
? val._id
|
|
|
|
|
: JSON.stringify(val)
|
|
|
|
|
return `${item.property}:${valPart}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const item of incoming) {
|
|
|
|
|
if (item.property && item.value !== undefined) {
|
|
|
|
|
// It's a group node
|
|
|
|
|
const itemKey = getGroupKey(item)
|
|
|
|
|
const existingIdx = merged.findIndex(
|
|
|
|
|
(x) =>
|
|
|
|
|
x.property && x.value !== undefined && getGroupKey(x) === itemKey
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (existingIdx > -1) {
|
|
|
|
|
merged[existingIdx] = {
|
|
|
|
|
...merged[existingIdx],
|
|
|
|
|
children: mergeGroups(merged[existingIdx].children, item.children)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
merged.push(item)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// It's a leaf object
|
|
|
|
|
if (!merged.some((x) => String(x._id) === String(item._id))) {
|
|
|
|
|
merged.push(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return merged
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
// Fetch the object properties tree from the API
|
|
|
|
|
const handleFetchObjectsProperties = useCallback(
|
|
|
|
|
async (customFilter = filter) => {
|
|
|
|
|
@ -130,14 +78,11 @@ const ObjectSelect = ({
|
|
|
|
|
filter: customFilter,
|
|
|
|
|
masterFilter
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
|
setObjectPropertiesTree((prev) => mergeGroups(prev, data))
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback if API returns something unexpected
|
|
|
|
|
setObjectPropertiesTree((prev) => merge([], prev, data))
|
|
|
|
|
} else {
|
|
|
|
|
setObjectPropertiesTree((prev) => merge({}, prev, data))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setInitialLoading(false)
|
|
|
|
|
setError(false)
|
|
|
|
|
return data
|
|
|
|
|
@ -147,31 +92,24 @@ const ObjectSelect = ({
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[
|
|
|
|
|
type,
|
|
|
|
|
fetchObjectsByProperty,
|
|
|
|
|
properties,
|
|
|
|
|
filter,
|
|
|
|
|
masterFilter,
|
|
|
|
|
mergeGroups
|
|
|
|
|
]
|
|
|
|
|
[type, fetchObjectsByProperty, properties, filter, masterFilter]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Convert the API response to AntD TreeSelect treeData
|
|
|
|
|
const buildTreeData = useCallback(
|
|
|
|
|
(data, pIdx = 0, parentKeys = [], filterPath = []) => {
|
|
|
|
|
if (!data || !Array.isArray(data)) return []
|
|
|
|
|
console.log(data, pIdx, properties.length)
|
|
|
|
|
// If we are past the grouping properties, these are leaf objects
|
|
|
|
|
if (pIdx >= properties.length) {
|
|
|
|
|
if (!data) return []
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
|
return data.map((object) => {
|
|
|
|
|
setObjectList((prev) => {
|
|
|
|
|
if (prev.some((p) => p._id === object._id)) return prev
|
|
|
|
|
return [...prev, object]
|
|
|
|
|
const filtered = prev.filter(
|
|
|
|
|
(prevObject) => prevObject._id != object._id
|
|
|
|
|
)
|
|
|
|
|
return [...filtered, object]
|
|
|
|
|
})
|
|
|
|
|
return {
|
|
|
|
|
title: (
|
|
|
|
|
<div style={{ paddingTop: 0 }}>
|
|
|
|
|
<div style={{ paddingTop: '2px' }}>
|
|
|
|
|
<ObjectProperty
|
|
|
|
|
key={object._id}
|
|
|
|
|
type='object'
|
|
|
|
|
@ -185,63 +123,48 @@ const ObjectSelect = ({
|
|
|
|
|
value: object._id,
|
|
|
|
|
key: object._id,
|
|
|
|
|
isLeaf: true,
|
|
|
|
|
property: properties[pIdx - 1], // previous property
|
|
|
|
|
parentKeys,
|
|
|
|
|
filterPath
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Group Nodes
|
|
|
|
|
return data
|
|
|
|
|
.map((group) => {
|
|
|
|
|
// Only process if it looks like a group
|
|
|
|
|
if (!group.property) return null
|
|
|
|
|
|
|
|
|
|
const { property, value, children } = group
|
|
|
|
|
var valueString = value
|
|
|
|
|
if (value && typeof value === 'object' && value._id) {
|
|
|
|
|
valueString = value._id
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(valueString)) {
|
|
|
|
|
valueString = valueString.join(',')
|
|
|
|
|
}
|
|
|
|
|
const nodeKey = parentKeys
|
|
|
|
|
.concat(property + ':' + valueString)
|
|
|
|
|
.join('-')
|
|
|
|
|
const newFilterPath = filterPath.concat({
|
|
|
|
|
property,
|
|
|
|
|
value: valueString
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const modelProperty = getModelProperty(type, property)
|
|
|
|
|
if (typeof data == 'object') {
|
|
|
|
|
const property = properties[pIdx] || null
|
|
|
|
|
return Object.entries(data)
|
|
|
|
|
.map(([key, value]) => {
|
|
|
|
|
if (property != null && typeof value === 'object') {
|
|
|
|
|
const newFilterPath = filterPath.concat({ property, value: key })
|
|
|
|
|
return {
|
|
|
|
|
title: <ObjectProperty {...modelProperty} value={value} />,
|
|
|
|
|
value: nodeKey,
|
|
|
|
|
key: nodeKey,
|
|
|
|
|
title: <ObjectProperty type={property} value={key} />,
|
|
|
|
|
value: parentKeys.concat(key).join('-'),
|
|
|
|
|
filterValue: key,
|
|
|
|
|
key: parentKeys.concat(key).join('-'),
|
|
|
|
|
property,
|
|
|
|
|
filterValue: valueString,
|
|
|
|
|
parentKeys: parentKeys.concat(valueString),
|
|
|
|
|
parentKeys: parentKeys.concat(key || '-'),
|
|
|
|
|
filterPath: newFilterPath,
|
|
|
|
|
selectable: false,
|
|
|
|
|
isLeaf: false,
|
|
|
|
|
|
|
|
|
|
children: buildTreeData(
|
|
|
|
|
children,
|
|
|
|
|
value,
|
|
|
|
|
pIdx + 1,
|
|
|
|
|
parentKeys.concat(valueString),
|
|
|
|
|
parentKeys.concat(key),
|
|
|
|
|
newFilterPath
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
isLeaf: false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[properties, type]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// --- loadData for async loading on expand ---
|
|
|
|
|
const loadData = async (node) => {
|
|
|
|
|
// node.property is the property name, node.value is the value key
|
|
|
|
|
// node.property is the property name, node.value is the value
|
|
|
|
|
if (!node.property) return
|
|
|
|
|
if (type == 'unknown') return
|
|
|
|
|
// Build filter for this node by merging all parent property-value pairs
|
|
|
|
|
const customFilter = { ...filter }
|
|
|
|
|
if (Array.isArray(node.filterPath)) {
|
|
|
|
|
@ -249,40 +172,26 @@ const ObjectSelect = ({
|
|
|
|
|
customFilter[property] = value
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// Ensure current node is in filter (should be covered by filterPath, but redundancy is safe)
|
|
|
|
|
customFilter[node.property] = node.filterValue
|
|
|
|
|
// Fetch children for this node
|
|
|
|
|
const data = await handleFetchObjectsProperties(customFilter)
|
|
|
|
|
if (!data) return
|
|
|
|
|
|
|
|
|
|
// Navigate to the specific node's children in the response
|
|
|
|
|
let nodeSpecificChildren = data
|
|
|
|
|
|
|
|
|
|
if (node.filterPath && Array.isArray(node.filterPath)) {
|
|
|
|
|
for (const pathItem of node.filterPath) {
|
|
|
|
|
if (!Array.isArray(nodeSpecificChildren)) break
|
|
|
|
|
const match = nodeSpecificChildren.find(
|
|
|
|
|
(g) =>
|
|
|
|
|
g.property === pathItem.property &&
|
|
|
|
|
areValuesEqual(g.value, pathItem.value)
|
|
|
|
|
)
|
|
|
|
|
if (match) {
|
|
|
|
|
nodeSpecificChildren = match.children
|
|
|
|
|
} else {
|
|
|
|
|
nodeSpecificChildren = []
|
|
|
|
|
break
|
|
|
|
|
// Extract only the children for the specific node that was expanded
|
|
|
|
|
let nodeSpecificData = data
|
|
|
|
|
if (typeof data === 'object' && !Array.isArray(data)) {
|
|
|
|
|
// If the API returns an object with multiple keys, get only the data for this node
|
|
|
|
|
nodeSpecificData = data[node.value] || {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build new tree children only for this specific node
|
|
|
|
|
const children = buildTreeData(
|
|
|
|
|
nodeSpecificChildren,
|
|
|
|
|
nodeSpecificData,
|
|
|
|
|
properties.indexOf(node.property) + 1,
|
|
|
|
|
node.parentKeys || [],
|
|
|
|
|
node.filterPath
|
|
|
|
|
(node.filterPath || []).concat({
|
|
|
|
|
property: node.property,
|
|
|
|
|
value: node.filterValue
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Update treeData with new children for this node only
|
|
|
|
|
setTreeData((prevTreeData) => {
|
|
|
|
|
// Helper to recursively update the correct node
|
|
|
|
|
@ -341,8 +250,7 @@ const ObjectSelect = ({
|
|
|
|
|
value &&
|
|
|
|
|
typeof value === 'object' &&
|
|
|
|
|
value !== null &&
|
|
|
|
|
!initialized &&
|
|
|
|
|
type != 'unknown'
|
|
|
|
|
!initialized
|
|
|
|
|
) {
|
|
|
|
|
// Check if value is a minimal object and fetch full object if needed
|
|
|
|
|
const fullValue = await fetchFullObjectIfNeeded(value)
|
|
|
|
|
@ -352,13 +260,7 @@ const ObjectSelect = ({
|
|
|
|
|
properties.forEach((prop) => {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(fullValue, prop)) {
|
|
|
|
|
const filterValue = fullValue[prop]
|
|
|
|
|
if (
|
|
|
|
|
filterValue &&
|
|
|
|
|
typeof filterValue === 'object' &&
|
|
|
|
|
filterValue._id
|
|
|
|
|
) {
|
|
|
|
|
valueFilter[prop] = filterValue._id
|
|
|
|
|
} else if (filterValue?.name) {
|
|
|
|
|
if (filterValue?.name) {
|
|
|
|
|
valueFilter[prop] = filterValue.name
|
|
|
|
|
} else if (Array.isArray(filterValue)) {
|
|
|
|
|
valueFilter[prop] = filterValue.join(',')
|
|
|
|
|
@ -373,13 +275,12 @@ const ObjectSelect = ({
|
|
|
|
|
setInitialized(true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!initialized && token != null && type != 'unknown') {
|
|
|
|
|
if (!initialized && token != null) {
|
|
|
|
|
handleFetchObjectsProperties()
|
|
|
|
|
setInitialized(true)
|
|
|
|
|
}
|
|
|
|
|
if (value == null || type == 'unknown') {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
setTreeSelectValue(null)
|
|
|
|
|
setInitialLoading(false)
|
|
|
|
|
setInitialized(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -391,8 +292,7 @@ const ObjectSelect = ({
|
|
|
|
|
handleFetchObjectsProperties,
|
|
|
|
|
initialized,
|
|
|
|
|
token,
|
|
|
|
|
fetchFullObjectIfNeeded,
|
|
|
|
|
type
|
|
|
|
|
fetchFullObjectIfNeeded
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const prevValuesRef = useRef({ type, masterFilter })
|
|
|
|
|
@ -441,14 +341,6 @@ const ObjectSelect = ({
|
|
|
|
|
}
|
|
|
|
|
}, [value])
|
|
|
|
|
|
|
|
|
|
const placeholder = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
type == 'unknown'
|
|
|
|
|
? 'n/a'
|
|
|
|
|
: `Select a ${getModelByName(type).label.toLowerCase()}...`,
|
|
|
|
|
[type]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// --- Error UI ---
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
@ -481,12 +373,12 @@ const ObjectSelect = ({
|
|
|
|
|
multiple={multiple}
|
|
|
|
|
loadData={loadData}
|
|
|
|
|
showCheckedStrategy={SHOW_CHILD}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
placeholder={`Select a ${getModelByName(type).label.toLowerCase()}...`}
|
|
|
|
|
{...treeSelectProps}
|
|
|
|
|
{...rest}
|
|
|
|
|
value={treeSelectValue}
|
|
|
|
|
onChange={onTreeSelectChange}
|
|
|
|
|
disabled={disabled || type == 'unknown'}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|