From 008d37c6374d802543811bffdbbc086b20681d50 Mon Sep 17 00:00:00 2001 From: Bill McConaghy Date: Mon, 5 Nov 2018 17:08:38 -0500 Subject: [PATCH] scrolling to first error when user submits form for edit policy --- .../public/lib/find_errors.js | 23 ++++++++++------ .../components/cold_phase/cold_phase.js | 4 +++ .../components/delete_phase/delete_phase.js | 2 ++ .../components/hot_phase/hot_phase.js | 7 ++++- .../components/warm_phase/warm_phase.js | 6 +++++ .../edit_policy/edit_policy.container.js | 26 ++++++++++++------- .../sections/edit_policy/edit_policy.js | 19 +++++++++----- 7 files changed, 61 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/index_lifecycle_management/public/lib/find_errors.js b/x-pack/plugins/index_lifecycle_management/public/lib/find_errors.js index 39d260b05301e..6a95326ee85c9 100644 --- a/x-pack/plugins/index_lifecycle_management/public/lib/find_errors.js +++ b/x-pack/plugins/index_lifecycle_management/public/lib/find_errors.js @@ -4,19 +4,26 @@ * you may not use this file except in compliance with the Elastic License. */ -export const hasErrors = (object, keysToIgnore = []) => { - let errors = false; - for (const [key, value] of Object.entries(object)) { - if (keysToIgnore.includes(key)) continue; +export const findFirstError = (object, topLevel = true) => { + console.log("ERRORS", object); + + let firstError; + const keys = topLevel ? [ 'policyName', 'hot', 'warm', 'cold', 'delete'] : Object.keys(object); + for (const key of keys) { + const value = object[key]; if (Array.isArray(value) && value.length > 0) { - errors = true; + firstError = key; + console.log(`Found error ${firstError}`); break; } else if (value) { - errors = hasErrors(value, keysToIgnore); - if (errors) { + firstError = findFirstError(value, false); + if (firstError) { + firstError = `${key}.${firstError}`; + console.log(`Found error ${firstError}`); break; } } } - return errors; + console.log(`Returning ${firstError}`); + return firstError; }; diff --git a/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/components/cold_phase/cold_phase.js b/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/components/cold_phase/cold_phase.js index 3e24bc18da1be..6ee756c142b61 100644 --- a/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/components/cold_phase/cold_phase.js +++ b/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/components/cold_phase/cold_phase.js @@ -22,6 +22,7 @@ import { EuiButton, } from '@elastic/eui'; import { + PHASE_COLD, PHASE_ENABLED, PHASE_ROLLOVER_ALIAS, PHASE_ROLLOVER_MINIMUM_AGE, @@ -118,6 +119,7 @@ export class ColdPhase extends PureComponent { @@ -173,6 +177,7 @@ export class HotPhase extends PureComponent { ({ - errors: validateLifecycle(state), - selectedPolicy: getSelectedPolicy(state), - affectedIndexTemplates: getAffectedIndexTemplates(state), - saveAsNewPolicy: getSaveAsNewPolicy(state), - lifecycle: getLifecycle(state), - policies: getPolicies(state), - isPolicyListLoaded: isPolicyListLoaded(state), - isNewPolicy: getIsNewPolicy(state), - }), + state => { + const errors = validateLifecycle(state); + const firstError = findFirstError(errors); + return { + firstError, + errors, + selectedPolicy: getSelectedPolicy(state), + affectedIndexTemplates: getAffectedIndexTemplates(state), + saveAsNewPolicy: getSaveAsNewPolicy(state), + lifecycle: getLifecycle(state), + policies: getPolicies(state), + isPolicyListLoaded: isPolicyListLoaded(state), + isNewPolicy: getIsNewPolicy(state), + }; + }, { setSelectedPolicy, setSelectedPolicyName, diff --git a/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/edit_policy.js b/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/edit_policy.js index 4686efb39928d..9715953c53d3b 100644 --- a/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/edit_policy.js +++ b/x-pack/plugins/index_lifecycle_management/public/sections/edit_policy/edit_policy.js @@ -34,7 +34,7 @@ import { PHASE_WARM, STRUCTURE_POLICY_NAME, } from '../../store/constants'; -import { hasErrors } from '../../lib/find_errors'; +import { findFirstError } from '../../lib/find_errors'; import { NodeAttrsDetails } from './components/node_attrs_details'; import { ErrableFormRow } from './form_errors'; @@ -87,13 +87,17 @@ export class EditPolicy extends Component { submit = async () => { this.setState({ isShowingErrors: true }); const { - errors, saveLifecyclePolicy, lifecycle, saveAsNewPolicy, + firstError } = this.props; - if (hasErrors(errors)) { + if (firstError) { toastNotifications.addDanger('Please the fix errors on the page'); + const element = document.getElementById(`${firstError}-row`); + if (element) { + element.scrollIntoView(); + } } else { const success = await saveLifecyclePolicy(lifecycle, saveAsNewPolicy); if (success) { @@ -176,6 +180,7 @@ export class EditPolicy extends Component { {saveAsNewPolicy || !policyName ? (