import { Descriptions, Card, Flex, Divider, Skeleton } from 'antd'
import PropTypes from 'prop-types'
import ObjectProperty from './ObjectProperty'
import { createElement } from 'react'
import Thumbnail from './Thumbnail'
import { LoadingOutlined } from '@ant-design/icons'
const ObjectCard = ({
isSkeleton = false,
model,
modelProperties,
visibleColumns = {},
record,
isEditing = false,
rowActions = [],
renderActions,
lazyLoading = false,
cardStyle = 'borderless'
}) => {
const descriptionItems = []
const modelIcon = createElement(model.icon, { style: { fontSize: 24 } })
const thumbnailProperty =
model.properties.find((p) => p.thumbnail === true) || null
const thumbnailPresent =
thumbnailProperty !== null && record?.[thumbnailProperty.name]?._id
model.columns.forEach((colName) => {
const prop = modelProperties.find((p) => p.name === colName)
if (prop) {
if (
(Object.keys(visibleColumns).length > 0 &&
visibleColumns[prop.name] === false) ||
prop.name == 'name' ||
(prop.name == 'state' && visibleColumns?.name == true) ||
(thumbnailPresent && prop.name === thumbnailProperty.name)
) {
return
}
descriptionItems.push({
key: prop.name,
label: prop.label,
span: 2,
children: isSkeleton ? (
) : (
)
})
}
})
var actions = undefined
if (rowActions.length > 0) {
actions = renderActions(record, lazyLoading)
}
const primaryDescriptionItems = thumbnailPresent
? descriptionItems.slice(0, 2)
: []
const remainingDescriptionItems = thumbnailPresent
? descriptionItems.slice(2)
: descriptionItems
const nameContent = (
<>
{modelIcon}
p.name === 'name')}
objectData={record}
isEditing={isEditing}
style={{
fontSize: 20,
fontWeight: '600',
lineHeight: 1.2,
minWidth: 0
}}
/>
{visibleColumns?.state == true && (
p.name === 'state')}
objectData={record}
/>
)}
>
)
return (
{thumbnailPresent && (
{visibleColumns?.name == true && nameContent}
{primaryDescriptionItems.length > 0 && (
)}
)}
{visibleColumns?.name == true && !thumbnailPresent && nameContent}
{remainingDescriptionItems.length > 0 && (
)}
{actions && (
<>
{actions}
{lazyLoading && (
)}
>
)}
)
}
ObjectCard.propTypes = {
model: PropTypes.object.isRequired,
modelProperties: PropTypes.array.isRequired,
visibleColumns: PropTypes.object,
record: PropTypes.object.isRequired,
isEditing: PropTypes.bool,
rowActions: PropTypes.array,
renderActions: PropTypes.func.isRequired,
cardStyle: PropTypes.string,
isSkeleton: PropTypes.bool,
lazyLoading: PropTypes.bool
}
export default ObjectCard