builds a centralized way to format fields for admin use

This commit is contained in:
James
2020-05-22 11:54:16 -04:00
parent ad9a28916f
commit 0a23684e80
3 changed files with 24 additions and 9 deletions

View File

@@ -2,7 +2,7 @@
.table {
margin-bottom: $baseline;
overflow-x: scroll;
overflow: auto;
max-width: 100%;
thead {

View File

@@ -11,6 +11,7 @@ import Pill from '../../../elements/Pill';
import Button from '../../../elements/Button';
import SortColumn from '../../../elements/SortColumn';
import Table from '../../../elements/Table';
import formatAdminFields from '../../../../utilities/formatAdminFields';
import './index.scss';
@@ -165,8 +166,6 @@ const ListView = (props) => {
collection,
collection: {
slug,
fields,
timestamps,
labels: {
plural,
},
@@ -174,11 +173,6 @@ const ListView = (props) => {
} = props;
const { setStepNav } = useStepNav();
let allFields = [...fields, { name: 'id', label: 'ID' }];
if (timestamps) {
allFields = allFields.concat([{ name: 'createdAt', label: 'Created At' }, { name: 'updatedAt', label: 'Updated At' }]);
}
useEffect(() => {
setStepNav([
@@ -189,10 +183,11 @@ const ListView = (props) => {
}, [setStepNav, plural]);
const List = customComponents?.[slug]?.views?.List || DefaultList;
const formattedFields = formatAdminFields(collection);
return (
<>
<List collection={{ ...collection, fields: allFields }} />
<List collection={{ ...collection, fields: formattedFields }} />
</>
);
};

View File

@@ -0,0 +1,20 @@
const formatAdminFields = (config) => {
let formattedFields = config.fields.reduce((formatted, field) => {
if (field.hidden === true || field?.hidden?.admin === true) {
return formatted;
}
return [
...formatted,
field,
];
}, [{ name: 'id', label: 'ID' }]);
if (config.timestamps) {
formattedFields = formattedFields.concat([{ name: 'createdAt', label: 'Created At' }, { name: 'updatedAt', label: 'Updated At' }]);
}
return formattedFields;
};
export default formatAdminFields;