119 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-07-09 16:56:40 +02:00
import React, { memo } from 'react';
2019-07-10 09:31:26 +02:00
import { withRouter } from 'react-router';
2019-07-09 16:56:40 +02:00
import PropTypes from 'prop-types';
2019-07-10 11:12:30 +02:00
import { get, isEmpty, isNull, isObject, toLower, toString } from 'lodash';
import moment from 'moment';
2019-07-09 16:56:40 +02:00
import { IcoContainer } from 'strapi-helper-plugin';
2019-07-09 19:38:39 +02:00
import { useListView } from '../../contexts/ListView';
2019-07-09 16:56:40 +02:00
import CustomInputCheckbox from '../CustomInputCheckbox';
import { ActionContainer, Truncate, Truncated } from './styledComponents';
2019-07-10 11:12:30 +02:00
const getDisplayedValue = (type, value, name) => {
switch (toLower(type)) {
case 'string':
case 'text':
case 'email':
case 'enumeration':
return (value && !isEmpty(toString(value))) || name === 'id'
? toString(value)
: '-';
case 'float':
case 'integer':
case 'biginteger':
case 'decimal':
return !isNull(value) ? toString(value) : '-';
case 'boolean':
return value !== null ? toString(value) : '-';
case 'date':
case 'time':
case 'datetime':
case 'timestamp': {
if (value === null) {
return '-';
}
const date =
value && isObject(value) && value._isAMomentObject === true
? value
: moment.utc(value);
return date.format('YYYY-MM-DD HH:mm:ss');
}
case 'password':
return '••••••••';
default:
return '-';
}
};
2019-07-10 09:31:26 +02:00
function Row({ goTo, isBulkable, row, headers }) {
2019-07-10 11:12:30 +02:00
const {
entriesToDelete,
onChangeBulk,
onClickDelete,
schema,
} = useListView();
2019-07-10 08:43:40 +02:00
2019-07-09 16:56:40 +02:00
return (
<>
{isBulkable && (
2019-07-18 14:07:22 +02:00
<td key="i" onClick={e => e.stopPropagation()}>
2019-07-09 19:38:39 +02:00
<CustomInputCheckbox
name={row.id}
onChange={onChangeBulk}
value={
entriesToDelete.filter(id => toString(id) === toString(row.id))
.length > 0
}
/>
2019-07-09 16:56:40 +02:00
</td>
)}
{headers.map(header => {
return (
<td key={header.name}>
<Truncate>
2019-07-10 11:12:30 +02:00
<Truncated>
{getDisplayedValue(
get(schema, ['attributes', header.name, 'type'], 'string'),
row[header.name],
header.name
)}
</Truncated>
2019-07-09 16:56:40 +02:00
</Truncate>
</td>
);
})}
<ActionContainer>
<IcoContainer
icons={[
2019-07-10 09:31:26 +02:00
{
icoType: 'pencil',
onClick: () => {
goTo(row.id);
},
},
2019-07-09 16:56:40 +02:00
{
id: row.id,
icoType: 'trash',
2019-07-10 09:12:48 +02:00
onClick: () => {
onClickDelete(row.id);
},
2019-07-09 16:56:40 +02:00
},
]}
/>
</ActionContainer>
</>
);
}
Row.propTypes = {
2019-07-10 09:31:26 +02:00
goTo: PropTypes.func.isRequired,
2019-07-09 16:56:40 +02:00
headers: PropTypes.array.isRequired,
isBulkable: PropTypes.bool.isRequired,
row: PropTypes.object.isRequired,
};
2019-07-10 09:31:26 +02:00
export default withRouter(memo(Row));