diff --git a/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.test.tsx b/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.test.tsx index 3b0e0447c92..576379c0257 100644 --- a/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.test.tsx +++ b/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.test.tsx @@ -159,8 +159,14 @@ describe('EditColumnsModal', () => { await waitFor(() => { expect(setColumns).toHaveBeenCalledWith([ - { ...DEFAULT_COLUMNS[0], title: 'newCol1', type: 'STRING', comment: 'new comment' }, - { ...DEFAULT_COLUMNS[1], type: 'INT' } + { + ...DEFAULT_COLUMNS[0], + title: 'newCol1', + type: 'STRING', + comment: 'new comment', + isPrimaryKey: false + }, + { ...DEFAULT_COLUMNS[1], type: 'INT', isPrimaryKey: false } ]); expect(closeModal).toHaveBeenCalled(); }); @@ -263,8 +269,8 @@ describe('EditColumnsModal', () => { await waitFor(() => { expect(setColumns).toHaveBeenCalledWith([ - { ...duplicateColumns[0], title: 'col1', type: 'STRING' }, - { ...duplicateColumns[1], title: 'col2_fixed', type: 'INT' } + { ...duplicateColumns[0], title: 'col1', type: 'STRING', isPrimaryKey: false }, + { ...duplicateColumns[1], title: 'col2_fixed', type: 'INT', isPrimaryKey: false } ]); }); }); @@ -312,8 +318,14 @@ describe('EditColumnsModal', () => { await waitFor(() => { expect(setColumns).toHaveBeenCalledWith([ - { ...columnsWithEmpty[0], title: 'fixed_name', type: 'STRING' }, - { ...columnsWithEmpty[1], type: 'INT' } + { + ...columnsWithEmpty[0], + title: 'fixed_name', + type: 'STRING', + comment: 'comment1', + isPrimaryKey: false + }, + { ...columnsWithEmpty[1], type: 'INT', comment: 'comment2', isPrimaryKey: false } ]); }); }); diff --git a/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.tsx b/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.tsx index 0ceb124ec49..ee2d2a290e8 100644 --- a/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.tsx +++ b/desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.tsx @@ -20,7 +20,7 @@ import Modal from 'cuix/dist/components/Modal'; import Table from 'cuix/dist/components/Table'; import Input from 'cuix/dist/components/Input'; import Select from 'cuix/dist/components/Select'; -import { Alert } from 'antd'; +import { Alert, Checkbox } from 'antd'; import { SQL_TYPE_MAPPING_API_URL } from '../../../admin/Components/utils'; import useLoadData from '../../../../utils/hooks/useLoadData/useLoadData'; import LoadingErrorWrapper from '../../../../reactComponents/LoadingErrorWrapper/LoadingErrorWrapper'; @@ -39,6 +39,7 @@ interface EditableRow extends Required { type: string; sample: string; comment: string; + isPrimaryKey: boolean; } interface EditColumnsModalProps { @@ -190,15 +191,25 @@ const EditColumnsModal = ({ title: col.title, type: (col.type || 'string').toUpperCase(), sample: sample && sample[col.dataIndex] !== undefined ? String(sample[col.dataIndex]) : '', - comment: col.comment || '' + comment: col.comment || '', + isPrimaryKey: col.isPrimaryKey || false })) ); }, [columns, sample]); - const handleChange = (rowIndex: number, field: keyof EditableRow, value: string) => { - setEditableRows(rows => - rows.map((row, i) => (i === rowIndex ? { ...row, [field]: value } : row)) - ); + const handleChange = (rowIndex: number, field: keyof EditableRow, value: string | boolean) => { + if (field === 'isPrimaryKey' && value === true) { + setEditableRows(rows => + rows.map((row, i) => ({ + ...row, + isPrimaryKey: i === rowIndex ? true : false + })) + ); + } else { + setEditableRows(rows => + rows.map((row, i) => (i === rowIndex ? { ...row, [field]: value } : row)) + ); + } }; const handleDone = async () => { @@ -210,13 +221,26 @@ const EditColumnsModal = ({ ...columns[row.key], title: row.title.trim(), type: row.type, - comment: row.comment + comment: row.comment, + isPrimaryKey: row.isPrimaryKey })); setColumns(updatedColumns); closeModal(); }; const modalColumns = [ + { + title: t('P Key'), + dataIndex: 'isPrimaryKey', + className: 'hue-importer-edit-columns-modal__primary-key', + render: (isPrimaryKey: boolean, _: EditableRow, rowIndex: number) => ( + handleChange(rowIndex, 'isPrimaryKey', true)} + aria-label={t('Set as primary key')} + /> + ) + }, { title: t('Title'), dataIndex: 'title', diff --git a/desktop/core/src/desktop/js/apps/newimporter/types.ts b/desktop/core/src/desktop/js/apps/newimporter/types.ts index 7fa9c902ec7..6cd536c8351 100644 --- a/desktop/core/src/desktop/js/apps/newimporter/types.ts +++ b/desktop/core/src/desktop/js/apps/newimporter/types.ts @@ -69,6 +69,7 @@ export interface FileMetaData { export interface BaseColumnProperties { type?: string; comment?: string; + isPrimaryKey?: boolean; } export interface FilePreviewTableColumn extends BaseColumnProperties {