Refactor Object Creation and Update Logic in NewObjectForm and ObjectForm

- Enhanced the object creation and update processes by integrating the stripNestedObjectProperties utility function, ensuring cleaner payloads are sent to the createObject and updateObject functions.
- Updated the import statements in both NewObjectForm and ObjectForm to include the new utility function, improving data management and consistency across forms.
This commit is contained in:
Tom Butcher 2026-08-25 21:28:55 +01:00
parent 4fe8d4f51f
commit 4b03caff5c
2 changed files with 6 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import { useMessageContext } from '../context/MessageContext'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import set from 'lodash/set' import set from 'lodash/set'
import { getModelByName } from '../../../database/ObjectModels' import { getModelByName } from '../../../database/ObjectModels'
import { mergeFormData } from '../utils/Utils' import { mergeFormData, stripNestedObjectProperties } from '../utils/Utils'
const buildObjectFromEntries = (entries = []) => { const buildObjectFromEntries = (entries = []) => {
return entries.reduce((acc, entry) => { return entries.reduce((acc, entry) => {
@ -196,7 +196,8 @@ const NewObjectForm = ({ type, style, defaultValues = {}, children }) => {
objectData, objectData,
computedEntries computedEntries
) )
const newObject = await createObject(type, computedObjectData) const payload = stripNestedObjectProperties(computedObjectData, model)
const newObject = await createObject(type, payload)
showSuccess('Object created successfully') showSuccess('Object created successfully')
return newObject return newObject
} catch (err) { } catch (err) {

View File

@ -16,7 +16,7 @@ import { getModelByName } from '../../../database/ObjectModels'
import { useLocation, useNavigate } from 'react-router-dom' import { useLocation, useNavigate } from 'react-router-dom'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { useActions } from '../context/ActionsContext' import { useActions } from '../context/ActionsContext'
import { mergeFormData } from '../utils/Utils' import { mergeFormData, stripNestedObjectProperties } from '../utils/Utils'
const getUserId = (user) => user?._id || user const getUserId = (user) => user?._id || user
@ -771,7 +771,8 @@ const ObjectForm = forwardRef(
...value ...value
} }
onStateChangeRef.current({ editLoading: true }) onStateChangeRef.current({ editLoading: true })
const updatedObject = await updateObject(id, type, currentFormData) const payload = stripNestedObjectProperties(currentFormData, model)
const updatedObject = await updateObject(id, type, payload)
if (updatedObject.error) { if (updatedObject.error) {
console.log('THERE IS AN ERROR') console.log('THERE IS AN ERROR')