From d3301ac0d1b298b44c26abdac64210948b157673 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Thu, 9 Jul 2020 17:12:29 -0700 Subject: [PATCH] #69750 Auto-expand inputs with required & empty (invalid) vars --- .../components/package_config_input_panel.tsx | 33 ++++++- .../package_config_input_stream.tsx | 2 +- .../has_invalid_but_required_var.test.ts | 94 +++++++++++++++++++ .../services/has_invalid_but_required_var.ts | 25 +++++ .../services/index.ts | 2 + .../services/is_advanced_var.ts | 2 +- .../services/validate_package_config.ts | 2 +- .../ingest_manager/types/index.ts | 1 + 8 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.test.ts create mode 100644 x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.ts diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_panel.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_panel.tsx index 219dcd03076b7..af26afdbf74d7 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_panel.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_panel.tsx @@ -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'; @@ -33,6 +37,29 @@ const ShortenedHorizontalRule = styled(EuiHorizontalRule)` } `; +const shouldShowStreamsByDefault = ( + packageInput: RegistryInput, + packageInputStreams: Array, + 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; @@ -50,7 +77,9 @@ export const PackageConfigInputPanel: React.FunctionComponent<{ forceShowErrors, }) => { // Showing streams toggle state - const [isShowingStreams, setIsShowingStreams] = useState(false); + const [isShowingStreams, setIsShowingStreams] = useState( + shouldShowStreamsByDefault(packageInput, packageInputStreams, packageConfigInput) + ); // Errors state const errorCount = countValidationErrors(inputValidationResults); diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_stream.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_stream.tsx index 5e581e4c9565f..11a9df276485b 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_stream.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/components/package_config_input_stream.tsx @@ -38,7 +38,7 @@ export const PackageConfigInputStreamConfig: React.FunctionComponent<{ forceShowErrors, }) => { // Showing advanced options toggle state - const [isShowingAdvanced, setIsShowingAdvanced] = useState(false); + const [isShowingAdvanced, setIsShowingAdvanced] = useState(); // Errors state const hasErrors = forceShowErrors && validationHasErrors(inputStreamValidationResults); diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.test.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.test.ts new file mode 100644 index 0000000000000..679ae4b1456d6 --- /dev/null +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.test.ts @@ -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); + }); +}); diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.ts new file mode 100644 index 0000000000000..8471c2b0770bf --- /dev/null +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/has_invalid_but_required_var.ts @@ -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) + ) + ) + ); +}; diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/index.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/index.ts index d8c8a42aeb908..0d33a4e113f03 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/index.ts +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/index.ts @@ -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'; diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/is_advanced_var.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/is_advanced_var.ts index 398f1d675c5df..a2f4a6675ac80 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/is_advanced_var.ts +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/is_advanced_var.ts @@ -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; diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/validate_package_config.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/validate_package_config.ts index bb0c2e9a2d844..bd9d216ca969a 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/validate_package_config.ts +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/services/validate_package_config.ts @@ -171,7 +171,7 @@ export const validatePackageConfig = ( return validationResults; }; -const validatePackageConfigConfig = ( +export const validatePackageConfigConfig = ( configEntry: PackageConfigConfigRecordEntry, varDef: RegistryVarsEntry ): string[] | null => { diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/types/index.ts b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/types/index.ts index 170a9cedc08d9..dc27da18bc008 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/types/index.ts +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/types/index.ts @@ -18,6 +18,7 @@ export { UpdatePackageConfig, PackageConfigInput, PackageConfigInputStream, + PackageConfigConfigRecord, PackageConfigConfigRecordEntry, Output, DataStream,