Skip to content

Commit 2ed016f

Browse files
committed
Fix use of useFormData after update
- also minor refactor to use useCallback in policy flyout now that getFormData changes when the form data changes.
1 parent 4f2a615 commit 2ed016f

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/phases/shared/data_tier_allocation_field/components/data_tier_allocation.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import React, { FunctionComponent } from 'react';
8+
import { get } from 'lodash';
89
import { i18n } from '@kbn/i18n';
910
import { EuiText, EuiSpacer, EuiSuperSelectOption } from '@elastic/eui';
1011

@@ -93,10 +94,12 @@ export const DataTierAllocation: FunctionComponent<SharedProps> = (props) => {
9394

9495
const dataTierAllocationTypePath = `_meta.${phase}.dataTierAllocationType`;
9596

96-
const [{ [dataTierAllocationTypePath]: dataTierAllocationType }] = useFormData({
97-
watch: [dataTierAllocationTypePath],
97+
const [formData] = useFormData({
98+
watch: dataTierAllocationTypePath,
9899
});
99100

101+
const dataTierAllocationType = get(formData, dataTierAllocationTypePath);
102+
100103
return (
101104
<div data-test-subj={`${phase}-dataTierAllocationControls`}>
102105
<UseField path={dataTierAllocationTypePath}>

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/phases/shared/data_tier_allocation_field/components/node_allocation.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import React, { useState, FunctionComponent } from 'react';
8+
import { get } from 'lodash';
89
import { FormattedMessage } from '@kbn/i18n/react';
910
import { i18n } from '@kbn/i18n';
1011
import { EuiButtonEmpty, EuiText, EuiSpacer } from '@elastic/eui';
@@ -43,10 +44,12 @@ const i18nTexts = {
4344
export const NodeAllocation: FunctionComponent<SharedProps> = ({ phase, nodes }) => {
4445
const allocationNodeAttributePath = `_meta.${phase}.allocationNodeAttribute`;
4546

46-
const [{ [allocationNodeAttributePath]: selectedAllocationNodeAttribute }] = useFormData({
47+
const [formData] = useFormData({
4748
watch: [allocationNodeAttributePath],
4849
});
4950

51+
const selectedAllocationNodeAttribute = get(formData, allocationNodeAttributePath);
52+
5053
const [selectedNodeAttrsForDetails, setSelectedNodeAttrsForDetails] = useState<string | null>(
5154
null
5255
);

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/phases/shared/data_tier_allocation_field/data_tier_allocation_field.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { get } from 'lodash';
78
import React, { FunctionComponent } from 'react';
89
import { i18n } from '@kbn/i18n';
910

@@ -44,8 +45,9 @@ export const DataTierAllocationField: FunctionComponent<Props> = ({ phase, descr
4445
services: { cloud },
4546
} = useKibana();
4647

47-
const allocationTypePath = `_meta.${phase}.dataTierAllocationType`;
48-
const [{ [allocationTypePath]: allocationType }] = useFormData({ watch: [allocationTypePath] });
48+
const dataTierAllocationTypePath = `_meta.${phase}.dataTierAllocationType`;
49+
const [formData] = useFormData({ watch: dataTierAllocationTypePath });
50+
const allocationType = get(formData, dataTierAllocationTypePath);
4951

5052
return (
5153
<NodesDataProvider>

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/phases/shared/min_age_input_field/min_age_input_field.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
76
import React, { FunctionComponent } from 'react';
7+
import { get } from 'lodash';
88
import { FormattedMessage } from '@kbn/i18n/react';
99
import { i18n } from '@kbn/i18n';
1010

@@ -29,7 +29,8 @@ interface Props {
2929
}
3030

3131
export const MinAgeInputField: FunctionComponent<Props> = ({ phase }): React.ReactElement => {
32-
const [{ [useRolloverPath]: rolloverEnabled }] = useFormData({ watch: useRolloverPath });
32+
const [formData] = useFormData({ watch: useRolloverPath });
33+
const rolloverEnabled = get(formData, useRolloverPath);
3334

3435
let daysOptionLabel;
3536
let hoursOptionLabel;

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/policy_json_flyout.tsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { useEffect, useState } from 'react';
7+
import React, { useCallback, useEffect, useState } from 'react';
88
import { i18n } from '@kbn/i18n';
99
import { FormattedMessage } from '@kbn/i18n/react';
1010

@@ -46,27 +46,28 @@ export const PolicyJsonFlyout: React.FunctionComponent<Props> = ({
4646
const [policy, setPolicy] = useState<undefined | null | SerializedPolicy>(undefined);
4747

4848
const form = useFormContext();
49-
const [formData, getFormData] = useFormData<FormInternal>();
49+
const [, getFormData] = useFormData<FormInternal>();
50+
51+
const getPolicy = useCallback(async () => {
52+
setPolicy(undefined);
53+
if (await form.validate()) {
54+
const p = getFormData() as SerializedPolicy;
55+
setPolicy({
56+
...legacyPolicy,
57+
phases: {
58+
...legacyPolicy.phases,
59+
hot: p.phases.hot,
60+
warm: p.phases.warm,
61+
},
62+
});
63+
} else {
64+
setPolicy(null);
65+
}
66+
}, [setPolicy, getFormData, legacyPolicy, form]);
5067

5168
useEffect(() => {
52-
(async function checkPolicy() {
53-
setPolicy(undefined);
54-
if (await form.validate()) {
55-
const p = getFormData() as SerializedPolicy;
56-
setPolicy({
57-
...legacyPolicy,
58-
phases: {
59-
...legacyPolicy.phases,
60-
hot: p.phases.hot,
61-
warm: p.phases.warm,
62-
},
63-
});
64-
} else {
65-
setPolicy(null);
66-
}
67-
})();
68-
// eslint-disable-next-line react-hooks/exhaustive-deps
69-
}, [form, legacyPolicy, formData]);
69+
getPolicy();
70+
}, [getPolicy]);
7071

7172
let content: React.ReactNode;
7273
switch (policy) {

0 commit comments

Comments
 (0)