141 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-09-11 10:07:25 +02:00
import React, { memo, useCallback } 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-11-28 15:57:39 +01:00
import { IcoContainer, useGlobalContext } from 'strapi-helper-plugin';
2019-10-31 11:57:40 +01:00
import useListView from '../../hooks/useListView';
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': {
2019-12-05 11:59:23 +01:00
if (value == null) {
2019-07-10 11:12:30 +02:00
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-09-11 10:07:25 +02:00
const memoizedDisplayedValue = useCallback(
name => {
const type = get(schema, ['attributes', name, 'type'], 'string');
return getDisplayedValue(type, row[name], name);
},
[row, schema]
);
2019-08-29 18:13:47 +02:00
2019-11-28 15:57:39 +01:00
const { emitEvent } = useGlobalContext;
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>
2019-09-11 10:07:25 +02:00
<Truncated>{memoizedDisplayedValue(header.name)}</Truncated>
2019-08-29 18:13:47 +02:00
</Truncate>
) : (
2019-09-11 10:07:25 +02:00
<MediaPreviewList
files={memoizedDisplayedValue(header.name)}
></MediaPreviewList>
2019-08-29 18:13:47 +02:00
)}
2019-07-09 16:56:40 +02:00
</td>
);
})}
<ActionContainer>
<IcoContainer
2019-11-25 15:56:44 +01:00
style={{ minWidth: 'inherit', width: '100%', lineHeight: 48 }}
2019-07-09 16:56:40 +02:00
icons={[
2019-07-10 09:31:26 +02:00
{
2019-11-19 16:17:15 +01:00
icoType: 'pencil-alt',
2019-07-10 09:31:26 +02:00
onClick: () => {
2019-11-28 15:57:39 +01:00
emitEvent('willEditEntryFromList');
2019-07-10 09:31:26 +02:00
goTo(row.id);
},
},
2019-07-09 16:56:40 +02:00
{
id: row.id,
2019-12-06 17:11:17 +01:00
icoType: 'trash',
2019-07-10 09:12:48 +02:00
onClick: () => {
2019-11-28 15:57:39 +01:00
emitEvent('willDeleteEntryFromList');
2019-07-10 09:12:48 +02:00
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));