Skip to content

Commit

Permalink
[Ingest Manager] Fix create data source from integration (elastic#66626)
Browse files Browse the repository at this point in the history
* Fix add datasource button from integration not responsive

* Fix React key prop warnings

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
jen-huang and elasticmachine committed May 15, 2020
1 parent f542b3f commit 0442db5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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, { useState, useEffect } from 'react';
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import { useRouteMatch, useHistory } from 'react-router-dom';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
Expand Down Expand Up @@ -95,32 +95,46 @@ export const CreateDatasourcePage: React.FunctionComponent = () => {
// Datasource validation state
const [validationResults, setValidationResults] = useState<DatasourceValidationResults>();

// Form state
const [formState, setFormState] = useState<DatasourceFormState>('INVALID');

// Update package info method
const updatePackageInfo = (updatedPackageInfo: PackageInfo | undefined) => {
if (updatedPackageInfo) {
setPackageInfo(updatedPackageInfo);
setFormState('VALID');
} else {
setFormState('INVALID');
setPackageInfo(undefined);
}
const updatePackageInfo = useCallback(
(updatedPackageInfo: PackageInfo | undefined) => {
if (updatedPackageInfo) {
setPackageInfo(updatedPackageInfo);
if (agentConfig) {
setFormState('VALID');
}
} else {
setFormState('INVALID');
setPackageInfo(undefined);
}

// eslint-disable-next-line no-console
console.debug('Package info updated', updatedPackageInfo);
};
// eslint-disable-next-line no-console
console.debug('Package info updated', updatedPackageInfo);
},
[agentConfig, setPackageInfo, setFormState]
);

// Update agent config method
const updateAgentConfig = (updatedAgentConfig: AgentConfig | undefined) => {
if (updatedAgentConfig) {
setAgentConfig(updatedAgentConfig);
} else {
setFormState('INVALID');
setAgentConfig(undefined);
}
const updateAgentConfig = useCallback(
(updatedAgentConfig: AgentConfig | undefined) => {
if (updatedAgentConfig) {
setAgentConfig(updatedAgentConfig);
if (packageInfo) {
setFormState('VALID');
}
} else {
setFormState('INVALID');
setAgentConfig(undefined);
}

// eslint-disable-next-line no-console
console.debug('Agent config updated', updatedAgentConfig);
};
// eslint-disable-next-line no-console
console.debug('Agent config updated', updatedAgentConfig);
},
[packageInfo, setAgentConfig, setFormState]
);

const hasErrors = validationResults ? validationHasErrors(validationResults) : false;

Expand Down Expand Up @@ -167,7 +181,6 @@ export const CreateDatasourcePage: React.FunctionComponent = () => {
const cancelUrl = from === 'config' ? CONFIG_URL : PACKAGE_URL;

// Save datasource
const [formState, setFormState] = useState<DatasourceFormState>('INVALID');
const saveDatasource = async () => {
setFormState('LOADING');
const result = await sendCreateDatasource(datasource);
Expand Down Expand Up @@ -219,33 +232,43 @@ export const CreateDatasourcePage: React.FunctionComponent = () => {
packageInfo,
};

const stepSelectConfig = useMemo(
() => (
<StepSelectConfig
pkgkey={pkgkey}
updatePackageInfo={updatePackageInfo}
agentConfig={agentConfig}
updateAgentConfig={updateAgentConfig}
/>
),
[pkgkey, updatePackageInfo, agentConfig, updateAgentConfig]
);

const stepSelectPackage = useMemo(
() => (
<StepSelectPackage
agentConfigId={configId}
updateAgentConfig={updateAgentConfig}
packageInfo={packageInfo}
updatePackageInfo={updatePackageInfo}
/>
),
[configId, updateAgentConfig, packageInfo, updatePackageInfo]
);

const steps: EuiStepProps[] = [
from === 'package'
? {
title: i18n.translate('xpack.ingestManager.createDatasource.stepSelectAgentConfigTitle', {
defaultMessage: 'Select an agent configuration',
}),
children: (
<StepSelectConfig
pkgkey={pkgkey}
updatePackageInfo={updatePackageInfo}
agentConfig={agentConfig}
updateAgentConfig={updateAgentConfig}
/>
),
children: stepSelectConfig,
}
: {
title: i18n.translate('xpack.ingestManager.createDatasource.stepSelectPackageTitle', {
defaultMessage: 'Select an integration',
}),
children: (
<StepSelectPackage
agentConfigId={configId}
updateAgentConfig={updateAgentConfig}
packageInfo={packageInfo}
updatePackageInfo={updatePackageInfo}
/>
),
children: stepSelectPackage,
},
{
title: i18n.translate('xpack.ingestManager.createDatasource.stepDefineDatasourceTitle', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export const DatasourcesTable: React.FunctionComponent<Props> = ({
search={{
toolsRight: [
<EuiButton
key="addDatasourceButton"
isDisabled={!hasWriteCapabilities}
iconType="plusInCircle"
href={addDatasourceLink}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ export const DataStreamListPage: React.FunctionComponent<{}> = () => {
sorting={true}
search={{
toolsRight: [
<EuiButton color="primary" iconType="refresh" onClick={() => sendRequest()}>
<EuiButton
key="reloadButton"
color="primary"
iconType="refresh"
onClick={() => sendRequest()}
>
<FormattedMessage
id="xpack.ingestManager.dataStreamList.reloadDataStreamsButtonText"
defaultMessage="Reload"
Expand Down

0 comments on commit 0442db5

Please sign in to comment.