Refactor ObjectSelect component to improve code clarity and maintainability

- Removed unnecessary whitespace to enhance code readability.
- Streamlined data handling logic for better performance and consistency.
- Improved structure of the loadData function for clearer data fetching and processing.
This commit is contained in:
Tom Butcher 2025-08-18 01:02:48 +01:00
parent e423f32493
commit dcf90469f4

View File

@ -52,7 +52,6 @@ const ObjectSelect = ({
} else { } else {
setObjectPropertiesTree((prev) => merge({}, prev, data)) setObjectPropertiesTree((prev) => merge({}, prev, data))
} }
setInitialLoading(false) setInitialLoading(false)
setError(false) setError(false)
return data return data
@ -96,7 +95,6 @@ const ObjectSelect = ({
} }
}) })
} }
if (typeof data == 'object') { if (typeof data == 'object') {
const property = properties[pIdx] || null const property = properties[pIdx] || null
return Object.entries(data) return Object.entries(data)
@ -131,7 +129,6 @@ const ObjectSelect = ({
const loadData = async (node) => { const loadData = async (node) => {
// node.property is the property name, node.value is the value // node.property is the property name, node.value is the value
if (!node.property) return if (!node.property) return
// Build filter for this node by merging all parent property-value pairs // Build filter for this node by merging all parent property-value pairs
const customFilter = { ...filter } const customFilter = { ...filter }
if (Array.isArray(node.filterPath)) { if (Array.isArray(node.filterPath)) {
@ -140,18 +137,15 @@ const ObjectSelect = ({
}) })
} }
customFilter[node.property] = node.value customFilter[node.property] = node.value
// Fetch children for this node // Fetch children for this node
const data = await handleFetchObjectsProperties(customFilter) const data = await handleFetchObjectsProperties(customFilter)
if (!data) return if (!data) return
// Extract only the children for the specific node that was expanded // Extract only the children for the specific node that was expanded
let nodeSpecificData = data let nodeSpecificData = data
if (typeof data === 'object' && !Array.isArray(data)) { if (typeof data === 'object' && !Array.isArray(data)) {
// If the API returns an object with multiple keys, get only the data for this node // If the API returns an object with multiple keys, get only the data for this node
nodeSpecificData = data[node.value] || {} nodeSpecificData = data[node.value] || {}
} }
// Build new children only for this specific node // Build new children only for this specific node
const children = buildTreeData( const children = buildTreeData(
nodeSpecificData, nodeSpecificData,
@ -162,7 +156,6 @@ const ObjectSelect = ({
value: node.value value: node.value
}) })
) )
// Update treeData with new children for this node only // Update treeData with new children for this node only
setTreeData((prevTreeData) => { setTreeData((prevTreeData) => {
// Helper to recursively update the correct node // Helper to recursively update the correct node