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

[BUG][Discover] Allow default columns settings #5261

Merged
merged 2 commits into from
Oct 10, 2023
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 @@ -11,7 +11,7 @@
import { buildColumns } from '../columns';
import * as utils from './common';
import { SortOrder } from '../../../saved_searches/types';
import { PLUGIN_ID } from '../../../../common';
import { DEFAULT_COLUMNS_SETTING, PLUGIN_ID } from '../../../../common';

export interface DiscoverState {
/**
Expand Down Expand Up @@ -57,6 +57,7 @@

export const getPreloadedState = async ({
getSavedSearchById,
uiSettings: config,
}: DiscoverServices): Promise<DefaultViewState<DiscoverState>> => {
const preloadedState: DefaultViewState<DiscoverState> = {
state: {
Expand Down Expand Up @@ -86,6 +87,8 @@

savedSearchInstance.destroy(); // this instance is no longer needed, will create another one later
}
} else if (config.get(DEFAULT_COLUMNS_SETTING)) {
preloadedState.state.columns = config.get(DEFAULT_COLUMNS_SETTING);

Check warning on line 91 in src/plugins/discover/public/application/utils/state_management/discover_slice.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/utils/state_management/discover_slice.tsx#L91

Added line #L91 was not covered by tests
}

return preloadedState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ describe('filterColumns', () => {
getAll: () => [{ name: 'a' }, { name: 'c' }, { name: 'd' }],
},
} as IndexPattern;
const defaultColumns = ['_defaultColumn'];

it('should return columns that exist in the index pattern fields', () => {
const columns = ['a', 'b'];
const result = filterColumns(columns, indexPatternMock, defaultColumns);
const result = filterColumns(columns, indexPatternMock, ['a']);
expect(result).toEqual(['a']);
});

it('should return defaultColumns if no columns exist in the index pattern fields', () => {
const columns = ['b', 'e'];
const result = filterColumns(columns, indexPatternMock, defaultColumns);
expect(result).toEqual(defaultColumns);
const result = filterColumns(columns, indexPatternMock, ['e']);
expect(result).toEqual(['_source']);
});

it('should return defaultColumns if no columns and indexPattern is null', () => {
it('should return defaultColumns if no columns and indexPattern is undefined', () => {
const columns = ['b', 'e'];
const result = filterColumns(columns, null, defaultColumns);
expect(result).toEqual(defaultColumns);
const result = filterColumns(columns, undefined, ['a']);
expect(result).toEqual(['_source']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function filterColumns(
defaultColumns: string[]
) {
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || [];
const filteredColumns = columns.filter((column) => fieldsName.includes(column));
return filteredColumns.length > 0 ? filteredColumns : defaultColumns;
// combine columns and defaultColumns without duplicates
const combinedColumns = [...new Set([...columns, ...defaultColumns])];
const filteredColumns = combinedColumns.filter((column) => fieldsName.includes(column));
return filteredColumns.length > 0 ? filteredColumns : ['_source'];
}
Loading