feat: do not show disabled fields in table (#2319)

Closes #2309
This commit is contained in:
Thaïs 2023-11-03 15:05:04 +01:00 committed by GitHub
parent e053fba089
commit 60b1024efb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 55 deletions

View File

@ -18,6 +18,18 @@ import { generateUpdateOneObjectMutation } from '../utils/generateUpdateOneObjec
import { useFindManyObjectMetadataItems } from './useFindManyObjectMetadataItems';
const EMPTY_QUERY = gql`
query EmptyQuery {
empty
}
`;
const EMPTY_MUTATION = gql`
mutation EmptyMutation {
empty
}
`;
export const useFindOneObjectMetadataItem = ({
objectNamePlural,
objectNameSingular,
@ -39,81 +51,64 @@ export const useFindOneObjectMetadataItem = ({
objectMetadataItems.length === 0 ||
(objectMetadataItems.length > 0 && !foundObjectMetadataItem);
const activeFields =
foundObjectMetadataItem?.fields.filter(({ isActive }) => isActive) ?? [];
const columnDefinitions: ColumnDefinition<FieldMetadata>[] =
foundObjectMetadataItem?.fields.map((field, index) =>
formatMetadataFieldAsColumnDefinition({
position: index,
field,
objectMetadataItem: foundObjectMetadataItem,
icons,
}),
) ?? [];
foundObjectMetadataItem
? activeFields.map((field, index) =>
formatMetadataFieldAsColumnDefinition({
position: index,
field,
objectMetadataItem: foundObjectMetadataItem,
icons,
}),
)
: [];
const filterDefinitions: FilterDefinition[] =
foundObjectMetadataItem?.fields.map((field) =>
formatMetadataFieldAsFilterDefinition({
field,
icons,
}),
) ?? [];
const filterDefinitions: FilterDefinition[] = activeFields.map((field) =>
formatMetadataFieldAsFilterDefinition({
field,
icons,
}),
);
const sortDefinitions: SortDefinition[] =
foundObjectMetadataItem?.fields.map((field) =>
formatMetadataFieldAsSortDefinition({
field,
icons,
}),
) ?? [];
const sortDefinitions: SortDefinition[] = activeFields.map((field) =>
formatMetadataFieldAsSortDefinition({
field,
icons,
}),
);
const findManyQuery = foundObjectMetadataItem
? generateFindManyCustomObjectsQuery({
objectMetadataItem: foundObjectMetadataItem,
})
: gql`
query EmptyQuery {
empty
}
`;
: EMPTY_QUERY;
const findOneQuery = foundObjectMetadataItem
? generateFindOneCustomObjectQuery({
objectMetadataItem: foundObjectMetadataItem,
})
: gql`
query EmptyQuery {
empty
}
`;
: EMPTY_QUERY;
const createOneMutation = foundObjectMetadataItem
? generateCreateOneObjectMutation({
objectMetadataItem: foundObjectMetadataItem,
})
: gql`
mutation EmptyMutation {
empty
}
`;
: EMPTY_MUTATION;
const updateOneMutation = foundObjectMetadataItem
? generateUpdateOneObjectMutation({
objectMetadataItem: foundObjectMetadataItem,
})
: gql`
mutation EmptyMutation {
empty
}
`;
: EMPTY_MUTATION;
const deleteOneMutation = foundObjectMetadataItem
? generateDeleteOneObjectMutation({
objectMetadataItem: foundObjectMetadataItem,
})
: gql`
mutation EmptyMutation {
empty
}
`;
: EMPTY_MUTATION;
return {
foundObjectMetadataItem,

View File

@ -1,5 +1,7 @@
import { IconComponent } from '@/ui/display/icon/types/IconComponent';
import { FieldMetadata } from '@/ui/object/field/types/FieldMetadata';
import { ColumnDefinition } from '@/ui/object/record-table/types/ColumnDefinition';
import { Field } from '~/generated-metadata/graphql';
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
@ -12,9 +14,9 @@ export const formatMetadataFieldAsColumnDefinition = ({
icons,
}: {
position: number;
field: ObjectMetadataItem['fields'][0];
field: Field;
objectMetadataItem: Omit<ObjectMetadataItem, 'fields'>;
icons: Record<string, any>;
icons: Record<string, IconComponent>;
}): ColumnDefinition<FieldMetadata> => ({
position,
fieldId: field.id,

View File

@ -1,15 +1,23 @@
import { selectorFamily } from 'recoil';
import { availableTableColumnsScopedState } from '../availableTableColumnsScopedState';
import { tableColumnsScopedState } from '../tableColumnsScopedState';
export const visibleTableColumnsScopedSelector = selectorFamily({
key: 'visibleTableColumnsScopedSelector',
get:
(scopeId: string) =>
({ get }) =>
[
...get(tableColumnsScopedState(scopeId)).filter(
(column) => column.isVisible,
),
].sort((a, b) => a.position - b.position),
({ get }) => {
const columns = get(tableColumnsScopedState(scopeId));
const availableColumnKeys = get(
availableTableColumnsScopedState(scopeId),
).map(({ fieldId }) => fieldId);
return columns
.filter(
(column) =>
column.isVisible && availableColumnKeys.includes(column.fieldId),
)
.toSorted((a, b) => a.position - b.position);
},
});