import PropTypes from 'prop-types'
import { Descriptions, Typography, Space } from 'antd'
import { getModelProperty } from '../../../database/ObjectModels'
import ObjectProperty from './ObjectProperty'
import ArrowRightIcon from '../../Icons/ArrowRightIcon'
const { Text } = Typography
const PropertyChanges = ({ type, value }) => {
if (!value || (!value.new && !value.old)) {
return n/a
}
// Helper to flatten nested objects into dot notation keys
function flattenObject(obj, prefix = '', res = {}) {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const val = obj[key]
const newKey = prefix ? `${prefix}.${key}` : key
// Don't flatten keys that are "state" or "netGross"
if (
val &&
typeof val === 'object' &&
!Array.isArray(val) &&
key !== 'state' &&
key !== 'netGross'
) {
flattenObject(val, newKey, res)
} else {
res[newKey] = val
}
}
}
return res
}
const flatOld = value?.old ? flattenObject(value.old) : {}
const flatNew = value?.new ? flattenObject(value.new) : {}
const combinedChanges = {
...flatOld,
...flatNew
}
return (
{Object.keys(combinedChanges).map((key) => {
var changeProperty = getModelProperty(type, key)
if (!changeProperty) {
return null
}
return (
{value?.old ? (
) : null}
{value?.old && value?.new ? (
) : null}
{value?.new ? (
) : null}
)
})}
)
}
PropertyChanges.propTypes = {
type: PropTypes.string.isRequired,
value: PropTypes.shape({
old: PropTypes.object,
new: PropTypes.object
})
}
export default PropertyChanges