Skip to content

Commit

Permalink
add object id column to csv export (#5971)
Browse files Browse the repository at this point in the history
closes: #5893

---------

Co-authored-by: Félix Malfait <[email protected]>
  • Loading branch information
AdityaPimpalkar and FelixMalfait authored Jun 20, 2024
1 parent bc8c895 commit 59b9ce6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('generateCsv', () => {
] as ColumnDefinition<FieldMetadata>[];
const rows = [
{
id: '1',
bar: 'another field',
empty: null,
foo: 'some field',
Expand All @@ -48,8 +49,8 @@ describe('generateCsv', () => {
},
];
const csv = generateCsv({ columns, rows });
expect(csv).toEqual(`Foo,Empty,Nested Foo,Nested Nested,Relation
some field,,foo,nested,a relation`);
expect(csv).toEqual(`id,Foo,Empty,Nested Foo,Nested Nested,Relation
1,some field,,foo,nested,a relation`);
});
});

Expand All @@ -62,6 +63,7 @@ describe('csvDownloader', () => {
{ id: 2, name: 'Alice' },
],
columns: [],
objectNameSingular: '',
};

const link = document.createElement('a');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates';
import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';
import { FieldMetadataType } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
import { sleep } from '~/utils/sleep';
Expand Down Expand Up @@ -45,14 +46,27 @@ export const generateCsv: GenerateExport = ({
col.metadata.relationType === 'TO_ONE_OBJECT',
);

const keys = columnsToExport.flatMap((col) => {
const objectIdColumn: ColumnDefinition<FieldMetadata> = {
fieldMetadataId: '',
type: FieldMetadataType.Uuid,
iconName: '',
label: `Id`,
metadata: {
fieldName: 'id',
},
position: 0,
size: 0,
};

const columnsToExportWithIdColumn = [objectIdColumn, ...columnsToExport];

const keys = columnsToExportWithIdColumn.flatMap((col) => {
const column = {
field: `${col.metadata.fieldName}${col.type === 'RELATION' ? 'Id' : ''}`,
title: [col.label, col.type === 'RELATION' ? 'Id' : null]
.filter(isDefined)
.join(' '),
};

const fieldsWithSubFields = rows.find((row) => {
const fieldValue = (row as any)[column.field];
const hasSubFields =
Expand Down

0 comments on commit 59b9ce6

Please sign in to comment.