Skip to content

Commit

Permalink
elastic#69750 Auto-expand inputs with required & empty (invalid) vars
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Jul 10, 2020
1 parent 8b1bb92 commit d3301ac
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
RegistryInput,
RegistryStream,
} from '../../../../types';
import { PackageConfigInputValidationResults, countValidationErrors } from '../services';
import {
PackageConfigInputValidationResults,
hasInvalidButRequiredVar,
countValidationErrors,
} from '../services';
import { PackageConfigInputConfig } from './package_config_input_config';
import { PackageConfigInputStreamConfig } from './package_config_input_stream';

Expand All @@ -33,6 +37,29 @@ const ShortenedHorizontalRule = styled(EuiHorizontalRule)`
}
`;

const shouldShowStreamsByDefault = (
packageInput: RegistryInput,
packageInputStreams: Array<RegistryStream & { dataset: { name: string } }>,
packageConfigInput: PackageConfigInput
): boolean => {
return (
packageConfigInput.enabled &&
(hasInvalidButRequiredVar(packageInput.vars, packageConfigInput.vars) ||
Boolean(
packageInputStreams.find(
(stream) =>
stream.enabled &&
hasInvalidButRequiredVar(
stream.vars,
packageConfigInput.streams.find(
(pkgStream) => stream.dataset.name === pkgStream.dataset.name
)?.vars
)
)
))
);
};

export const PackageConfigInputPanel: React.FunctionComponent<{
packageInput: RegistryInput;
packageInputStreams: Array<RegistryStream & { dataset: { name: string } }>;
Expand All @@ -50,7 +77,9 @@ export const PackageConfigInputPanel: React.FunctionComponent<{
forceShowErrors,
}) => {
// Showing streams toggle state
const [isShowingStreams, setIsShowingStreams] = useState<boolean>(false);
const [isShowingStreams, setIsShowingStreams] = useState<boolean>(
shouldShowStreamsByDefault(packageInput, packageInputStreams, packageConfigInput)
);

// Errors state
const errorCount = countValidationErrors(inputValidationResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const PackageConfigInputStreamConfig: React.FunctionComponent<{
forceShowErrors,
}) => {
// Showing advanced options toggle state
const [isShowingAdvanced, setIsShowingAdvanced] = useState<boolean>(false);
const [isShowingAdvanced, setIsShowingAdvanced] = useState<boolean>();

// Errors state
const hasErrors = forceShowErrors && validationHasErrors(inputStreamValidationResults);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 { hasInvalidButRequiredVar } from './has_invalid_but_required_var';

describe('Ingest Manager - hasInvalidButRequiredVar', () => {
it('returns true for invalid & required vars', () => {
expect(
hasInvalidButRequiredVar(
[
{
name: 'mock_var',
type: 'text',
required: true,
},
],
{}
)
).toBe(true);

expect(
hasInvalidButRequiredVar(
[
{
name: 'mock_var',
type: 'text',
required: true,
},
],
{
mock_var: {
value: undefined,
},
}
)
).toBe(true);
});

it('returns false for valid & required vars', () => {
expect(
hasInvalidButRequiredVar(
[
{
name: 'mock_var',
type: 'text',
required: true,
},
],
{
mock_var: {
value: 'foo',
},
}
)
).toBe(false);
});

it('returns false for optional vars', () => {
expect(
hasInvalidButRequiredVar(
[
{
name: 'mock_var',
type: 'text',
},
],
{
mock_var: {
value: 'foo',
},
}
)
).toBe(false);

expect(
hasInvalidButRequiredVar(
[
{
name: 'mock_var',
type: 'text',
required: false,
},
],
{
mock_var: {
value: undefined,
},
}
)
).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 { PackageConfigConfigRecord, RegistryVarsEntry } from '../../../../types';
import { validatePackageConfigConfig } from './';

export const hasInvalidButRequiredVar = (
registryVars?: RegistryVarsEntry[],
packageConfigVars?: PackageConfigConfigRecord
): boolean => {
return (
!packageConfigVars ||
Boolean(
registryVars &&
registryVars.find(
(registryVar) =>
registryVar.required &&
(!packageConfigVars[registryVar.name] ||
validatePackageConfigConfig(packageConfigVars[registryVar.name], registryVar)?.length)
)
)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/
export { isAdvancedVar } from './is_advanced_var';
export { hasInvalidButRequiredVar } from './has_invalid_but_required_var';
export {
PackageConfigValidationResults,
PackageConfigConfigValidationResults,
PackageConfigInputValidationResults,
validatePackageConfig,
validatePackageConfigConfig,
validationHasErrors,
countValidationErrors,
} from './validate_package_config';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { RegistryVarsEntry } from '../../../../types';

export const isAdvancedVar = (varDef: RegistryVarsEntry): boolean => {
if (varDef.show_user || (varDef.required && !varDef.default)) {
if (varDef.show_user || (varDef.required && varDef.default === undefined)) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const validatePackageConfig = (
return validationResults;
};

const validatePackageConfigConfig = (
export const validatePackageConfigConfig = (
configEntry: PackageConfigConfigRecordEntry,
varDef: RegistryVarsEntry
): string[] | null => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
UpdatePackageConfig,
PackageConfigInput,
PackageConfigInputStream,
PackageConfigConfigRecord,
PackageConfigConfigRecordEntry,
Output,
DataStream,
Expand Down

0 comments on commit d3301ac

Please sign in to comment.