142 lines
3.6 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';
2019-08-29 18:13:47 +02:00
import MediaPreviewList from '../MediaPreviewList';
2019-07-09 16:56:40 +02:00
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
2019-07-30 16:18:11 +02:00
? JSON.stringify(value)
: value;
2019-07-10 11:12:30 +02:00
2019-07-31 16:50:05 +02:00
return moment
.parseZone(date)
.utc()
.format('dddd, MMMM Do YYYY');
2019-07-10 11:12:30 +02:00
}
case 'password':
return '••••••••';
2019-08-29 18:13:47 +02:00
case 'media':
case 'file':
case 'files':
return value;
2019-07-10 11:12:30 +02:00
default:
return '-';
}
};
2019-09-05 17:51:38 +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-08-29 18:13:47 +02:00
const renderMediaList = files => {
2019-09-05 17:51:38 +02:00
return <MediaPreviewList files={files}></MediaPreviewList>;
2019-08-29 18:13:47 +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}>
2019-08-29 18:13:47 +02:00
{get(schema, ['attributes', header.name, 'type']) !== 'media' ? (
<Truncate>
<Truncated>
{getDisplayedValue(
get(schema, ['attributes', header.name, 'type'], 'string'),
row[header.name],
header.name
)}
</Truncated>
</Truncate>
) : (
renderMediaList(
getDisplayedValue(
2019-07-10 11:12:30 +02:00
get(schema, ['attributes', header.name, 'type'], 'string'),
row[header.name],
header.name
2019-08-29 18:13:47 +02:00
)
)
)}
2019-07-09 16:56:40 +02:00
</td>
);
})}
<ActionContainer>
<IcoContainer
2019-08-29 18:13:47 +02:00
style={{ minWidth: 'inherit', width: '100%' }}
2019-07-09 16:56:40 +02:00
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));