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

[Upgrade Assistant] Fix filter deprecations search filter #57541

Merged
merged 9 commits into from
Feb 17, 2020

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class CheckupTab extends UpgradeAssistantTabComponent<CheckupTabProps, Ch
setSelectedTabIndex,
showBackupWarning = false,
} = this.props;
const { currentFilter, search, currentGroupBy } = this.state;
const { currentFilter, currentGroupBy } = this.state;

return (
<Fragment>
Expand Down Expand Up @@ -143,7 +143,6 @@ export class CheckupTab extends UpgradeAssistantTabComponent<CheckupTabProps, Ch
loadData={refreshCheckupData}
currentFilter={currentFilter}
onFilterChange={this.changeFilter}
search={search}
onSearchChange={this.changeSearch}
availableGroupByOptions={this.availableGroupByOptions()}
currentGroupBy={currentGroupBy}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,112 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { FunctionComponent } from 'react';

import { EuiButton, EuiFieldSearch, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { FormattedMessage, injectI18n } from '@kbn/i18n/react';
import React, { FunctionComponent, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButton, EuiFieldSearch, EuiFlexGroup, EuiFlexItem, EuiCallOut } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';

import { DeprecationInfo } from 'src/legacy/core_plugins/elasticsearch';
import { GroupByOption, LevelFilterOption, LoadingState } from '../../types';
import { FilterBar } from './filter_bar';
import { GroupByBar } from './group_by_bar';

interface CheckupControlsProps extends ReactIntl.InjectedIntlProps {
import { validateRegExpString } from '../../../utils';

interface CheckupControlsProps {
allDeprecations?: DeprecationInfo[];
loadingState: LoadingState;
loadData: () => void;
currentFilter: LevelFilterOption;
onFilterChange: (filter: LevelFilterOption) => void;
search: string;
onSearchChange: (filter: string) => void;
availableGroupByOptions: GroupByOption[];
currentGroupBy: GroupByOption;
onGroupByChange: (groupBy: GroupByOption) => void;
}

export const CheckupControlsUI: FunctionComponent<CheckupControlsProps> = ({
export const CheckupControls: FunctionComponent<CheckupControlsProps> = ({
allDeprecations,
loadingState,
loadData,
currentFilter,
onFilterChange,
search,
onSearchChange,
availableGroupByOptions,
currentGroupBy,
onGroupByChange,
intl,
}) => (
<EuiFlexGroup alignItems="center" wrap={true} responsive={false}>
<EuiFlexItem grow={true}>
<EuiFieldSearch
aria-label="Filter"
placeholder={intl.formatMessage({
id: 'xpack.upgradeAssistant.checkupTab.controls.searchBarPlaceholder',
defaultMessage: 'Filter',
})}
value={search}
onChange={e => onSearchChange(e.target.value)}
/>
</EuiFlexItem>

{/* These two components provide their own EuiFlexItem wrappers */}
<FilterBar {...{ allDeprecations, currentFilter, onFilterChange }} />
<GroupByBar {...{ availableGroupByOptions, currentGroupBy, onGroupByChange }} />
}) => {
const [searchTermError, setSearchTermError] = useState<null | string>(null);
const filterInvalid = Boolean(searchTermError);
return (
<EuiFlexGroup direction="column" responsive={false}>
<EuiFlexItem grow={true}>
<EuiFlexGroup alignItems="center" wrap={true} responsive={false}>
<EuiFlexItem>
<EuiFieldSearch
isInvalid={filterInvalid}
aria-label={i18n.translate(
'xpack.upgradeAssistant.checkupTab.controls.searchBarPlaceholderAriaLabel',
{ defaultMessage: 'Filter' }
)}
placeholder={i18n.translate(
'xpack.upgradeAssistant.checkupTab.controls.searchBarPlaceholder',
{
defaultMessage: 'Filter',
}
)}
onChange={e => {
const string = e.target.value;
const errorMessage = validateRegExpString(string);
if (errorMessage) {
// Emit an empty search term to listeners if search term is invalid.
onSearchChange('');
setSearchTermError(errorMessage);
} else {
onSearchChange(e.target.value);
if (searchTermError) {
setSearchTermError(null);
}
}
}}
/>
</EuiFlexItem>

<EuiFlexItem grow={false}>
<EuiButton
fill
onClick={loadData}
iconType="refresh"
isLoading={loadingState === LoadingState.Loading}
>
<FormattedMessage
id="xpack.upgradeAssistant.checkupTab.controls.refreshButtonLabel"
defaultMessage="Refresh"
/>
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
);
{/* These two components provide their own EuiFlexItem wrappers */}
<FilterBar {...{ allDeprecations, currentFilter, onFilterChange }} />
<GroupByBar {...{ availableGroupByOptions, currentGroupBy, onGroupByChange }} />

export const CheckupControls = injectI18n(CheckupControlsUI);
<EuiFlexItem grow={false}>
<EuiButton
fill
onClick={loadData}
iconType="refresh"
isLoading={loadingState === LoadingState.Loading}
>
<FormattedMessage
id="xpack.upgradeAssistant.checkupTab.controls.refreshButtonLabel"
defaultMessage="Refresh"
/>
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
{filterInvalid && (
<EuiFlexItem grow={false}>
<EuiCallOut
color="danger"
title={i18n.translate(
'xpack.upgradeAssistant.checkupTab.controls.filterErrorMessageLabel',
{
defaultMessage: 'Filter invalid: {searchTermError}',
values: { searchTermError },
}
)}
iconType="faceSad"
/>
</EuiFlexItem>
)}
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { validateRegExpString } from './utils';

describe('validRegExpString', () => {
it('correctly returns false for invalid strings', () => {
expect(validateRegExpString('?asd')).toContain(`Invalid regular expression`);
expect(validateRegExpString('*asd')).toContain(`Invalid regular expression`);
expect(validateRegExpString('(')).toContain(`Invalid regular expression`);
});

it('correctly returns true for valid strings', () => {
expect(validateRegExpString('asd')).toBe('');
expect(validateRegExpString('.*asd')).toBe('');
expect(validateRegExpString('')).toBe('');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { pipe } from 'fp-ts/lib/pipeable';
import { tryCatch, fold } from 'fp-ts/lib/Either';

export const validateRegExpString = (s: string) =>
pipe(
tryCatch(
() => new RegExp(s),
e => (e as Error).message
),
fold(
(errorMessage: string) => errorMessage,
() => ''
)
);