2454 update filter definition to work with new backend (#2482)

* wip

* filters are working

* updated functions

* remove comment

* improve readability
This commit is contained in:
bosiraphael 2023-11-13 18:05:49 +01:00 committed by GitHub
parent 3de2fc72dc
commit 230459b23c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 133 additions and 50 deletions

View File

@ -2558,6 +2558,16 @@ export enum QueryMode {
Insensitive = 'insensitive'
}
export type RelationConnection = {
__typename?: 'RelationConnection';
/** Array of edges. */
edges: Array<RelationEdge>;
/** Paging information */
pageInfo: PageInfo;
/** Fetch total count of records */
totalCount: Scalars['Int'];
};
export enum SortOrder {
Asc = 'asc',
Desc = 'desc'

View File

@ -7,14 +7,12 @@ import { generateFindOneCustomObjectQuery } from '@/object-record/utils/generate
import { generateUpdateOneObjectMutation } from '@/object-record/utils/generateUpdateOneObjectMutation';
import { useLazyLoadIcons } from '@/ui/input/hooks/useLazyLoadIcons';
import { FieldMetadata } from '@/ui/object/field/types/FieldMetadata';
import { FilterDefinition } from '@/ui/object/object-filter-dropdown/types/FilterDefinition';
import { SortDefinition } from '@/ui/object/object-sort-dropdown/types/SortDefinition';
import { ColumnDefinition } from '@/ui/object/record-table/types/ColumnDefinition';
import { ObjectMetadataItemIdentifier } from '../types/ObjectMetadataItemIdentifier';
import { formatFieldMetadataItemAsColumnDefinition } from '../utils/formatFieldMetadataItemAsColumnDefinition';
import { formatFieldMetadataItemAsFilterDefinition } from '../utils/formatFieldMetadataItemAsFilterDefinition';
import { formatFieldMetadataItemAsSortDefinition } from '../utils/formatFieldMetadataItemAsSortDefinition';
import { formatFieldMetadataItemsAsFilterDefinitions } from '../utils/formatFieldMetadataItemsAsFilterDefinitions';
import { formatFieldMetadataItemsAsSortDefinitions } from '../utils/formatFieldMetadataItemsAsSortDefinitions';
import { useFindManyObjectMetadataItems } from './useFindManyObjectMetadataItems';
@ -66,19 +64,15 @@ export const useFindOneObjectMetadataItem = ({
)
: [];
const filterDefinitions: FilterDefinition[] = activeFields.map((field) =>
formatFieldMetadataItemAsFilterDefinition({
field,
icons,
}),
);
const filterDefinitions = formatFieldMetadataItemsAsFilterDefinitions({
fields: activeFields,
icons,
});
const sortDefinitions: SortDefinition[] = activeFields.map((field) =>
formatFieldMetadataItemAsSortDefinition({
field,
icons,
}),
);
const sortDefinitions = formatFieldMetadataItemsAsSortDefinitions({
fields: activeFields,
icons,
});
const findManyQuery = foundObjectMetadataItem
? generateFindManyCustomObjectsQuery({

View File

@ -1,16 +0,0 @@
import { FilterDefinition } from '@/ui/object/object-filter-dropdown/types/FilterDefinition';
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
export const formatFieldMetadataItemAsFilterDefinition = ({
field,
icons,
}: {
field: ObjectMetadataItem['fields'][0];
icons: Record<string, any>;
}): FilterDefinition => ({
fieldMetadataId: field.id,
label: field.label,
Icon: icons[field.icon ?? 'Icon123'],
type: 'TEXT',
});

View File

@ -1,15 +0,0 @@
import { SortDefinition } from '@/ui/object/object-sort-dropdown/types/SortDefinition';
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
export const formatFieldMetadataItemAsSortDefinition = ({
field,
icons,
}: {
field: ObjectMetadataItem['fields'][0];
icons: Record<string, any>;
}): SortDefinition => ({
fieldMetadataId: field.id,
label: field.label,
Icon: icons[field.icon ?? 'Icon123'],
});

View File

@ -0,0 +1,45 @@
import { FilterDefinition } from '@/ui/object/object-filter-dropdown/types/FilterDefinition';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
export const formatFieldMetadataItemsAsFilterDefinitions = ({
fields,
icons,
}: {
fields: Array<ObjectMetadataItem['fields'][0]>;
icons: Record<string, any>;
}): FilterDefinition[] =>
fields.reduce((acc, field) => {
if (
![
FieldMetadataType.Date,
FieldMetadataType.Number,
FieldMetadataType.Text,
].includes(field.type)
) {
return acc;
}
return [
...acc,
formatFieldMetadataItemAsFilterDefinition({ field, icons }),
];
}, [] as FilterDefinition[]);
const formatFieldMetadataItemAsFilterDefinition = ({
field,
icons,
}: {
field: ObjectMetadataItem['fields'][0];
icons: Record<string, any>;
}): FilterDefinition => ({
fieldMetadataId: field.id,
label: field.label,
Icon: icons[field.icon ?? 'Icon123'],
type:
field.type === FieldMetadataType.Date
? 'DATE'
: field.type === FieldMetadataType.Number
? 'NUMBER'
: 'TEXT',
});

View File

@ -0,0 +1,33 @@
import { SortDefinition } from '@/ui/object/object-sort-dropdown/types/SortDefinition';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
export const formatFieldMetadataItemsAsSortDefinitions = ({
fields,
icons,
}: {
fields: Array<ObjectMetadataItem['fields'][0]>;
icons: Record<string, any>;
}): SortDefinition[] =>
fields.reduce((acc, field) => {
if (
![
FieldMetadataType.Date,
FieldMetadataType.Number,
FieldMetadataType.Text,
FieldMetadataType.Boolean,
].includes(field.type)
) {
return acc;
}
return [
...acc,
{
fieldMetadataId: field.id,
label: field.label,
Icon: icons[field.icon ?? 'Icon123'],
},
];
}, [] as SortDefinition[]);

View File

@ -26,8 +26,6 @@ export const RecordTableEffect = () => {
setViewObjectMetadataId,
} = useView();
useRecordTable();
useEffect(() => {
if (!foundObjectMetadataItem) {
return;

View File

@ -45,6 +45,40 @@ export const turnFiltersIntoWhereClauseV2 = (
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
case 'NUMBER':
switch (filter.operand) {
case ViewFilterOperand.GreaterThan:
whereClause[correspondingField.name] = {
gte: parseFloat(filter.value),
};
return;
case ViewFilterOperand.LessThan:
whereClause[correspondingField.name] = {
lte: parseFloat(filter.value),
};
return;
default:
throw new Error(
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
case 'DATE':
switch (filter.operand) {
case ViewFilterOperand.GreaterThan:
whereClause[correspondingField.name] = {
gte: filter.value,
};
return;
case ViewFilterOperand.LessThan:
whereClause[correspondingField.name] = {
lte: filter.value,
};
return;
default:
throw new Error(
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
default:
throw new Error('Unknown filter type');
}

View File

@ -76,7 +76,7 @@ export const SettingsObjectNewFieldStep2 = () => {
});
objectViews.forEach(async (view) => {
await createOneViewField?.({
viewId: view.id,
view: view.id,
fieldMetadataId: createdField.data?.createOneField.id,
position: activeObjectMetadataItem.fields.length,
isVisible: true,