diff --git a/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/entity_form.tsx b/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/entity_form.tsx index 5751e32c8c0eb..d5f0c09f17e00 100644 --- a/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/entity_form.tsx +++ b/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/entity_form.tsx @@ -39,6 +39,7 @@ function getGeoFields(fields: DataViewField[]) { interface Props { data: DataPublicPluginStart; + getValidationError: (key: string) => string | null; ruleParams: GeoContainmentAlertParams; setDataViewId: (id: string) => void; setDataViewTitle: (title: string) => void; @@ -86,6 +87,44 @@ export const EntityForm = (props: Props) => { }; }, [props.ruleParams.indexId]); + function getDataViewError() { + const validationError = props.getValidationError('index'); + if (validationError) { + return validationError; + } + + if (dataView && dateFields.length === 0) { + return i18n.translate('xpack.stackAlerts.geoContainment.noDateFieldInIndexPattern.message', { + defaultMessage: + 'Data view does not contain date fields.', + }); + } + + if (dataView && geoFields.length === 0) { + return i18n.translate('xpack.stackAlerts.geoContainment.noGeoFieldInIndexPattern.message', { + defaultMessage: + 'Data view does not contain geospatial fields. Must have one of type: {geoFieldTypes}.', + values: { + geoFieldTypes: ENTITY_GEO_FIELD_TYPES.join(', '), + }, + }); + } + + if (dataViewNotFound) { + return i18n.translate('xpack.stackAlerts.geoContainment.dataViewNotFound', { + defaultMessage: `Unable to find data view '{id}'`, + values: { id: props.ruleParams.indexId }, + }); + } + + return null; + } + + const dataViewError = getDataViewError(); + const dateFieldError = props.getValidationError('dateField'); + const geoFieldError = props.getValidationError('geoField'); + const entityFieldError = props.getValidationError('entity'); + return ( @@ -101,6 +140,8 @@ export const EntityForm = (props: Props) => { { { props.setDataViewId(dataView.id); props.setDataViewTitle(dataView.title); + const dateFields = getDateFields(dataView.fields); - props.setDateField(dateFields.length ? dateFields[0].name : ''); + if (dateFields.length) { + props.setDateField(dateFields[0].name); + } else if ('dateField' in props.ruleParams) { + props.setDateField(''); + } + // do not attempt to auto select entity field // there can be many matches so auto selecting the correct field is improbable - props.setEntityField(''); + if ('entity' in props.ruleParams) { + props.setEntityField(''); + } + const geoFields = getGeoFields(dataView.fields); - props.setGeoField(geoFields.length ? geoFields[0].name : ''); + if (geoFields.length) { + props.setGeoField(geoFields[0].name); + } else if ('geoField' in props.ruleParams) { + props.setGeoField(''); + } }} unifiedSearch={props.unifiedSearch} /> @@ -127,6 +181,8 @@ export const EntityForm = (props: Props) => { {props.ruleParams.indexId && <> { } > { { > = (props) => { - console.log('props', props); + + function getValidationError(key: string) { + return props.errors[key]?.length > 0 && key in props.ruleParams + ? props.errors[key][0] + : null; + } + return ( <> props.setRuleParams('indexId', id)} setDataViewTitle={(title: string) => props.setRuleParams('index', title)} diff --git a/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/single_field_select.tsx b/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/single_field_select.tsx index dc18e1f95e707..6f8bb8a857cb0 100644 --- a/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/single_field_select.tsx +++ b/x-pack/plugins/stack_alerts/public/rule_types/geo_containment/expression/single_field_select.tsx @@ -33,13 +33,14 @@ function fieldsToOptions(fields?: DataViewField[]): Array void; fields: DataViewField[]; } -export function SingleFieldSelect({ placeholder, value, onChange, fields }: Props) { +export function SingleFieldSelect({ isInvalid, placeholder, value, onChange, fields }: Props) { function renderOption( option: EuiComboBoxOptionOption, searchValue: string, @@ -71,6 +72,7 @@ export function SingleFieldSelect({ placeholder, value, onChange, fields }: Prop return (