Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add object id column to csv export #5971

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading