diff --git a/x-pack/legacy/plugins/ml/public/components/custom_hooks/index.ts b/x-pack/legacy/plugins/ml/public/components/custom_hooks/index.ts new file mode 100644 index 00000000000000..ffead802bd6f97 --- /dev/null +++ b/x-pack/legacy/plugins/ml/public/components/custom_hooks/index.ts @@ -0,0 +1,7 @@ +/* + * 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. + */ + +export { usePartialState } from './use_partial_state'; diff --git a/x-pack/legacy/plugins/ml/public/components/custom_hooks/use_partial_state.ts b/x-pack/legacy/plugins/ml/public/components/custom_hooks/use_partial_state.ts new file mode 100644 index 00000000000000..3bb7dbf6578ccc --- /dev/null +++ b/x-pack/legacy/plugins/ml/public/components/custom_hooks/use_partial_state.ts @@ -0,0 +1,21 @@ +/* + * 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 { useState } from 'react'; + +/** + * Custom hook for partial state update. + */ +export function usePartialState(initialValue: T): [T, (update: Partial) => void] { + const [state, setState] = useState(initialValue); + const setFormStateCallback = (update: Partial) => { + setState({ + ...state, + ...update, + }); + }; + return [state, setFormStateCallback]; +} diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/components/job_settings_form.tsx b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/components/job_settings_form.tsx index 7b6dfd46b3416b..e5ea8fe72cbbda 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/components/job_settings_form.tsx +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/components/job_settings_form.tsx @@ -8,15 +8,15 @@ import React, { FC, useEffect, useState } from 'react'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { - EuiFieldText, + EuiAccordion, + EuiButton, EuiDescribedFormGroup, + EuiFieldText, EuiForm, EuiFormRow, EuiSpacer, EuiSwitch, - EuiAccordion, EuiTextAlign, - EuiButton, } from '@elastic/eui'; import { JobGroupsInput } from '../../pages/components/job_details_step/components/groups/job_groups_input'; import { TimeRangePicker } from '../../pages/components/time_range_step/time_range_picker'; @@ -31,6 +31,7 @@ import { import { JOB_ID_MAX_LENGTH } from '../../../../../common/constants/validation'; import { isJobIdValid } from '../../../../../common/util/job_utils'; import { TimeRange } from '../../pages/components/time_range_step/time_range'; +import { usePartialState } from '../../../../components/custom_hooks'; export interface JobSettingsFormValues { jobPrefix: string; @@ -69,7 +70,7 @@ export const JobSettingsForm: FC = ({ maxLengthValidator(JOB_ID_MAX_LENGTH) ); - const [formState, setFormState] = useState({ + const [formState, setFormState] = usePartialState({ jobPrefix: '', startDatafeedAfterSave: true, useFullIndexData: true, @@ -84,7 +85,6 @@ export const JobSettingsForm: FC = ({ const onJobPrefixChange = (value: string) => { setFormState({ - ...formState, jobPrefix: value && value.toLowerCase(), }); }; @@ -176,7 +176,6 @@ export const JobSettingsForm: FC = ({ selectedGroups={formState.jobGroups} onChange={value => { setFormState({ - ...formState, jobGroups: value, }); }} @@ -204,7 +203,6 @@ export const JobSettingsForm: FC = ({ checked={formState.startDatafeedAfterSave} onChange={({ target: { checked } }) => { setFormState({ - ...formState, startDatafeedAfterSave: checked, }); }} @@ -224,22 +222,23 @@ export const JobSettingsForm: FC = ({ checked={formState.useFullIndexData} onChange={({ target: { checked } }) => { setFormState({ - ...formState, useFullIndexData: checked, }); }} /> {!formState.useFullIndexData && ( - { - setFormState({ - ...formState, - timeRange: value, - }); - }} - timeRange={formState.timeRange} - /> + <> + + { + setFormState({ + timeRange: value, + }); + }} + timeRange={formState.timeRange} + /> + )} = ({ checked={formState.useDedicatedIndex} onChange={({ target: { checked } }) => { setFormState({ - ...formState, useDedicatedIndex: checked, }); }} diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/page.tsx b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/page.tsx index a75a78a0629ae5..f4ac434083521c 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/page.tsx +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/recognize/page.tsx @@ -114,7 +114,7 @@ export const Page: FC = ({ moduleId, existingGroupIds }) => { const getTimeRange = async ( useFullIndexData: boolean, timeRange: TimeRange - ): Promise<{ start: number; end: number }> => { + ): Promise => { if (useFullIndexData) { const { start, end } = await ml.getTimeFieldRange({ index: indexPattern.title,