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

fix(Schema): display keys in right order #596

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
30 changes: 19 additions & 11 deletions src/containers/Tenant/Schema/SchemaViewer/SchemaViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useMemo} from 'react';
import cn from 'bem-cn-lite';

import DataTable, {Column} from '@gravity-ui/react-data-table';
Expand Down Expand Up @@ -28,6 +29,16 @@ interface SchemaViewerProps {
}

export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaViewerProps) => {
// Keys should be displayd by their order in keyColumnIds (Primary Key)
const keyColumnsOrderValues = useMemo(() => {
return keyColumnIds.reduce<Record<number, number>>((result, keyColumnId, index) => {
// Put columns with negative values, so they will be the first with ascending sort
// Minus keyColumnIds.length for the first key, -1 for the last
result[keyColumnId] = index - keyColumnIds.length;
return result;
}, {});
}, [keyColumnIds]);

let dataTableColumns: Column<TColumnDescription>[] = [
{
name: SchemaViewerColumns.id,
Expand All @@ -36,8 +47,11 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
{
name: SchemaViewerColumns.key,
width: 40,
// Table should start with key columns on sort click
defaultOrder: DataTable.ASCENDING,
sortAccessor: (row) => {
return row.Id && keyColumnIds.includes(row.Id) ? 1 : 0;
// Values in keyColumnsOrderValues are always negative, so it will be 1 for not key columns
return (row.Id && keyColumnsOrderValues[row.Id]) || 1;
},
render: ({row}) => {
return row.Id && keyColumnIds.includes(row.Id) ? (
Expand All @@ -58,6 +72,8 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
{
name: SchemaViewerColumns.notNull,
width: 100,
// Table should start with notNull columns on sort click
defaultOrder: DataTable.DESCENDING,
render: ({row}) => {
if (row.NotNull) {
return '\u2713';
Expand All @@ -75,22 +91,14 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
);
}

// Display key columns first
const tableData = columns.sort((column) => {
if (column.Id && keyColumnIds.includes(column.Id)) {
return 1;
}
return -1;
});

Comment on lines -78 to -85
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialSortOrder should be used instead. Without sorting columns should be displayed as they are in /json/describe response

return (
<div className={b()}>
<DataTable
theme="yandex-cloud"
data={tableData}
data={columns}
columns={dataTableColumns}
settings={DEFAULT_TABLE_SETTINGS}
initialSortOrder={{columnId: SchemaViewerColumns.key, order: DataTable.DESCENDING}}
initialSortOrder={{columnId: SchemaViewerColumns.key, order: DataTable.ASCENDING}}
/>
</div>
);
Expand Down