Skip to content

Commit

Permalink
Fix use of useFormData after update
Browse files Browse the repository at this point in the history
- also minor refactor to use useCallback in policy flyout now
  that getFormData changes when the form data changes.
  • Loading branch information
jloleysens committed Oct 21, 2020
1 parent 4f2a615 commit 2ed016f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import React, { FunctionComponent } from 'react';
import { get } from 'lodash';
import { i18n } from '@kbn/i18n';
import { EuiText, EuiSpacer, EuiSuperSelectOption } from '@elastic/eui';

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

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

const [{ [dataTierAllocationTypePath]: dataTierAllocationType }] = useFormData({
watch: [dataTierAllocationTypePath],
const [formData] = useFormData({
watch: dataTierAllocationTypePath,
});

const dataTierAllocationType = get(formData, dataTierAllocationTypePath);

return (
<div data-test-subj={`${phase}-dataTierAllocationControls`}>
<UseField path={dataTierAllocationTypePath}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

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

const [{ [allocationNodeAttributePath]: selectedAllocationNodeAttribute }] = useFormData({
const [formData] = useFormData({
watch: [allocationNodeAttributePath],
});

const selectedAllocationNodeAttribute = get(formData, allocationNodeAttributePath);

const [selectedNodeAttrsForDetails, setSelectedNodeAttrsForDetails] = useState<string | null>(
null
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { get } from 'lodash';
import React, { FunctionComponent } from 'react';
import { i18n } from '@kbn/i18n';

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

const allocationTypePath = `_meta.${phase}.dataTierAllocationType`;
const [{ [allocationTypePath]: allocationType }] = useFormData({ watch: [allocationTypePath] });
const dataTierAllocationTypePath = `_meta.${phase}.dataTierAllocationType`;
const [formData] = useFormData({ watch: dataTierAllocationTypePath });
const allocationType = get(formData, dataTierAllocationTypePath);

return (
<NodesDataProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FunctionComponent } from 'react';
import { get } from 'lodash';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';

Expand All @@ -29,7 +29,8 @@ interface Props {
}

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

let daysOptionLabel;
let hoursOptionLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

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

const form = useFormContext();
const [formData, getFormData] = useFormData<FormInternal>();
const [, getFormData] = useFormData<FormInternal>();

const getPolicy = useCallback(async () => {
setPolicy(undefined);
if (await form.validate()) {
const p = getFormData() as SerializedPolicy;
setPolicy({
...legacyPolicy,
phases: {
...legacyPolicy.phases,
hot: p.phases.hot,
warm: p.phases.warm,
},
});
} else {
setPolicy(null);
}
}, [setPolicy, getFormData, legacyPolicy, form]);

useEffect(() => {
(async function checkPolicy() {
setPolicy(undefined);
if (await form.validate()) {
const p = getFormData() as SerializedPolicy;
setPolicy({
...legacyPolicy,
phases: {
...legacyPolicy.phases,
hot: p.phases.hot,
warm: p.phases.warm,
},
});
} else {
setPolicy(null);
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [form, legacyPolicy, formData]);
getPolicy();
}, [getPolicy]);

let content: React.ReactNode;
switch (policy) {
Expand Down

0 comments on commit 2ed016f

Please sign in to comment.