Skip to content

Commit

Permalink
[ML] custom hook for partial state update
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Oct 10, 2019
1 parent e0d2c17 commit 81c3081
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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<T>(initialValue: T): [T, (update: Partial<T>) => void] {
const [state, setState] = useState<T>(initialValue);
const setFormStateCallback = (update: Partial<T>) => {
setState({
...state,
...update,
});
};
return [state, setFormStateCallback];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -69,7 +70,7 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({
maxLengthValidator(JOB_ID_MAX_LENGTH)
);

const [formState, setFormState] = useState<JobSettingsFormValues>({
const [formState, setFormState] = usePartialState({
jobPrefix: '',
startDatafeedAfterSave: true,
useFullIndexData: true,
Expand All @@ -84,7 +85,6 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({

const onJobPrefixChange = (value: string) => {
setFormState({
...formState,
jobPrefix: value && value.toLowerCase(),
});
};
Expand Down Expand Up @@ -176,7 +176,6 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({
selectedGroups={formState.jobGroups}
onChange={value => {
setFormState({
...formState,
jobGroups: value,
});
}}
Expand Down Expand Up @@ -204,7 +203,6 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({
checked={formState.startDatafeedAfterSave}
onChange={({ target: { checked } }) => {
setFormState({
...formState,
startDatafeedAfterSave: checked,
});
}}
Expand All @@ -224,22 +222,23 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({
checked={formState.useFullIndexData}
onChange={({ target: { checked } }) => {
setFormState({
...formState,
useFullIndexData: checked,
});
}}
/>
</EuiFormRow>
{!formState.useFullIndexData && (
<TimeRangePicker
setTimeRange={value => {
setFormState({
...formState,
timeRange: value,
});
}}
timeRange={formState.timeRange}
/>
<>
<EuiSpacer size="m" />
<TimeRangePicker
setTimeRange={value => {
setFormState({
timeRange: value,
});
}}
timeRange={formState.timeRange}
/>
</>
)}
<EuiSpacer size="l" />
<EuiAccordion
Expand Down Expand Up @@ -279,7 +278,6 @@ export const JobSettingsForm: FC<JobSettingsFormProps> = ({
checked={formState.useDedicatedIndex}
onChange={({ target: { checked } }) => {
setFormState({
...formState,
useDedicatedIndex: checked,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const Page: FC<PageProps> = ({ moduleId, existingGroupIds }) => {
const getTimeRange = async (
useFullIndexData: boolean,
timeRange: TimeRange
): Promise<{ start: number; end: number }> => {
): Promise<TimeRange> => {
if (useFullIndexData) {
const { start, end } = await ml.getTimeFieldRange({
index: indexPattern.title,
Expand Down

0 comments on commit 81c3081

Please sign in to comment.